3 * Rebuild recent changes from scratch. This takes several hours,
4 * depending on the database size and server configuration.
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.
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.
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
22 * @ingroup Maintenance
26 require_once __DIR__
. '/Maintenance.php';
28 use MediaWiki\MediaWikiServices
;
29 use Wikimedia\Rdbms\ILBFactory
;
32 * Maintenance script that rebuilds recent changes from scratch.
34 * @ingroup Maintenance
36 class RebuildRecentchanges
extends Maintenance
{
37 /** @var int UNIX timestamp */
39 /** @var int UNIX timestamp */
42 public function __construct() {
43 parent
::__construct();
44 $this->addDescription( 'Rebuild recent changes' );
48 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
54 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
58 $this->setBatchSize( 200 );
61 public function execute() {
63 ( $this->hasOption( 'from' ) && !$this->hasOption( 'to' ) ) ||
64 ( !$this->hasOption( 'from' ) && $this->hasOption( 'to' ) )
66 $this->fatalError( "Both 'from' and 'to' must be given, or neither" );
69 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
70 $this->rebuildRecentChangesTablePass1( $lbFactory );
71 $this->rebuildRecentChangesTablePass2( $lbFactory );
72 $this->rebuildRecentChangesTablePass3( $lbFactory );
73 $this->rebuildRecentChangesTablePass4( $lbFactory );
74 $this->rebuildRecentChangesTablePass5( $lbFactory );
75 if ( !( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) ) {
78 $this->output( "Done.\n" );
82 * Rebuild pass 1: Insert `recentchanges` entries for page revisions.
84 private function rebuildRecentChangesTablePass1( ILBFactory
$lbFactory ) {
85 $dbw = $this->getDB( DB_MASTER
);
86 $commentStore = CommentStore
::getStore();
88 if ( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) {
89 $this->cutoffFrom
= wfTimestamp( TS_UNIX
, $this->getOption( 'from' ) );
90 $this->cutoffTo
= wfTimestamp( TS_UNIX
, $this->getOption( 'to' ) );
92 $sec = $this->cutoffTo
- $this->cutoffFrom
;
93 $days = $sec / 24 / 3600;
94 $this->output( "Rebuilding range of $sec seconds ($days days)\n" );
98 $days = $wgRCMaxAge / 24 / 3600;
99 $this->output( "Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
101 $this->cutoffFrom
= time() - $wgRCMaxAge;
102 $this->cutoffTo
= time();
105 $this->output( "Clearing recentchanges table for time range...\n" );
106 $rcids = $dbw->selectFieldValues(
110 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
111 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
114 foreach ( array_chunk( $rcids, $this->getBatchSize() ) as $rcidBatch ) {
115 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcidBatch ], __METHOD__
);
116 $lbFactory->waitForReplication();
119 $this->output( "Loading from page and revision tables...\n" );
121 $commentQuery = $commentStore->getJoin( 'rev_comment' );
122 $actorQuery = ActorMigration
::newMigration()->getJoin( 'rev_user' );
124 [ 'revision', 'page' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
134 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
136 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
137 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
140 [ 'ORDER BY' => 'rev_timestamp DESC' ],
142 'page' => [ 'JOIN', 'rev_page=page_id' ],
143 ] +
$commentQuery['joins'] +
$actorQuery['joins']
146 $this->output( "Inserting from page and revision tables...\n" );
148 $actorMigration = ActorMigration
::newMigration();
149 foreach ( $res as $row ) {
150 $comment = $commentStore->getComment( 'rev_comment', $row );
151 $user = User
::newFromAnyId( $row->rev_user
, $row->rev_user_text
, $row->rev_actor
);
155 'rc_timestamp' => $row->rev_timestamp
,
156 'rc_namespace' => $row->page_namespace
,
157 'rc_title' => $row->page_title
,
158 'rc_minor' => $row->rev_minor_edit
,
160 'rc_new' => $row->page_is_new
,
161 'rc_cur_id' => $row->page_id
,
162 'rc_this_oldid' => $row->rev_id
,
163 'rc_last_oldid' => 0, // is this ok?
164 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
165 'rc_source' => $row->page_is_new ? RecentChange
::SRC_NEW
: RecentChange
::SRC_EDIT
,
166 'rc_deleted' => $row->rev_deleted
167 ] +
$commentStore->insert( $dbw, 'rc_comment', $comment )
168 +
$actorMigration->getInsertValues( $dbw, 'rc_user', $user ),
171 if ( ( ++
$inserted %
$this->getBatchSize() ) == 0 ) {
172 $lbFactory->waitForReplication();
178 * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
179 * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
181 private function rebuildRecentChangesTablePass2( ILBFactory
$lbFactory ) {
182 $dbw = $this->getDB( DB_MASTER
);
184 $this->output( "Updating links and size differences...\n" );
186 # Fill in the rc_last_oldid field, which points to the previous edit
189 [ 'rc_cur_id', 'rc_this_oldid', 'rc_timestamp' ],
191 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
192 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
195 [ 'ORDER BY' => 'rc_cur_id,rc_timestamp' ]
202 foreach ( $res as $obj ) {
205 if ( $obj->rc_cur_id
!= $lastCurId ) {
206 # Switch! Look up the previous last edit, if any
207 $lastCurId = intval( $obj->rc_cur_id
);
208 $emit = $obj->rc_timestamp
;
210 $row = $dbw->selectRow(
212 [ 'rev_id', 'rev_len' ],
213 [ 'rev_page' => $lastCurId, "rev_timestamp < " . $dbw->addQuotes( $emit ) ],
215 [ 'ORDER BY' => 'rev_timestamp DESC' ]
218 $lastOldId = intval( $row->rev_id
);
219 # Grab the last text size if available
220 $lastSize = !is_null( $row->rev_len
) ?
intval( $row->rev_len
) : null;
225 $new = 1; // probably true
229 if ( $lastCurId == 0 ) {
230 $this->output( "Uhhh, something wrong? No curid\n" );
232 # Grab the entry's text size
233 $size = (int)$dbw->selectField(
236 [ 'rev_id' => $obj->rc_this_oldid
],
243 'rc_last_oldid' => $lastOldId,
245 'rc_type' => $new ? RC_NEW
: RC_EDIT
,
246 'rc_source' => $new === 1 ? RecentChange
::SRC_NEW
: RecentChange
::SRC_EDIT
,
247 'rc_old_len' => $lastSize,
248 'rc_new_len' => $size,
251 'rc_cur_id' => $lastCurId,
252 'rc_this_oldid' => $obj->rc_this_oldid
,
253 'rc_timestamp' => $obj->rc_timestamp
// index usage
258 $lastOldId = intval( $obj->rc_this_oldid
);
261 if ( ( ++
$updated %
$this->getBatchSize() ) == 0 ) {
262 $lbFactory->waitForReplication();
269 * Rebuild pass 3: Insert `recentchanges` entries for action logs.
271 private function rebuildRecentChangesTablePass3( ILBFactory
$lbFactory ) {
272 global $wgLogTypes, $wgLogRestrictions;
274 $dbw = $this->getDB( DB_MASTER
);
275 $commentStore = CommentStore
::getStore();
277 $this->output( "Loading from user, page, and logging tables...\n" );
279 $commentQuery = $commentStore->getJoin( 'log_comment' );
280 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
282 [ 'logging', 'page' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
293 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
295 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
296 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
297 // Some logs don't go in RC since they are private.
298 // @FIXME: core/extensions also have spammy logs that don't go in RC.
299 'log_type' => array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) ),
302 [ 'ORDER BY' => 'log_timestamp DESC' ],
305 [ 'LEFT JOIN', [ 'log_namespace=page_namespace', 'log_title=page_title' ] ]
306 ] +
$commentQuery['joins'] +
$actorQuery['joins']
309 $field = $dbw->fieldInfo( 'recentchanges', 'rc_cur_id' );
312 $actorMigration = ActorMigration
::newMigration();
313 foreach ( $res as $row ) {
314 $comment = $commentStore->getComment( 'log_comment', $row );
315 $user = User
::newFromAnyId( $row->log_user
, $row->log_user_text
, $row->log_actor
);
319 'rc_timestamp' => $row->log_timestamp
,
320 'rc_namespace' => $row->log_namespace
,
321 'rc_title' => $row->log_title
,
326 'rc_this_oldid' => 0,
327 'rc_last_oldid' => 0,
329 'rc_source' => RecentChange
::SRC_LOG
,
330 'rc_cur_id' => $field->isNullable()
332 : (int)$row->page_id
, // NULL => 0,
333 'rc_log_type' => $row->log_type
,
334 'rc_log_action' => $row->log_action
,
335 'rc_logid' => $row->log_id
,
336 'rc_params' => $row->log_params
,
337 'rc_deleted' => $row->log_deleted
338 ] +
$commentStore->insert( $dbw, 'rc_comment', $comment )
339 +
$actorMigration->getInsertValues( $dbw, 'rc_user', $user ),
343 if ( ( ++
$inserted %
$this->getBatchSize() ) == 0 ) {
344 $lbFactory->waitForReplication();
350 * Rebuild pass 4: Mark bot and autopatrolled entries.
352 private function rebuildRecentChangesTablePass4( ILBFactory
$lbFactory ) {
353 global $wgUseRCPatrol, $wgMiserMode;
355 $dbw = $this->getDB( DB_MASTER
);
357 $userQuery = User
::getQueryInfo();
359 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
360 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
361 # may be lost at this point (aside from joining on the patrol log table entries).
362 $botgroups = [ 'bot' ];
363 $autopatrolgroups = $wgUseRCPatrol ? User
::getGroupsWithPermission( 'autopatrol' ) : [];
365 # Flag our recent bot edits
367 $this->output( "Flagging bot account edits...\n" );
369 # Find all users that are bots
371 array_merge( [ 'user_groups' ], $userQuery['tables'] ),
372 $userQuery['fields'],
373 [ 'ug_group' => $botgroups ],
376 [ 'user_group' => [ 'JOIN', 'user_id = ug_user' ] ] +
$userQuery['joins']
380 foreach ( $res as $obj ) {
381 $botusers[] = User
::newFromRow( $obj );
384 # Fill in the rc_bot field
386 $actorQuery = ActorMigration
::newMigration()->getWhere( $dbw, 'rc_user', $botusers, false );
388 foreach ( $actorQuery['orconds'] as $cond ) {
389 $rcids = array_merge( $rcids, $dbw->selectFieldValues(
390 [ 'recentchanges' ] +
$actorQuery['tables'],
393 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
394 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
402 $rcids = array_values( array_unique( $rcids ) );
404 foreach ( array_chunk( $rcids, $this->getBatchSize() ) as $rcidBatch ) {
408 [ 'rc_id' => $rcidBatch ],
411 $lbFactory->waitForReplication();
416 # Flag our recent autopatrolled edits
417 if ( !$wgMiserMode && $autopatrolgroups ) {
420 $this->output( "Flagging auto-patrolled edits...\n" );
422 # Find all users in RC with autopatrol rights
424 array_merge( [ 'user_groups' ], $userQuery['tables'] ),
425 $userQuery['fields'],
426 [ 'ug_group' => $autopatrolgroups ],
429 [ 'user_group' => [ 'JOIN', 'user_id = ug_user' ] ] +
$userQuery['joins']
432 foreach ( $res as $obj ) {
433 $patrolusers[] = User
::newFromRow( $obj );
436 # Fill in the rc_patrolled field
437 if ( $patrolusers ) {
438 $actorQuery = ActorMigration
::newMigration()->getWhere( $dbw, 'rc_user', $patrolusers, false );
439 foreach ( $actorQuery['orconds'] as $cond ) {
442 [ 'rc_patrolled' => 1 ],
445 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
446 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
450 $lbFactory->waitForReplication();
457 * Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log entry
458 * for a single action (upload only, at the moment, but potentially also move, protect, ...).
460 private function rebuildRecentChangesTablePass5( ILBFactory
$lbFactory ) {
461 $dbw = wfGetDB( DB_MASTER
);
463 $this->output( "Removing duplicate revision and logging entries...\n" );
466 [ 'logging', 'log_search' ],
467 [ 'ls_value', 'ls_log_id' ],
469 'ls_log_id = log_id',
470 'ls_field' => 'associated_rev_id',
471 'log_type' => 'upload',
472 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
473 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
479 foreach ( $res as $obj ) {
480 $rev_id = $obj->ls_value
;
481 $log_id = $obj->ls_log_id
;
483 // Mark the logging row as having an associated rev id
486 /*SET*/ [ 'rc_this_oldid' => $rev_id ],
487 /*WHERE*/ [ 'rc_logid' => $log_id ],
491 // Delete the revision row
494 /*WHERE*/ [ 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ],
498 if ( ( ++
$updates %
$this->getBatchSize() ) == 0 ) {
499 $lbFactory->waitForReplication();
505 * Purge cached feeds in $wanCache
507 private function purgeFeeds() {
508 global $wgFeedClasses;
510 $this->output( "Deleting feed timestamps.\n" );
512 $wanCache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
513 foreach ( $wgFeedClasses as $feed => $className ) {
514 $wanCache->delete( $wanCache->makeKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
519 $maintClass = RebuildRecentchanges
::class;
520 require_once RUN_MAINTENANCE_IF_MAIN
;