Merge "Rename autonym for 'no' from 'norsk bokmål' to 'norsk'"
[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 use MediaWiki\MediaWikiServices;
28
29 /**
30 * Maintenance script that rebuilds recent changes from scratch.
31 *
32 * @ingroup Maintenance
33 */
34 class RebuildRecentchanges extends Maintenance {
35 /** @var integer UNIX timestamp */
36 private $cutoffFrom;
37 /** @var integer UNIX timestamp */
38 private $cutoffTo;
39
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Rebuild recent changes' );
43
44 $this->addOption(
45 'from',
46 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
47 false,
48 true
49 );
50 $this->addOption(
51 'to',
52 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
53 false,
54 true
55 );
56 $this->setBatchSize( 200 );
57 }
58
59 public function execute() {
60 if (
61 ( $this->hasOption( 'from' ) && !$this->hasOption( 'to' ) ) ||
62 ( !$this->hasOption( 'from' ) && $this->hasOption( 'to' ) )
63 ) {
64 $this->error( "Both 'from' and 'to' must be given, or neither", 1 );
65 }
66
67 $this->rebuildRecentChangesTablePass1();
68 $this->rebuildRecentChangesTablePass2();
69 $this->rebuildRecentChangesTablePass3();
70 $this->rebuildRecentChangesTablePass4();
71 $this->rebuildRecentChangesTablePass5();
72 if ( !( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) ) {
73 $this->purgeFeeds();
74 }
75 $this->output( "Done.\n" );
76 }
77
78 /**
79 * Rebuild pass 1: Insert `recentchanges` entries for page revisions.
80 */
81 private function rebuildRecentChangesTablePass1() {
82 $dbw = $this->getDB( DB_MASTER );
83
84 if ( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) {
85 $this->cutoffFrom = wfTimestamp( TS_UNIX, $this->getOption( 'from' ) );
86 $this->cutoffTo = wfTimestamp( TS_UNIX, $this->getOption( 'to' ) );
87
88 $sec = $this->cutoffTo - $this->cutoffFrom;
89 $days = $sec / 24 / 3600;
90 $this->output( "Rebuilding range of $sec seconds ($days days)\n" );
91 } else {
92 global $wgRCMaxAge;
93
94 $days = $wgRCMaxAge / 24 / 3600;
95 $this->output( "Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
96
97 $this->cutoffFrom = time() - $wgRCMaxAge;
98 $this->cutoffTo = time();
99 }
100
101 $this->output( "Clearing recentchanges table for time range...\n" );
102 $rcids = $dbw->selectFieldValues(
103 'recentchanges',
104 'rc_id',
105 [
106 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
107 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
108 ]
109 );
110 foreach ( array_chunk( $rcids, $this->mBatchSize ) as $rcidBatch ) {
111 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcidBatch ], __METHOD__ );
112 wfGetLBFactory()->waitForReplication();
113 }
114
115 $this->output( "Loading from page and revision tables...\n" );
116 $res = $dbw->select(
117 [ 'page', 'revision' ],
118 [
119 'rev_timestamp',
120 'rev_user',
121 'rev_user_text',
122 'rev_comment',
123 'rev_minor_edit',
124 'rev_id',
125 'rev_deleted',
126 'page_namespace',
127 'page_title',
128 'page_is_new',
129 'page_id'
130 ],
131 [
132 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
133 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
134 'rev_page=page_id'
135 ],
136 __METHOD__,
137 [ 'ORDER BY' => 'rev_timestamp DESC' ]
138 );
139
140 $this->output( "Inserting from page and revision tables...\n" );
141 $inserted = 0;
142 foreach ( $res as $row ) {
143 $dbw->insert(
144 'recentchanges',
145 [
146 'rc_timestamp' => $row->rev_timestamp,
147 'rc_user' => $row->rev_user,
148 'rc_user_text' => $row->rev_user_text,
149 'rc_namespace' => $row->page_namespace,
150 'rc_title' => $row->page_title,
151 'rc_comment' => $row->rev_comment,
152 'rc_minor' => $row->rev_minor_edit,
153 'rc_bot' => 0,
154 'rc_new' => $row->page_is_new,
155 'rc_cur_id' => $row->page_id,
156 'rc_this_oldid' => $row->rev_id,
157 'rc_last_oldid' => 0, // is this ok?
158 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
159 'rc_source' => $row->page_is_new ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT
160 ,
161 'rc_deleted' => $row->rev_deleted
162 ],
163 __METHOD__
164 );
165 if ( ( ++$inserted % $this->mBatchSize ) == 0 ) {
166 wfGetLBFactory()->waitForReplication();
167 }
168 }
169 }
170
171 /**
172 * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
173 * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
174 */
175 private function rebuildRecentChangesTablePass2() {
176 $dbw = $this->getDB( DB_MASTER );
177
178 $this->output( "Updating links and size differences...\n" );
179
180 # Fill in the rc_last_oldid field, which points to the previous edit
181 $res = $dbw->select(
182 'recentchanges',
183 [ 'rc_cur_id', 'rc_this_oldid', 'rc_timestamp' ],
184 [
185 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
186 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
187 ],
188 __METHOD__,
189 [ 'ORDER BY' => 'rc_cur_id,rc_timestamp' ]
190 );
191
192 $lastCurId = 0;
193 $lastOldId = 0;
194 $lastSize = null;
195 $updated = 0;
196 foreach ( $res as $obj ) {
197 $new = 0;
198
199 if ( $obj->rc_cur_id != $lastCurId ) {
200 # Switch! Look up the previous last edit, if any
201 $lastCurId = intval( $obj->rc_cur_id );
202 $emit = $obj->rc_timestamp;
203
204 $row = $dbw->selectRow(
205 'revision',
206 [ 'rev_id', 'rev_len' ],
207 [ 'rev_page' => $lastCurId, "rev_timestamp < " . $dbw->addQuotes( $emit ) ],
208 __METHOD__,
209 [ 'ORDER BY' => 'rev_timestamp DESC' ]
210 );
211 if ( $row ) {
212 $lastOldId = intval( $row->rev_id );
213 # Grab the last text size if available
214 $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : null;
215 } else {
216 # No previous edit
217 $lastOldId = 0;
218 $lastSize = null;
219 $new = 1; // probably true
220 }
221 }
222
223 if ( $lastCurId == 0 ) {
224 $this->output( "Uhhh, something wrong? No curid\n" );
225 } else {
226 # Grab the entry's text size
227 $size = (int)$dbw->selectField(
228 'revision',
229 'rev_len',
230 [ 'rev_id' => $obj->rc_this_oldid ],
231 __METHOD__
232 );
233
234 $dbw->update(
235 'recentchanges',
236 [
237 'rc_last_oldid' => $lastOldId,
238 'rc_new' => $new,
239 'rc_type' => $new ? RC_NEW : RC_EDIT,
240 'rc_source' => $new === 1 ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT,
241 'rc_old_len' => $lastSize,
242 'rc_new_len' => $size,
243 ],
244 [
245 'rc_cur_id' => $lastCurId,
246 'rc_this_oldid' => $obj->rc_this_oldid,
247 'rc_timestamp' => $obj->rc_timestamp // index usage
248 ],
249 __METHOD__
250 );
251
252 $lastOldId = intval( $obj->rc_this_oldid );
253 $lastSize = $size;
254
255 if ( ( ++$updated % $this->mBatchSize ) == 0 ) {
256 wfGetLBFactory()->waitForReplication();
257 }
258 }
259 }
260 }
261
262 /**
263 * Rebuild pass 3: Insert `recentchanges` entries for action logs.
264 */
265 private function rebuildRecentChangesTablePass3() {
266 global $wgLogTypes, $wgLogRestrictions;
267
268 $dbw = $this->getDB( DB_MASTER );
269
270 $this->output( "Loading from user, page, and logging tables...\n" );
271
272 $res = $dbw->select(
273 [ 'user', 'logging', 'page' ],
274 [
275 'log_timestamp',
276 'log_user',
277 'user_name',
278 'log_namespace',
279 'log_title',
280 'log_comment',
281 'page_id',
282 'log_type',
283 'log_action',
284 'log_id',
285 'log_params',
286 'log_deleted'
287 ],
288 [
289 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
290 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
291 'log_user=user_id',
292 // Some logs don't go in RC since they are private.
293 // @FIXME: core/extensions also have spammy logs that don't go in RC.
294 'log_type' => array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) ),
295 ],
296 __METHOD__,
297 [ 'ORDER BY' => 'log_timestamp DESC' ],
298 [
299 'page' =>
300 [ 'LEFT JOIN', [ 'log_namespace=page_namespace', 'log_title=page_title' ] ]
301 ]
302 );
303
304 $field = $dbw->fieldInfo( 'recentchanges', 'rc_cur_id' );
305
306 $inserted = 0;
307 foreach ( $res as $row ) {
308 $dbw->insert(
309 'recentchanges',
310 [
311 'rc_timestamp' => $row->log_timestamp,
312 'rc_user' => $row->log_user,
313 'rc_user_text' => $row->user_name,
314 'rc_namespace' => $row->log_namespace,
315 'rc_title' => $row->log_title,
316 'rc_comment' => $row->log_comment,
317 'rc_minor' => 0,
318 'rc_bot' => 0,
319 'rc_patrolled' => 1,
320 'rc_new' => 0,
321 'rc_this_oldid' => 0,
322 'rc_last_oldid' => 0,
323 'rc_type' => RC_LOG,
324 'rc_source' => RecentChange::SRC_LOG,
325 'rc_cur_id' => $field->isNullable()
326 ? $row->page_id
327 : (int)$row->page_id, // NULL => 0,
328 'rc_log_type' => $row->log_type,
329 'rc_log_action' => $row->log_action,
330 'rc_logid' => $row->log_id,
331 'rc_params' => $row->log_params,
332 'rc_deleted' => $row->log_deleted
333 ],
334 __METHOD__
335 );
336
337 if ( ( ++$inserted % $this->mBatchSize ) == 0 ) {
338 wfGetLBFactory()->waitForReplication();
339 }
340 }
341 }
342
343 /**
344 * Rebuild pass 4: Mark bot and autopatrolled entries.
345 */
346 private function rebuildRecentChangesTablePass4() {
347 global $wgUseRCPatrol, $wgMiserMode;
348
349 $dbw = $this->getDB( DB_MASTER );
350
351 list( $recentchanges, $usergroups, $user ) =
352 $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
353
354 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
355 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
356 # may be lost at this point (aside from joining on the patrol log table entries).
357 $botgroups = [ 'bot' ];
358 $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : [];
359
360 # Flag our recent bot edits
361 if ( $botgroups ) {
362 $botwhere = $dbw->makeList( $botgroups );
363
364 $this->output( "Flagging bot account edits...\n" );
365
366 # Find all users that are bots
367 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
368 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
369 $res = $dbw->query( $sql, __METHOD__ );
370
371 $botusers = [];
372 foreach ( $res as $obj ) {
373 $botusers[] = $obj->user_name;
374 }
375
376 # Fill in the rc_bot field
377 if ( $botusers ) {
378 $rcids = $dbw->selectFieldValues(
379 'recentchanges',
380 'rc_id',
381 [
382 'rc_user_text' => $botusers,
383 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
384 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
385 ],
386 __METHOD__
387 );
388
389 foreach ( array_chunk( $rcids, $this->mBatchSize ) as $rcidBatch ) {
390 $dbw->update(
391 'recentchanges',
392 [ 'rc_bot' => 1 ],
393 [ 'rc_id' => $rcidBatch ],
394 __METHOD__
395 );
396 wfGetLBFactory()->waitForReplication();
397 }
398 }
399 }
400
401 # Flag our recent autopatrolled edits
402 if ( !$wgMiserMode && $autopatrolgroups ) {
403 $patrolwhere = $dbw->makeList( $autopatrolgroups );
404 $patrolusers = [];
405
406 $this->output( "Flagging auto-patrolled edits...\n" );
407
408 # Find all users in RC with autopatrol rights
409 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
410 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
411 $res = $dbw->query( $sql, __METHOD__ );
412
413 foreach ( $res as $obj ) {
414 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
415 }
416
417 # Fill in the rc_patrolled field
418 if ( $patrolusers ) {
419 $patrolwhere = implode( ',', $patrolusers );
420 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
421 "WHERE rc_user_text IN($patrolwhere) " .
422 "AND rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ) . ' ' .
423 "AND rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) );
424 $dbw->query( $sql2 );
425 }
426 }
427 }
428
429 /**
430 * Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log entry
431 * for a single action (upload only, at the moment, but potentially also move, protect, ...).
432 */
433 private function rebuildRecentChangesTablePass5() {
434 $dbw = wfGetDB( DB_MASTER );
435
436 $this->output( "Removing duplicate revision and logging entries...\n" );
437
438 $res = $dbw->select(
439 [ 'logging', 'log_search' ],
440 [ 'ls_value', 'ls_log_id' ],
441 [
442 'ls_log_id = log_id',
443 'ls_field' => 'associated_rev_id',
444 'log_type' => 'upload',
445 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
446 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
447 ],
448 __METHOD__
449 );
450
451 $updates = 0;
452 foreach ( $res as $obj ) {
453 $rev_id = $obj->ls_value;
454 $log_id = $obj->ls_log_id;
455
456 // Mark the logging row as having an associated rev id
457 $dbw->update(
458 'recentchanges',
459 /*SET*/ [ 'rc_this_oldid' => $rev_id ],
460 /*WHERE*/ [ 'rc_logid' => $log_id ],
461 __METHOD__
462 );
463
464 // Delete the revision row
465 $dbw->delete(
466 'recentchanges',
467 /*WHERE*/ [ 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ],
468 __METHOD__
469 );
470
471 if ( ( ++$updates % $this->mBatchSize ) == 0 ) {
472 wfGetLBFactory()->waitForReplication();
473 }
474 }
475 }
476
477 /**
478 * Purge cached feeds in $wanCache
479 */
480 private function purgeFeeds() {
481 global $wgFeedClasses;
482
483 $this->output( "Deleting feed timestamps.\n" );
484
485 $wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
486 foreach ( $wgFeedClasses as $feed => $className ) {
487 $wanCache->delete( $wanCache->makeKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
488 }
489 }
490 }
491
492 $maintClass = "RebuildRecentchanges";
493 require_once RUN_MAINTENANCE_IF_MAIN;