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 integer UNIX timestamp */
37 /** @var integer 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->error( "Both 'from' and 'to' must be given, or neither", 1 );
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
);
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' ) );
88 $sec = $this->cutoffTo
- $this->cutoffFrom
;
89 $days = $sec / 24 / 3600;
90 $this->output( "Rebuilding range of $sec seconds ($days days)\n" );
94 $days = $wgRCMaxAge / 24 / 3600;
95 $this->output( "Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
97 $this->cutoffFrom
= time() - $wgRCMaxAge;
98 $this->cutoffTo
= time();
101 $this->output( "Clearing recentchanges table for time range...\n" );
102 $rcids = $dbw->selectFieldValues(
106 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
107 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
110 foreach ( array_chunk( $rcids, $this->mBatchSize
) as $rcidBatch ) {
111 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcidBatch ], __METHOD__
);
112 wfGetLBFactory()->waitForReplication();
115 $this->output( "Loading from page and revision tables...\n" );
117 [ 'page', 'revision' ],
132 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
133 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
137 [ 'ORDER BY' => 'rev_timestamp DESC' ]
140 $this->output( "Inserting from page and revision tables...\n" );
142 foreach ( $res as $row ) {
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
,
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
160 ?
$dbw->addQuotes( RecentChange
::SRC_NEW
)
161 : $dbw->addQuotes( RecentChange
::SRC_EDIT
)
163 'rc_deleted' => $row->rev_deleted
167 if ( ( ++
$inserted %
$this->mBatchSize
) == 0 ) {
168 wfGetLBFactory()->waitForReplication();
174 * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
175 * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
177 private function rebuildRecentChangesTablePass2() {
178 $dbw = $this->getDB( DB_MASTER
);
180 $this->output( "Updating links and size differences...\n" );
182 # Fill in the rc_last_oldid field, which points to the previous edit
185 [ 'rc_cur_id', 'rc_this_oldid', 'rc_timestamp' ],
187 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
188 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
191 [ 'ORDER BY' => 'rc_cur_id,rc_timestamp' ]
198 foreach ( $res as $obj ) {
201 if ( $obj->rc_cur_id
!= $lastCurId ) {
202 # Switch! Look up the previous last edit, if any
203 $lastCurId = intval( $obj->rc_cur_id
);
204 $emit = $obj->rc_timestamp
;
206 $row = $dbw->selectRow(
208 [ 'rev_id', 'rev_len' ],
209 [ 'rev_page' => $lastCurId, "rev_timestamp < " . $dbw->addQuotes( $emit ) ],
211 [ 'ORDER BY' => 'rev_timestamp DESC' ]
214 $lastOldId = intval( $row->rev_id
);
215 # Grab the last text size if available
216 $lastSize = !is_null( $row->rev_len
) ?
intval( $row->rev_len
) : null;
221 $new = 1; // probably true
225 if ( $lastCurId == 0 ) {
226 $this->output( "Uhhh, something wrong? No curid\n" );
228 # Grab the entry's text size
229 $size = (int)$dbw->selectField(
232 [ 'rev_id' => $obj->rc_this_oldid
],
239 'rc_last_oldid' => $lastOldId,
241 'rc_type' => $new ? RC_NEW
: RC_EDIT
,
242 'rc_source' => $new === 1
243 ?
$dbw->addQuotes( RecentChange
::SRC_NEW
)
244 : $dbw->addQuotes( 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->mBatchSize
) == 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
);
274 $this->output( "Loading from user, page, and logging tables...\n" );
277 [ 'user', 'logging', 'page' ],
293 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
294 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
296 // Some logs don't go in RC since they are private.
297 // @FIXME: core/extensions also have spammy logs that don't go in RC.
298 'log_type' => array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) ),
301 [ 'ORDER BY' => 'log_timestamp DESC' ],
304 [ 'LEFT JOIN', [ 'log_namespace=page_namespace', 'log_title=page_title' ] ]
308 $field = $dbw->fieldInfo( 'recentchanges', 'rc_cur_id' );
311 foreach ( $res as $row ) {
315 'rc_timestamp' => $row->log_timestamp
,
316 'rc_user' => $row->log_user
,
317 'rc_user_text' => $row->user_name
,
318 'rc_namespace' => $row->log_namespace
,
319 'rc_title' => $row->log_title
,
320 'rc_comment' => $row->log_comment
,
325 'rc_this_oldid' => 0,
326 'rc_last_oldid' => 0,
328 'rc_source' => $dbw->addQuotes( RecentChange
::SRC_LOG
),
329 'rc_cur_id' => $field->isNullable()
331 : (int)$row->page_id
, // NULL => 0,
332 'rc_log_type' => $row->log_type
,
333 'rc_log_action' => $row->log_action
,
334 'rc_logid' => $row->log_id
,
335 'rc_params' => $row->log_params
,
336 'rc_deleted' => $row->log_deleted
341 if ( ( ++
$inserted %
$this->mBatchSize
) == 0 ) {
342 wfGetLBFactory()->waitForReplication();
348 * Rebuild pass 4: Mark bot and autopatrolled entries.
350 private function rebuildRecentChangesTablePass4() {
351 global $wgUseRCPatrol, $wgMiserMode;
353 $dbw = $this->getDB( DB_MASTER
);
355 list( $recentchanges, $usergroups, $user ) =
356 $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
358 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
359 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
360 # may be lost at this point (aside from joining on the patrol log table entries).
361 $botgroups = [ 'bot' ];
362 $autopatrolgroups = $wgUseRCPatrol ? User
::getGroupsWithPermission( 'autopatrol' ) : [];
364 # Flag our recent bot edits
366 $botwhere = $dbw->makeList( $botgroups );
368 $this->output( "Flagging bot account edits...\n" );
370 # Find all users that are bots
371 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
372 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
373 $res = $dbw->query( $sql, __METHOD__
);
376 foreach ( $res as $obj ) {
377 $botusers[] = $obj->user_name
;
380 # Fill in the rc_bot field
382 $rcids = $dbw->selectFieldValues(
386 'rc_user_text' => $botusers,
387 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
388 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
393 foreach ( array_chunk( $rcids, $this->mBatchSize
) as $rcidBatch ) {
397 [ 'rc_id' => $rcidBatch ],
400 wfGetLBFactory()->waitForReplication();
405 # Flag our recent autopatrolled edits
406 if ( !$wgMiserMode && $autopatrolgroups ) {
407 $patrolwhere = $dbw->makeList( $autopatrolgroups );
410 $this->output( "Flagging auto-patrolled edits...\n" );
412 # Find all users in RC with autopatrol rights
413 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
414 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
415 $res = $dbw->query( $sql, __METHOD__
);
417 foreach ( $res as $obj ) {
418 $patrolusers[] = $dbw->addQuotes( $obj->user_name
);
421 # Fill in the rc_patrolled field
422 if ( $patrolusers ) {
423 $patrolwhere = implode( ',', $patrolusers );
424 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
425 "WHERE rc_user_text IN($patrolwhere) " .
426 "AND rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ) . ' ' .
427 "AND rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) );
428 $dbw->query( $sql2 );
434 * Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log entry
435 * for a single action (upload only, at the moment, but potentially also move, protect, ...).
437 private function rebuildRecentChangesTablePass5() {
438 $dbw = wfGetDB( DB_MASTER
);
440 $this->output( "Removing duplicate revision and logging entries...\n" );
443 [ 'logging', 'log_search' ],
444 [ 'ls_value', 'ls_log_id' ],
446 'ls_log_id = log_id',
447 'ls_field' => 'associated_rev_id',
448 'log_type' => 'upload',
449 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
450 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
456 foreach ( $res as $obj ) {
457 $rev_id = $obj->ls_value
;
458 $log_id = $obj->ls_log_id
;
460 // Mark the logging row as having an associated rev id
463 /*SET*/ [ 'rc_this_oldid' => $rev_id ],
464 /*WHERE*/ [ 'rc_logid' => $log_id ],
468 // Delete the revision row
471 /*WHERE*/ [ 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ],
475 if ( ( ++
$updates %
$this->mBatchSize
) == 0 ) {
476 wfGetLBFactory()->waitForReplication();
482 * Purge cached feeds in $wanCache
484 private function purgeFeeds() {
485 global $wgFeedClasses;
487 $this->output( "Deleting feed timestamps.\n" );
489 $wanCache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
490 foreach ( $wgFeedClasses as $feed => $className ) {
491 $wanCache->delete( $wanCache->makeKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
496 $maintClass = "RebuildRecentchanges";
497 require_once RUN_MAINTENANCE_IF_MAIN
;