fixup_deletions.php 4.5 KB

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