strip_geo.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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', realpath(dirname(__FILE__) . '/..'));
  21. $shortoptions = 'i::n::y';
  22. $longoptions = array('id=', 'nickname=', 'yes', 'dry-run', 'all');
  23. $helptext = <<<END_OF_HELP
  24. strip_geo.php [options]
  25. Removes geolocation info from the given user's notices.
  26. -i --id ID of the user (may be a remote profile)
  27. -n --nickname nickname of the user
  28. -y --yes do not wait for confirmation
  29. --dry-run list affected notices without deleting
  30. --all run over and decache all messages, even if they don't
  31. have geo data now (helps to fix cache bugs)
  32. END_OF_HELP;
  33. require_once INSTALLDIR.'/scripts/commandline.inc';
  34. if (have_option('i', 'id')) {
  35. $id = get_option_value('i', 'id');
  36. $profile = Profile::getKV('id', $id);
  37. if (empty($profile)) {
  38. print "Can't find local or remote profile with ID $id\n";
  39. exit(1);
  40. }
  41. } else if (have_option('n', 'nickname')) {
  42. $nickname = get_option_value('n', 'nickname');
  43. $user = User::getKV('nickname', $nickname);
  44. if (empty($user)) {
  45. print "Can't find local user with nickname '$nickname'\n";
  46. exit(1);
  47. }
  48. $profile = $user->getProfile();
  49. } else {
  50. print "You must provide either an ID or a nickname.\n\n";
  51. show_help();
  52. exit(1);
  53. }
  54. if (!have_option('y', 'yes') && !have_option('--dry-run')) {
  55. print "About to PERMANENTLY remove geolocation data from user '{$profile->nickname}' ({$profile->id})'s notices. Are you sure? [y/N] ";
  56. $response = fgets(STDIN);
  57. if (strtolower(trim($response)) != 'y') {
  58. print "Aborting.\n";
  59. exit(0);
  60. }
  61. }
  62. // @fixme for a very prolific poster this could be too many.
  63. $notice = new Notice();
  64. $notice->profile_id = $profile->id;
  65. if (have_option('--all')) {
  66. print "Finding all notices by $profile->nickname...";
  67. } else {
  68. print "Finding notices by $profile->nickname with geolocation data...";
  69. $notice->whereAdd("lat != ''");
  70. }
  71. $notice->find();
  72. if ($notice->N) {
  73. print " $notice->N found.\n";
  74. while ($notice->fetch()) {
  75. print "notice id $notice->id ";
  76. if (have_option('v') || have_option('--verbose')) {
  77. print "({$notice->lat},{$notice->lon}) ";
  78. if ($notice->location_ns) {
  79. print "ns {$notice->location_ns} id {$notice->location_id} ";
  80. }
  81. }
  82. if (have_option('--dry-run')) {
  83. // sucka
  84. echo "(skipped)";
  85. } else {
  86. // note: setting fields to null and calling update() doesn't save the nulled fields
  87. $orig = clone($notice);
  88. $update = clone($notice);
  89. // In theory we could hit a chunk of notices at once in the UPDATE,
  90. // but we're going to have to decache them individually anyway and
  91. // it doesn't hurt to make sure we don't hold up replication with
  92. // what might be a very slow single UPDATE.
  93. $query = sprintf('UPDATE notice ' .
  94. 'SET lat=NULL,lon=NULL,location_ns=NULL,location_id=NULL ' .
  95. 'WHERE id=%d', $notice->id);
  96. $ok = $update->query($query);
  97. if ($ok) {
  98. // And now we decache him manually, as query() doesn't know what we're doing...
  99. $orig->decache();
  100. echo "(removed)";
  101. } else {
  102. echo "(unchanged?)";
  103. }
  104. }
  105. print "\n";
  106. }
  107. } else {
  108. print " none found.\n";
  109. }
  110. print "DONE.\n";