4 * StatusNet - a distributed open-source microblogging tool
5 * Copyright (C) 2010 StatusNet, Inc.
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.
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.
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/>.
21 define('INSTALLDIR', dirname(__DIR__));
22 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
24 $longoptions = array('dry-run', 'start=', 'end=');
26 $helptext = <<<END_OF_USERROLE_HELP
27 fixup_deletions.php [options]
28 Finds notices posted by deleted users and cleans them up.
29 Stray incompletely deleted items cause various fun problems!
31 --dry-run look but don't touch
32 --start=N start looking at profile_id N instead of 1
33 --end=N end looking at profile_id N instead of the max
37 require_once INSTALLDIR.'/scripts/commandline.inc';
40 * Find the highest profile_id currently listed in the notice table;
41 * this field is indexed and should return very quickly.
43 * We check notice.profile_id rather than profile.id because we're
44 * looking for notices left behind after deletion; if the most recent
45 * accounts were deleted, we wouldn't have them from profile.
50 function get_max_profile_id()
52 $query = 'SELECT MAX(profile_id) AS id FROM notice';
54 $profile = new Profile();
55 $profile->query($query);
57 if ($profile->fetch()) {
58 return intval($profile->id);
60 die("Something went awry; could not look up max used profile_id.");
65 * Check for profiles in the given id range that are missing, presumed deleted.
67 * @param int $start beginning profile.id, inclusive
68 * @param int $end final profile.id, inclusive
69 * @return array of integer profile.ids
72 function get_missing_profiles($start, $end)
74 $query = sprintf("SELECT id FROM profile WHERE id BETWEEN %d AND %d",
77 $profile = new Profile();
78 $profile->query($query);
80 $all = range($start, $end);
82 while ($row = $profile->fetch()) {
83 $known[] = intval($profile->id);
87 $missing = array_diff($all, $known);
92 * Look for stray notices from this profile and, if present, kill them.
94 * @param int $profile_id
95 * @param bool $dry if true, we won't delete anything
97 function cleanup_missing_profile($profile_id, $dry)
99 $notice = new Notice();
100 $notice->profile_id = $profile_id;
102 if ($notice->N == 0) {
106 $s = ($notice->N == 1) ? '' : 's';
107 print "Deleted profile $profile_id has $notice->N stray notice$s:\n";
109 while ($notice->fetch()) {
110 print " notice $notice->id";
112 print " (skipped; dry run)\n";
114 $victim = clone($notice);
117 print " (deleted)\n";
118 } catch (Exception $e) {
120 print $e->getMessage();
127 $dry = have_option('dry-run');
129 $max_profile_id = get_max_profile_id();
132 if (have_option('start')) {
133 $begin = intval(get_option_value('start'));
137 if (have_option('end')) {
138 $final = min($max_profile_id, intval(get_option_value('end')));
140 $final = $max_profile_id;
144 die("Silly human, you can't begin before profile number 1!\n");
146 if ($final < $begin) {
147 die("Silly human, you can't end at $final if it's before $begin!\n");
150 // Identify missing profiles...
151 for ($start = $begin; $start <= $final; $start += $chunk) {
152 $end = min($start + $chunk - 1, $final);
154 print "Checking for missing profiles between id $start and $end";
159 $missing = get_missing_profiles($start, $end);
161 foreach ($missing as $profile_id) {
162 cleanup_missing_profile($profile_id, $dry);