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', realpath(dirname(__FILE__) . '/..'));
23 $longoptions = array('dry-run', 'start=', 'end=');
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!
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
36 require_once INSTALLDIR.'/scripts/commandline.inc.php';
39 * Find the highest profile_id currently listed in the notice table;
40 * this field is indexed and should return very quickly.
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.
49 function get_max_profile_id()
51 $query = 'SELECT MAX(profile_id) AS id FROM notice';
53 $profile = new Profile();
54 $profile->query($query);
56 if ($profile->fetch()) {
57 return intval($profile->id);
59 die("Something went awry; could not look up max used profile_id.");
64 * Check for profiles in the given id range that are missing, presumed deleted.
66 * @param int $start beginning profile.id, inclusive
67 * @param int $end final profile.id, inclusive
68 * @return array of integer profile.ids
71 function get_missing_profiles($start, $end)
73 $query = sprintf("SELECT id FROM profile WHERE id BETWEEN %d AND %d",
76 $profile = new Profile();
77 $profile->query($query);
79 $all = range($start, $end);
81 while ($row = $profile->fetch()) {
82 $known[] = intval($profile->id);
86 $missing = array_diff($all, $known);
91 * Look for stray notices from this profile and, if present, kill them.
93 * @param int $profile_id
94 * @param bool $dry if true, we won't delete anything
96 function cleanup_missing_profile($profile_id, $dry)
98 $notice = new Notice();
99 $notice->profile_id = $profile_id;
101 if ($notice->N == 0) {
105 $s = ($notice->N == 1) ? '' : 's';
106 print "Deleted profile $profile_id has $notice->N stray notice$s:\n";
108 while ($notice->fetch()) {
109 print " notice $notice->id";
111 print " (skipped; dry run)\n";
113 $victim = clone($notice);
116 print " (deleted)\n";
117 } catch (Exception $e) {
119 print $e->getMessage();
126 $dry = have_option('dry-run');
128 $max_profile_id = get_max_profile_id();
131 if (have_option('start')) {
132 $begin = intval(get_option_value('start'));
136 if (have_option('end')) {
137 $final = min($max_profile_id, intval(get_option_value('end')));
139 $final = $max_profile_id;
143 die("Silly human, you can't begin before profile number 1!\n");
145 if ($final < $begin) {
146 die("Silly human, you can't end at $final if it's before $begin!\n");
149 // Identify missing profiles...
150 for ($start = $begin; $start <= $final; $start += $chunk) {
151 $end = min($start + $chunk - 1, $final);
153 print "Checking for missing profiles between id $start and $end";
158 $missing = get_missing_profiles($start, $end);
160 foreach ($missing as $profile_id) {
161 cleanup_missing_profile($profile_id, $dry);