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