strip_geo.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2009-2010, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', dirname(__DIR__));
  21. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  22. $shortoptions = 'i::n::y';
  23. $longoptions = array('id=', 'nickname=', 'yes', 'dry-run', 'all');
  24. $helptext = <<<END_OF_HELP
  25. strip_geo.php [options]
  26. Removes geolocation info from the given user's notices.
  27. -i --id ID of the user (may be a remote profile)
  28. -n --nickname nickname of the user
  29. -y --yes do not wait for confirmation
  30. --dry-run list affected notices without deleting
  31. --all run over and decache all messages, even if they don't
  32. have geo data now (helps to fix cache bugs)
  33. END_OF_HELP;
  34. require_once INSTALLDIR.'/scripts/commandline.inc';
  35. if (have_option('i', 'id')) {
  36. $id = get_option_value('i', 'id');
  37. $profile = Profile::getKV('id', $id);
  38. if (empty($profile)) {
  39. print "Can't find local or remote profile with ID $id\n";
  40. exit(1);
  41. }
  42. } else if (have_option('n', 'nickname')) {
  43. $nickname = get_option_value('n', 'nickname');
  44. $user = User::getKV('nickname', $nickname);
  45. if (empty($user)) {
  46. print "Can't find local user with nickname '$nickname'\n";
  47. exit(1);
  48. }
  49. $profile = $user->getProfile();
  50. } else {
  51. print "You must provide either an ID or a nickname.\n\n";
  52. show_help();
  53. exit(1);
  54. }
  55. if (!have_option('y', 'yes') && !have_option('--dry-run')) {
  56. print "About to PERMANENTLY remove geolocation data from user '{$profile->nickname}' ({$profile->id})'s notices. Are you sure? [y/N] ";
  57. $response = fgets(STDIN);
  58. if (strtolower(trim($response)) != 'y') {
  59. print "Aborting.\n";
  60. exit(0);
  61. }
  62. }
  63. // @fixme for a very prolific poster this could be too many.
  64. $notice = new Notice();
  65. $notice->profile_id = $profile->id;
  66. if (have_option('--all')) {
  67. print "Finding all notices by $profile->nickname...";
  68. } else {
  69. print "Finding notices by $profile->nickname with geolocation data...";
  70. $notice->whereAdd("lat != ''");
  71. }
  72. $notice->find();
  73. if ($notice->N) {
  74. print " $notice->N found.\n";
  75. while ($notice->fetch()) {
  76. print "notice id $notice->id ";
  77. if (have_option('v') || have_option('--verbose')) {
  78. print "({$notice->lat},{$notice->lon}) ";
  79. if ($notice->location_ns) {
  80. print "ns {$notice->location_ns} id {$notice->location_id} ";
  81. }
  82. }
  83. if (have_option('--dry-run')) {
  84. // sucka
  85. echo "(skipped)";
  86. } else {
  87. // note: setting fields to null and calling update() doesn't save the nulled fields
  88. $orig = clone($notice);
  89. $update = clone($notice);
  90. // In theory we could hit a chunk of notices at once in the UPDATE,
  91. // but we're going to have to decache them individually anyway and
  92. // it doesn't hurt to make sure we don't hold up replication with
  93. // what might be a very slow single UPDATE.
  94. $query = sprintf('UPDATE notice ' .
  95. 'SET lat=NULL,lon=NULL,location_ns=NULL,location_id=NULL ' .
  96. 'WHERE id=%d', $notice->id);
  97. $ok = $update->query($query);
  98. if ($ok) {
  99. // And now we decache him manually, as query() doesn't know what we're doing...
  100. $orig->decache();
  101. echo "(removed)";
  102. } else {
  103. echo "(unchanged?)";
  104. }
  105. }
  106. print "\n";
  107. }
  108. } else {
  109. print " none found.\n";
  110. }
  111. print "DONE.\n";