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 * @param ILBFactory $lbFactory
86 private function rebuildRecentChangesTablePass1( ILBFactory
$lbFactory ) {
87 $dbw = $this->getDB( DB_MASTER
);
88 $commentStore = CommentStore
::getStore();
90 if ( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) {
91 $this->cutoffFrom
= wfTimestamp( TS_UNIX
, $this->getOption( 'from' ) );
92 $this->cutoffTo
= wfTimestamp( TS_UNIX
, $this->getOption( 'to' ) );
94 $sec = $this->cutoffTo
- $this->cutoffFrom
;
95 $days = $sec / 24 / 3600;
96 $this->output( "Rebuilding range of $sec seconds ($days days)\n" );
100 $days = $wgRCMaxAge / 24 / 3600;
101 $this->output( "Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
103 $this->cutoffFrom
= time() - $wgRCMaxAge;
104 $this->cutoffTo
= time();
107 $this->output( "Clearing recentchanges table for time range...\n" );
108 $rcids = $dbw->selectFieldValues(
112 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
113 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
116 foreach ( array_chunk( $rcids, $this->getBatchSize() ) as $rcidBatch ) {
117 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcidBatch ], __METHOD__
);
118 $lbFactory->waitForReplication();
121 $this->output( "Loading from page and revision tables...\n" );
123 $commentQuery = $commentStore->getJoin( 'rev_comment' );
124 $actorQuery = ActorMigration
::newMigration()->getJoin( 'rev_user' );
126 [ 'revision', 'page' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
136 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
138 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
139 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
142 [ 'ORDER BY' => 'rev_timestamp DESC' ],
144 'page' => [ 'JOIN', 'rev_page=page_id' ],
145 ] +
$commentQuery['joins'] +
$actorQuery['joins']
148 $this->output( "Inserting from page and revision tables...\n" );
150 $actorMigration = ActorMigration
::newMigration();
151 foreach ( $res as $row ) {
152 $comment = $commentStore->getComment( 'rev_comment', $row );
153 $user = User
::newFromAnyId( $row->rev_user
, $row->rev_user_text
, $row->rev_actor
);
157 'rc_timestamp' => $row->rev_timestamp
,
158 'rc_namespace' => $row->page_namespace
,
159 'rc_title' => $row->page_title
,
160 'rc_minor' => $row->rev_minor_edit
,
162 'rc_new' => $row->page_is_new
,
163 'rc_cur_id' => $row->page_id
,
164 'rc_this_oldid' => $row->rev_id
,
165 'rc_last_oldid' => 0, // is this ok?
166 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
167 'rc_source' => $row->page_is_new ? RecentChange
::SRC_NEW
: RecentChange
::SRC_EDIT
,
168 'rc_deleted' => $row->rev_deleted
169 ] +
$commentStore->insert( $dbw, 'rc_comment', $comment )
170 +
$actorMigration->getInsertValues( $dbw, 'rc_user', $user ),
173 if ( ( ++
$inserted %
$this->getBatchSize() ) == 0 ) {
174 $lbFactory->waitForReplication();
180 * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
181 * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
183 * @param ILBFactory $lbFactory
185 private function rebuildRecentChangesTablePass2( ILBFactory
$lbFactory ) {
186 $dbw = $this->getDB( DB_MASTER
);
188 $this->output( "Updating links and size differences...\n" );
190 # Fill in the rc_last_oldid field, which points to the previous edit
193 [ 'rc_cur_id', 'rc_this_oldid', 'rc_timestamp' ],
195 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
196 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
199 [ 'ORDER BY' => 'rc_cur_id,rc_timestamp' ]
206 foreach ( $res as $row ) {
209 if ( $row->rc_cur_id
!= $lastCurId ) {
210 # Switch! Look up the previous last edit, if any
211 $lastCurId = intval( $row->rc_cur_id
);
212 $emit = $row->rc_timestamp
;
214 $revRow = $dbw->selectRow(
216 [ 'rev_id', 'rev_len' ],
217 [ 'rev_page' => $lastCurId, "rev_timestamp < " . $dbw->addQuotes( $emit ) ],
219 [ 'ORDER BY' => 'rev_timestamp DESC' ]
222 $lastOldId = intval( $revRow->rev_id
);
223 # Grab the last text size if available
224 $lastSize = !is_null( $revRow->rev_len
) ?
intval( $revRow->rev_len
) : null;
229 $new = 1; // probably true
233 if ( $lastCurId == 0 ) {
234 $this->output( "Uhhh, something wrong? No curid\n" );
236 # Grab the entry's text size
237 $size = (int)$dbw->selectField(
240 [ 'rev_id' => $row->rc_this_oldid
],
247 'rc_last_oldid' => $lastOldId,
249 'rc_type' => $new ? RC_NEW
: RC_EDIT
,
250 'rc_source' => $new === 1 ? RecentChange
::SRC_NEW
: RecentChange
::SRC_EDIT
,
251 'rc_old_len' => $lastSize,
252 'rc_new_len' => $size,
255 'rc_cur_id' => $lastCurId,
256 'rc_this_oldid' => $row->rc_this_oldid
,
257 'rc_timestamp' => $row->rc_timestamp
// index usage
262 $lastOldId = intval( $row->rc_this_oldid
);
265 if ( ( ++
$updated %
$this->getBatchSize() ) == 0 ) {
266 $lbFactory->waitForReplication();
273 * Rebuild pass 3: Insert `recentchanges` entries for action logs.
275 * @param ILBFactory $lbFactory
277 private function rebuildRecentChangesTablePass3( ILBFactory
$lbFactory ) {
278 global $wgLogRestrictions, $wgFilterLogTypes;
280 $dbw = $this->getDB( DB_MASTER
);
281 $commentStore = CommentStore
::getStore();
282 $nonRCLogs = array_merge( array_keys( $wgLogRestrictions ),
283 array_keys( $wgFilterLogTypes ),
286 $this->output( "Loading from user and logging tables...\n" );
288 $commentQuery = $commentStore->getJoin( 'log_comment' );
289 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
291 [ 'logging' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
302 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
304 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
305 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
306 // Some logs don't go in RC since they are private, or are included in the filterable log types.
307 'log_type' => array_diff( LogPage
::validTypes(), $nonRCLogs ),
310 [ 'ORDER BY' => 'log_timestamp DESC' ],
311 $commentQuery['joins'] +
$actorQuery['joins']
314 $field = $dbw->fieldInfo( 'recentchanges', 'rc_cur_id' );
317 $actorMigration = ActorMigration
::newMigration();
318 foreach ( $res as $row ) {
319 $comment = $commentStore->getComment( 'log_comment', $row );
320 $user = User
::newFromAnyId( $row->log_user
, $row->log_user_text
, $row->log_actor
);
324 'rc_timestamp' => $row->log_timestamp
,
325 'rc_namespace' => $row->log_namespace
,
326 'rc_title' => $row->log_title
,
331 'rc_this_oldid' => 0,
332 'rc_last_oldid' => 0,
334 'rc_source' => RecentChange
::SRC_LOG
,
335 'rc_cur_id' => $field->isNullable()
337 : (int)$row->log_page
, // NULL => 0,
338 'rc_log_type' => $row->log_type
,
339 'rc_log_action' => $row->log_action
,
340 'rc_logid' => $row->log_id
,
341 'rc_params' => $row->log_params
,
342 'rc_deleted' => $row->log_deleted
343 ] +
$commentStore->insert( $dbw, 'rc_comment', $comment )
344 +
$actorMigration->getInsertValues( $dbw, 'rc_user', $user ),
348 if ( ( ++
$inserted %
$this->getBatchSize() ) == 0 ) {
349 $lbFactory->waitForReplication();
355 * Rebuild pass 4: Mark bot and autopatrolled entries.
357 * @param ILBFactory $lbFactory
359 private function rebuildRecentChangesTablePass4( ILBFactory
$lbFactory ) {
360 global $wgUseRCPatrol, $wgMiserMode;
362 $dbw = $this->getDB( DB_MASTER
);
364 $userQuery = User
::getQueryInfo();
366 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
367 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
368 # may be lost at this point (aside from joining on the patrol log table entries).
369 $botgroups = [ 'bot' ];
370 $autopatrolgroups = $wgUseRCPatrol ? User
::getGroupsWithPermission( 'autopatrol' ) : [];
372 # Flag our recent bot edits
374 $this->output( "Flagging bot account edits...\n" );
376 # Find all users that are bots
378 array_merge( [ 'user_groups' ], $userQuery['tables'] ),
379 $userQuery['fields'],
380 [ 'ug_group' => $botgroups ],
383 [ 'user_groups' => [ 'JOIN', 'user_id = ug_user' ] ] +
$userQuery['joins']
387 foreach ( $res as $row ) {
388 $botusers[] = User
::newFromRow( $row );
391 # Fill in the rc_bot field
393 $actorQuery = ActorMigration
::newMigration()->getWhere( $dbw, 'rc_user', $botusers, false );
395 foreach ( $actorQuery['orconds'] as $cond ) {
396 $rcids = array_merge( $rcids, $dbw->selectFieldValues(
397 [ 'recentchanges' ] +
$actorQuery['tables'],
400 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
401 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
409 $rcids = array_values( array_unique( $rcids ) );
411 foreach ( array_chunk( $rcids, $this->getBatchSize() ) as $rcidBatch ) {
415 [ 'rc_id' => $rcidBatch ],
418 $lbFactory->waitForReplication();
423 # Flag our recent autopatrolled edits
424 if ( !$wgMiserMode && $autopatrolgroups ) {
427 $this->output( "Flagging auto-patrolled edits...\n" );
429 # Find all users in RC with autopatrol rights
431 array_merge( [ 'user_groups' ], $userQuery['tables'] ),
432 $userQuery['fields'],
433 [ 'ug_group' => $autopatrolgroups ],
436 [ 'user_groups' => [ 'JOIN', 'user_id = ug_user' ] ] +
$userQuery['joins']
439 foreach ( $res as $row ) {
440 $patrolusers[] = User
::newFromRow( $row );
443 # Fill in the rc_patrolled field
444 if ( $patrolusers ) {
445 $actorQuery = ActorMigration
::newMigration()->getWhere( $dbw, 'rc_user', $patrolusers, false );
446 foreach ( $actorQuery['orconds'] as $cond ) {
449 [ 'rc_patrolled' => 1 ],
452 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
453 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
457 $lbFactory->waitForReplication();
464 * Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log
465 * entry for a single action (upload only, at the moment, but potentially move, protect, ...).
467 * @param ILBFactory $lbFactory
469 private function rebuildRecentChangesTablePass5( ILBFactory
$lbFactory ) {
470 $dbw = wfGetDB( DB_MASTER
);
472 $this->output( "Removing duplicate revision and logging entries...\n" );
475 [ 'logging', 'log_search' ],
476 [ 'ls_value', 'ls_log_id' ],
478 'ls_log_id = log_id',
479 'ls_field' => 'associated_rev_id',
480 'log_type' => 'upload',
481 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
482 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
488 foreach ( $res as $row ) {
489 $rev_id = $row->ls_value
;
490 $log_id = $row->ls_log_id
;
492 // Mark the logging row as having an associated rev id
495 /*SET*/ [ 'rc_this_oldid' => $rev_id ],
496 /*WHERE*/ [ 'rc_logid' => $log_id ],
500 // Delete the revision row
503 /*WHERE*/ [ 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ],
507 if ( ( ++
$updates %
$this->getBatchSize() ) == 0 ) {
508 $lbFactory->waitForReplication();
514 * Purge cached feeds in $wanCache
516 private function purgeFeeds() {
517 global $wgFeedClasses;
519 $this->output( "Deleting feed timestamps.\n" );
521 $wanCache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
522 foreach ( $wgFeedClasses as $feed => $className ) {
523 $wanCache->delete( $wanCache->makeKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
528 $maintClass = RebuildRecentchanges
::class;
529 require_once RUN_MAINTENANCE_IF_MAIN
;