fixup_deletions.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 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);
  22. $longoptions = array('dry-run', 'start=', 'end=');
  23. $helptext = <<<END_OF_USERROLE_HELP
  24. fixup_deletions.php [options]
  25. Finds notices posted by deleted users and cleans them up.
  26. Stray incompletely deleted items cause various fun problems!
  27. --dry-run look but don't touch
  28. --start=N start looking at profile_id N instead of 1
  29. --end=N end looking at profile_id N instead of the max
  30. END_OF_USERROLE_HELP;
  31. require_once INSTALLDIR.'/scripts/commandline.inc';
  32. /**
  33. * Find the highest profile_id currently listed in the notice table;
  34. * this field is indexed and should return very quickly.
  35. *
  36. * We check notice.profile_id rather than profile.id because we're
  37. * looking for notices left behind after deletion; if the most recent
  38. * accounts were deleted, we wouldn't have them from profile.
  39. *
  40. * @return int
  41. * @access private
  42. */
  43. function get_max_profile_id()
  44. {
  45. $query = 'SELECT MAX(profile_id) AS id FROM notice';
  46. $profile = new Profile();
  47. $profile->query($query);
  48. if ($profile->fetch()) {
  49. return intval($profile->id);
  50. } else {
  51. die("Something went awry; could not look up max used profile_id.");
  52. }
  53. }
  54. /**
  55. * Check for profiles in the given id range that are missing, presumed deleted.
  56. *
  57. * @param int $start beginning profile.id, inclusive
  58. * @param int $end final profile.id, inclusive
  59. * @return array of integer profile.ids
  60. * @access private
  61. */
  62. function get_missing_profiles($start, $end)
  63. {
  64. $query = sprintf("SELECT id FROM profile WHERE id BETWEEN %d AND %d",
  65. $start, $end);
  66. $profile = new Profile();
  67. $profile->query($query);
  68. $all = range($start, $end);
  69. $known = array();
  70. while ($row = $profile->fetch()) {
  71. $known[] = intval($profile->id);
  72. }
  73. unset($profile);
  74. $missing = array_diff($all, $known);
  75. return $missing;
  76. }
  77. /**
  78. * Look for stray notices from this profile and, if present, kill them.
  79. *
  80. * @param int $profile_id
  81. * @param bool $dry if true, we won't delete anything
  82. */
  83. function cleanup_missing_profile($profile_id, $dry)
  84. {
  85. $notice = new Notice();
  86. $notice->profile_id = $profile_id;
  87. $notice->find();
  88. if ($notice->N == 0) {
  89. return;
  90. }
  91. $s = ($notice->N == 1) ? '' : 's';
  92. print "Deleted profile $profile_id has $notice->N stray notice$s:\n";
  93. while ($notice->fetch()) {
  94. print " notice $notice->id";
  95. if ($dry) {
  96. print " (skipped; dry run)\n";
  97. } else {
  98. $victim = clone($notice);
  99. try {
  100. $victim->delete();
  101. print " (deleted)\n";
  102. } catch (Exception $e) {
  103. print " FAILED: ";
  104. print $e->getMessage();
  105. print "\n";
  106. }
  107. }
  108. }
  109. }
  110. $dry = have_option('dry-run');
  111. $max_profile_id = get_max_profile_id();
  112. $chunk = 1000;
  113. if (have_option('start')) {
  114. $begin = intval(get_option_value('start'));
  115. } else {
  116. $begin = 1;
  117. }
  118. if (have_option('end')) {
  119. $final = min($max_profile_id, intval(get_option_value('end')));
  120. } else {
  121. $final = $max_profile_id;
  122. }
  123. if ($begin < 1) {
  124. die("Silly human, you can't begin before profile number 1!\n");
  125. }
  126. if ($final < $begin) {
  127. die("Silly human, you can't end at $final if it's before $begin!\n");
  128. }
  129. // Identify missing profiles...
  130. for ($start = $begin; $start <= $final; $start += $chunk) {
  131. $end = min($start + $chunk - 1, $final);
  132. print "Checking for missing profiles between id $start and $end";
  133. if ($dry) {
  134. print " (dry run)";
  135. }
  136. print "...\n";
  137. $missing = get_missing_profiles($start, $end);
  138. foreach ($missing as $profile_id) {
  139. cleanup_missing_profile($profile_id, $dry);
  140. }
  141. }
  142. echo "done.\n";