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