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';
27 use MediaWiki\MediaWikiServices
;
30 * Maintenance script that rebuilds recent changes from scratch.
32 * @ingroup Maintenance
34 class RebuildRecentchanges
extends Maintenance
{
35 /** @var int UNIX timestamp */
37 /** @var int UNIX timestamp */
40 public function __construct() {
41 parent
::__construct();
42 $this->addDescription( 'Rebuild recent changes' );
46 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
52 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
56 $this->setBatchSize( 200 );
59 public function execute() {
61 ( $this->hasOption( 'from' ) && !$this->hasOption( 'to' ) ) ||
62 ( !$this->hasOption( 'from' ) && $this->hasOption( 'to' ) )
64 $this->fatalError( "Both 'from' and 'to' must be given, or neither" );
67 $this->rebuildRecentChangesTablePass1();
68 $this->rebuildRecentChangesTablePass2();
69 $this->rebuildRecentChangesTablePass3();
70 $this->rebuildRecentChangesTablePass4();
71 $this->rebuildRecentChangesTablePass5();
72 if ( !( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) ) {
75 $this->output( "Done.\n" );
79 * Rebuild pass 1: Insert `recentchanges` entries for page revisions.
81 private function rebuildRecentChangesTablePass1() {
82 $dbw = $this->getDB( DB_MASTER
);
83 $revCommentStore = new CommentStore( 'rev_comment' );
84 $rcCommentStore = new CommentStore( 'rc_comment' );
86 if ( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) {
87 $this->cutoffFrom
= wfTimestamp( TS_UNIX
, $this->getOption( 'from' ) );
88 $this->cutoffTo
= wfTimestamp( TS_UNIX
, $this->getOption( 'to' ) );
90 $sec = $this->cutoffTo
- $this->cutoffFrom
;
91 $days = $sec / 24 / 3600;
92 $this->output( "Rebuilding range of $sec seconds ($days days)\n" );
96 $days = $wgRCMaxAge / 24 / 3600;
97 $this->output( "Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
99 $this->cutoffFrom
= time() - $wgRCMaxAge;
100 $this->cutoffTo
= time();
103 $this->output( "Clearing recentchanges table for time range...\n" );
104 $rcids = $dbw->selectFieldValues(
108 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
109 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
112 foreach ( array_chunk( $rcids, $this->getBatchSize() ) as $rcidBatch ) {
113 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcidBatch ], __METHOD__
);
114 wfGetLBFactory()->waitForReplication();
117 $this->output( "Loading from page and revision tables...\n" );
119 $commentQuery = $revCommentStore->getJoin();
121 [ 'revision', 'page' ] +
$commentQuery['tables'],
133 ] +
$commentQuery['fields'],
135 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
136 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
139 [ 'ORDER BY' => 'rev_timestamp DESC' ],
141 'page' => [ 'JOIN', 'rev_page=page_id' ],
142 ] +
$commentQuery['joins']
145 $this->output( "Inserting from page and revision tables...\n" );
147 foreach ( $res as $row ) {
148 $comment = $revCommentStore->getComment( $row );
152 'rc_timestamp' => $row->rev_timestamp
,
153 'rc_user' => $row->rev_user
,
154 'rc_user_text' => $row->rev_user_text
,
155 'rc_namespace' => $row->page_namespace
,
156 'rc_title' => $row->page_title
,
157 'rc_minor' => $row->rev_minor_edit
,
159 'rc_new' => $row->page_is_new
,
160 'rc_cur_id' => $row->page_id
,
161 'rc_this_oldid' => $row->rev_id
,
162 'rc_last_oldid' => 0, // is this ok?
163 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
164 'rc_source' => $row->page_is_new ? RecentChange
::SRC_NEW
: RecentChange
::SRC_EDIT
,
165 'rc_deleted' => $row->rev_deleted
166 ] +
$rcCommentStore->insert( $dbw, $comment ),
169 if ( ( ++
$inserted %
$this->getBatchSize() ) == 0 ) {
170 wfGetLBFactory()->waitForReplication();
176 * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
177 * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
179 private function rebuildRecentChangesTablePass2() {
180 $dbw = $this->getDB( DB_MASTER
);
182 $this->output( "Updating links and size differences...\n" );
184 # Fill in the rc_last_oldid field, which points to the previous edit
187 [ 'rc_cur_id', 'rc_this_oldid', 'rc_timestamp' ],
189 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
190 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
193 [ 'ORDER BY' => 'rc_cur_id,rc_timestamp' ]
200 foreach ( $res as $obj ) {
203 if ( $obj->rc_cur_id
!= $lastCurId ) {
204 # Switch! Look up the previous last edit, if any
205 $lastCurId = intval( $obj->rc_cur_id
);
206 $emit = $obj->rc_timestamp
;
208 $row = $dbw->selectRow(
210 [ 'rev_id', 'rev_len' ],
211 [ 'rev_page' => $lastCurId, "rev_timestamp < " . $dbw->addQuotes( $emit ) ],
213 [ 'ORDER BY' => 'rev_timestamp DESC' ]
216 $lastOldId = intval( $row->rev_id
);
217 # Grab the last text size if available
218 $lastSize = !is_null( $row->rev_len
) ?
intval( $row->rev_len
) : null;
223 $new = 1; // probably true
227 if ( $lastCurId == 0 ) {
228 $this->output( "Uhhh, something wrong? No curid\n" );
230 # Grab the entry's text size
231 $size = (int)$dbw->selectField(
234 [ 'rev_id' => $obj->rc_this_oldid
],
241 'rc_last_oldid' => $lastOldId,
243 'rc_type' => $new ? RC_NEW
: RC_EDIT
,
244 'rc_source' => $new === 1 ? RecentChange
::SRC_NEW
: RecentChange
::SRC_EDIT
,
245 'rc_old_len' => $lastSize,
246 'rc_new_len' => $size,
249 'rc_cur_id' => $lastCurId,
250 'rc_this_oldid' => $obj->rc_this_oldid
,
251 'rc_timestamp' => $obj->rc_timestamp
// index usage
256 $lastOldId = intval( $obj->rc_this_oldid
);
259 if ( ( ++
$updated %
$this->getBatchSize() ) == 0 ) {
260 wfGetLBFactory()->waitForReplication();
267 * Rebuild pass 3: Insert `recentchanges` entries for action logs.
269 private function rebuildRecentChangesTablePass3() {
270 global $wgLogTypes, $wgLogRestrictions;
272 $dbw = $this->getDB( DB_MASTER
);
273 $logCommentStore = new CommentStore( 'log_comment' );
274 $rcCommentStore = new CommentStore( 'rc_comment' );
276 $this->output( "Loading from user, page, and logging tables...\n" );
278 $commentQuery = $logCommentStore->getJoin();
280 [ 'user', 'logging', 'page' ] +
$commentQuery['tables'],
293 ] +
$commentQuery['fields'],
295 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
296 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
298 // Some logs don't go in RC since they are private.
299 // @FIXME: core/extensions also have spammy logs that don't go in RC.
300 'log_type' => array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) ),
303 [ 'ORDER BY' => 'log_timestamp DESC' ],
306 [ 'LEFT JOIN', [ 'log_namespace=page_namespace', 'log_title=page_title' ] ]
307 ] +
$commentQuery['joins']
310 $field = $dbw->fieldInfo( 'recentchanges', 'rc_cur_id' );
313 foreach ( $res as $row ) {
314 $comment = $logCommentStore->getComment( $row );
318 'rc_timestamp' => $row->log_timestamp
,
319 'rc_user' => $row->log_user
,
320 'rc_user_text' => $row->user_name
,
321 'rc_namespace' => $row->log_namespace
,
322 'rc_title' => $row->log_title
,
327 'rc_this_oldid' => 0,
328 'rc_last_oldid' => 0,
330 'rc_source' => RecentChange
::SRC_LOG
,
331 'rc_cur_id' => $field->isNullable()
333 : (int)$row->page_id
, // NULL => 0,
334 'rc_log_type' => $row->log_type
,
335 'rc_log_action' => $row->log_action
,
336 'rc_logid' => $row->log_id
,
337 'rc_params' => $row->log_params
,
338 'rc_deleted' => $row->log_deleted
339 ] +
$rcCommentStore->insert( $dbw, $comment ),
343 if ( ( ++
$inserted %
$this->getBatchSize() ) == 0 ) {
344 wfGetLBFactory()->waitForReplication();
350 * Rebuild pass 4: Mark bot and autopatrolled entries.
352 private function rebuildRecentChangesTablePass4() {
353 global $wgUseRCPatrol, $wgMiserMode;
355 $dbw = $this->getDB( DB_MASTER
);
357 list( $recentchanges, $usergroups, $user ) =
358 $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
360 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
361 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
362 # may be lost at this point (aside from joining on the patrol log table entries).
363 $botgroups = [ 'bot' ];
364 $autopatrolgroups = $wgUseRCPatrol ? User
::getGroupsWithPermission( 'autopatrol' ) : [];
366 # Flag our recent bot edits
368 $botwhere = $dbw->makeList( $botgroups );
370 $this->output( "Flagging bot account edits...\n" );
372 # Find all users that are bots
373 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
374 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
375 $res = $dbw->query( $sql, __METHOD__
);
378 foreach ( $res as $obj ) {
379 $botusers[] = $obj->user_name
;
382 # Fill in the rc_bot field
384 $rcids = $dbw->selectFieldValues(
388 'rc_user_text' => $botusers,
389 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
390 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
395 foreach ( array_chunk( $rcids, $this->getBatchSize() ) as $rcidBatch ) {
399 [ 'rc_id' => $rcidBatch ],
402 wfGetLBFactory()->waitForReplication();
407 # Flag our recent autopatrolled edits
408 if ( !$wgMiserMode && $autopatrolgroups ) {
409 $patrolwhere = $dbw->makeList( $autopatrolgroups );
412 $this->output( "Flagging auto-patrolled edits...\n" );
414 # Find all users in RC with autopatrol rights
415 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
416 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
417 $res = $dbw->query( $sql, __METHOD__
);
419 foreach ( $res as $obj ) {
420 $patrolusers[] = $dbw->addQuotes( $obj->user_name
);
423 # Fill in the rc_patrolled field
424 if ( $patrolusers ) {
425 $patrolwhere = implode( ',', $patrolusers );
426 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
427 "WHERE rc_user_text IN($patrolwhere) " .
428 "AND rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ) . ' ' .
429 "AND rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) );
430 $dbw->query( $sql2 );
436 * Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log entry
437 * for a single action (upload only, at the moment, but potentially also move, protect, ...).
439 private function rebuildRecentChangesTablePass5() {
440 $dbw = wfGetDB( DB_MASTER
);
442 $this->output( "Removing duplicate revision and logging entries...\n" );
445 [ 'logging', 'log_search' ],
446 [ 'ls_value', 'ls_log_id' ],
448 'ls_log_id = log_id',
449 'ls_field' => 'associated_rev_id',
450 'log_type' => 'upload',
451 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
452 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
458 foreach ( $res as $obj ) {
459 $rev_id = $obj->ls_value
;
460 $log_id = $obj->ls_log_id
;
462 // Mark the logging row as having an associated rev id
465 /*SET*/ [ 'rc_this_oldid' => $rev_id ],
466 /*WHERE*/ [ 'rc_logid' => $log_id ],
470 // Delete the revision row
473 /*WHERE*/ [ 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ],
477 if ( ( ++
$updates %
$this->getBatchSize() ) == 0 ) {
478 wfGetLBFactory()->waitForReplication();
484 * Purge cached feeds in $wanCache
486 private function purgeFeeds() {
487 global $wgFeedClasses;
489 $this->output( "Deleting feed timestamps.\n" );
491 $wanCache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
492 foreach ( $wgFeedClasses as $feed => $className ) {
493 $wanCache->delete( $wanCache->makeKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
498 $maintClass = RebuildRecentchanges
::class;
499 require_once RUN_MAINTENANCE_IF_MAIN
;