strip_geo.php 4.4 KB

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