Pass phpcs-strict on maintenance/ (2/8)
[lhc/web/wiklou.git] / maintenance / rebuildrecentchanges.php
1 <?php
2 /**
3 * Rebuild recent changes from scratch. This takes several hours,
4 * depending on the database size and server configuration.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 * @todo Document
24 */
25
26 require_once __DIR__ . '/Maintenance.php';
27
28 /**
29 * Maintenance script that rebuilds recent changes from scratch.
30 *
31 * @ingroup Maintenance
32 */
33 class RebuildRecentchanges extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Rebuild recent changes";
37 }
38
39 public function execute() {
40 $this->rebuildRecentChangesTablePass1();
41 $this->rebuildRecentChangesTablePass2();
42 $this->rebuildRecentChangesTablePass3();
43 $this->rebuildRecentChangesTablePass4();
44 $this->purgeFeeds();
45 $this->output( "Done.\n" );
46 }
47
48 /**
49 * Rebuild pass 1
50 * DOCUMENT ME!
51 */
52 private function rebuildRecentChangesTablePass1() {
53 $dbw = wfGetDB( DB_MASTER );
54
55 $dbw->delete( 'recentchanges', '*' );
56
57 $this->output( "Loading from page and revision tables...\n" );
58
59 global $wgRCMaxAge;
60
61 $this->output( '$wgRCMaxAge=' . $wgRCMaxAge );
62 $days = $wgRCMaxAge / 24 / 3600;
63 if ( intval( $days ) == $days ) {
64 $this->output( " (" . $days . " days)\n" );
65 } else {
66 $this->output( " (approx. " . intval( $days ) . " days)\n" );
67 }
68
69 $cutoff = time() - $wgRCMaxAge;
70 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
71 array(
72 'rc_timestamp' => 'rev_timestamp',
73 'rc_user' => 'rev_user',
74 'rc_user_text' => 'rev_user_text',
75 'rc_namespace' => 'page_namespace',
76 'rc_title' => 'page_title',
77 'rc_comment' => 'rev_comment',
78 'rc_minor' => 'rev_minor_edit',
79 'rc_bot' => 0,
80 'rc_new' => 'page_is_new',
81 'rc_cur_id' => 'page_id',
82 'rc_this_oldid' => 'rev_id',
83 'rc_last_oldid' => 0, // is this ok?
84 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
85 'rc_source' => $dbw->conditional(
86 'page_is_new != 0',
87 $dbw->addQuotes( RecentChange::SRC_NEW ),
88 $dbw->addQuotes( RecentChange::SRC_EDIT )
89 ),
90 'rc_deleted' => 'rev_deleted'
91 ), array(
92 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
93 'rev_page=page_id'
94 ), __METHOD__,
95 array(), // INSERT options
96 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
97 );
98 }
99
100 /**
101 * Rebuild pass 2
102 * DOCUMENT ME!
103 */
104 private function rebuildRecentChangesTablePass2() {
105 $dbw = wfGetDB( DB_MASTER );
106 list( $recentchanges, $revision ) = $dbw->tableNamesN( 'recentchanges', 'revision' );
107
108 $this->output( "Updating links and size differences...\n" );
109
110 # Fill in the rc_last_oldid field, which points to the previous edit
111 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
112 "ORDER BY rc_cur_id,rc_timestamp";
113 $res = $dbw->query( $sql, DB_MASTER );
114
115 $lastCurId = 0;
116 $lastOldId = 0;
117 foreach ( $res as $obj ) {
118 $new = 0;
119 if ( $obj->rc_cur_id != $lastCurId ) {
120 # Switch! Look up the previous last edit, if any
121 $lastCurId = intval( $obj->rc_cur_id );
122 $emit = $obj->rc_timestamp;
123 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
124 "WHERE rev_page={$lastCurId} " .
125 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC";
126 $sql2 = $dbw->limitResult( $sql2, 1, false );
127 $res2 = $dbw->query( $sql2 );
128 $row = $dbw->fetchObject( $res2 );
129 if ( $row ) {
130 $lastOldId = intval( $row->rev_id );
131 # Grab the last text size if available
132 $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : null;
133 } else {
134 # No previous edit
135 $lastOldId = 0;
136 $lastSize = null;
137 $new = 1; // probably true
138 }
139 }
140 if ( $lastCurId == 0 ) {
141 $this->output( "Uhhh, something wrong? No curid\n" );
142 } else {
143 # Grab the entry's text size
144 $size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid ) );
145
146 $dbw->update( 'recentchanges',
147 array(
148 'rc_last_oldid' => $lastOldId,
149 'rc_new' => $new,
150 'rc_type' => $new,
151 'rc_source' => $new === 1 ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT,
152 'rc_old_len' => $lastSize,
153 'rc_new_len' => $size,
154 ), array(
155 'rc_cur_id' => $lastCurId,
156 'rc_this_oldid' => $obj->rc_this_oldid,
157 ),
158 __METHOD__
159 );
160
161 $lastOldId = intval( $obj->rc_this_oldid );
162 $lastSize = $size;
163 }
164 }
165 }
166
167 /**
168 * Rebuild pass 3
169 * DOCUMENT ME!
170 */
171 private function rebuildRecentChangesTablePass3() {
172 $dbw = wfGetDB( DB_MASTER );
173
174 $this->output( "Loading from user, page, and logging tables...\n" );
175
176 global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
177 // Some logs don't go in RC. This should check for that
178 $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
179
180 // Escape...blah blah
181 $selectLogs = array();
182 foreach ( $basicRCLogs as $logtype ) {
183 $safetype = $dbw->strencode( $logtype );
184 $selectLogs[] = "'$safetype'";
185 }
186
187 $cutoff = time() - $wgRCMaxAge;
188 list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
189 $dbw->insertSelect(
190 'recentchanges',
191 array(
192 'user',
193 "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)"
194 ),
195 array(
196 'rc_timestamp' => 'log_timestamp',
197 'rc_user' => 'log_user',
198 'rc_user_text' => 'user_name',
199 'rc_namespace' => 'log_namespace',
200 'rc_title' => 'log_title',
201 'rc_comment' => 'log_comment',
202 'rc_minor' => 0,
203 'rc_bot' => 0,
204 'rc_patrolled' => 1,
205 'rc_new' => 0,
206 'rc_this_oldid' => 0,
207 'rc_last_oldid' => 0,
208 'rc_type' => RC_LOG,
209 'rc_source' => $dbw->addQuotes( RecentChange::SRC_LOG ),
210 'rc_cur_id' => $dbw->cascadingDeletes() ? 'page_id' : 'COALESCE(page_id, 0)',
211 'rc_log_type' => 'log_type',
212 'rc_log_action' => 'log_action',
213 'rc_logid' => 'log_id',
214 'rc_params' => 'log_params',
215 'rc_deleted' => 'log_deleted'
216 ), array(
217 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
218 'log_user=user_id',
219 'log_type IN(' . implode( ',', $selectLogs ) . ')'
220 ), __METHOD__,
221 array(), // INSERT options
222 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
223 );
224 }
225
226 /**
227 * Rebuild pass 4
228 * DOCUMENT ME!
229 */
230 private function rebuildRecentChangesTablePass4() {
231 global $wgUseRCPatrol;
232
233 $dbw = wfGetDB( DB_MASTER );
234
235 list( $recentchanges, $usergroups, $user ) =
236 $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
237
238 $botgroups = User::getGroupsWithPermission( 'bot' );
239 $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : array();
240 # Flag our recent bot edits
241 if ( !empty( $botgroups ) ) {
242 $botwhere = $dbw->makeList( $botgroups );
243 $botusers = array();
244
245 $this->output( "Flagging bot account edits...\n" );
246
247 # Find all users that are bots
248 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
249 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
250 $res = $dbw->query( $sql, DB_MASTER );
251
252 foreach ( $res as $obj ) {
253 $botusers[] = $dbw->addQuotes( $obj->user_name );
254 }
255 # Fill in the rc_bot field
256 if ( !empty( $botusers ) ) {
257 $botwhere = implode( ',', $botusers );
258 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
259 "WHERE rc_user_text IN($botwhere)";
260 $dbw->query( $sql2 );
261 }
262 }
263 global $wgMiserMode;
264 # Flag our recent autopatrolled edits
265 if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
266 $patrolwhere = $dbw->makeList( $autopatrolgroups );
267 $patrolusers = array();
268
269 $this->output( "Flagging auto-patrolled edits...\n" );
270
271 # Find all users in RC with autopatrol rights
272 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
273 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
274 $res = $dbw->query( $sql, DB_MASTER );
275
276 foreach ( $res as $obj ) {
277 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
278 }
279
280 # Fill in the rc_patrolled field
281 if ( !empty( $patrolusers ) ) {
282 $patrolwhere = implode( ',', $patrolusers );
283 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
284 "WHERE rc_user_text IN($patrolwhere)";
285 $dbw->query( $sql2 );
286 }
287 }
288 }
289
290 /**
291 * Purge cached feeds in $messageMemc
292 */
293 private function purgeFeeds() {
294 global $wgFeedClasses, $messageMemc;
295
296 $this->output( "Deleting feed timestamps.\n" );
297
298 foreach ( $wgFeedClasses as $feed => $className ) {
299 $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
300 }
301 }
302
303 }
304
305 $maintClass = "RebuildRecentchanges";
306 require_once RUN_MAINTENANCE_IF_MAIN;