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';
29 * Maintenance script that rebuilds recent changes from scratch.
31 * @ingroup Maintenance
33 class RebuildRecentchanges
extends Maintenance
{
34 /** @var integer UNIX timestamp */
36 /** @var integer UNIX timestamp */
39 public function __construct() {
40 parent
::__construct();
41 $this->addDescription( 'Rebuild recent changes' );
45 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
51 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
55 $this->setBatchSize( 200 );
58 public function execute() {
60 ( $this->hasOption( 'from' ) && !$this->hasOption( 'to' ) ) ||
61 ( !$this->hasOption( 'from' ) && $this->hasOption( 'to' ) )
63 $this->error( "Both 'from' and 'to' must be given, or neither", 1 );
66 $this->rebuildRecentChangesTablePass1();
67 $this->rebuildRecentChangesTablePass2();
68 $this->rebuildRecentChangesTablePass3();
69 $this->rebuildRecentChangesTablePass4();
70 $this->rebuildRecentChangesTablePass5();
71 if ( !( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) ) {
74 $this->output( "Done.\n" );
78 * Rebuild pass 1: Insert `recentchanges` entries for page revisions.
80 private function rebuildRecentChangesTablePass1() {
81 $dbw = $this->getDB( DB_MASTER
);
83 if ( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) {
84 $this->cutoffFrom
= wfTimestamp( TS_UNIX
, $this->getOption( 'from' ) );
85 $this->cutoffTo
= wfTimestamp( TS_UNIX
, $this->getOption( 'to' ) );
87 $sec = $this->cutoffTo
- $this->cutoffFrom
;
88 $days = $sec / 24 / 3600;
89 $this->output( "Rebuilding range of $sec seconds ($days days)\n" );
93 $days = $wgRCMaxAge / 24 / 3600;
94 $this->output( "Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
96 $this->cutoffFrom
= time() - $wgRCMaxAge;
97 $this->cutoffTo
= time();
100 $this->output( "Clearing recentchanges table for time range...\n" );
101 $rcids = $dbw->selectFieldValues(
105 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
106 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
109 foreach ( array_chunk( $rcids, $this->mBatchSize
) as $rcidBatch ) {
110 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcidBatch ], __METHOD__
);
111 wfGetLBFactory()->waitForReplication();
114 $this->output( "Loading from page and revision tables...\n" );
116 [ 'page', 'revision' ],
131 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
132 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
136 [ 'ORDER BY' => 'rev_timestamp DESC' ]
139 $this->output( "Inserting from page and revision tables...\n" );
141 foreach ( $res as $row ) {
145 'rc_timestamp' => $row->rev_timestamp
,
146 'rc_user' => $row->rev_user
,
147 'rc_user_text' => $row->rev_user_text
,
148 'rc_namespace' => $row->page_namespace
,
149 'rc_title' => $row->page_title
,
150 'rc_comment' => $row->rev_comment
,
151 'rc_minor' => $row->rev_minor_edit
,
153 'rc_new' => $row->page_is_new
,
154 'rc_cur_id' => $row->page_id
,
155 'rc_this_oldid' => $row->rev_id
,
156 'rc_last_oldid' => 0, // is this ok?
157 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
158 'rc_source' => $row->page_is_new
159 ?
$dbw->addQuotes( RecentChange
::SRC_NEW
)
160 : $dbw->addQuotes( RecentChange
::SRC_EDIT
)
162 'rc_deleted' => $row->rev_deleted
166 if ( ( ++
$inserted %
$this->mBatchSize
) == 0 ) {
167 wfGetLBFactory()->waitForReplication();
173 * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
174 * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
176 private function rebuildRecentChangesTablePass2() {
177 $dbw = $this->getDB( DB_MASTER
);
179 $this->output( "Updating links and size differences...\n" );
181 # Fill in the rc_last_oldid field, which points to the previous edit
184 [ 'rc_cur_id', 'rc_this_oldid', 'rc_timestamp' ],
186 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
187 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
190 [ 'ORDER BY' => 'rc_cur_id,rc_timestamp' ]
197 foreach ( $res as $obj ) {
200 if ( $obj->rc_cur_id
!= $lastCurId ) {
201 # Switch! Look up the previous last edit, if any
202 $lastCurId = intval( $obj->rc_cur_id
);
203 $emit = $obj->rc_timestamp
;
205 $row = $dbw->selectRow(
207 [ 'rev_id', 'rev_len' ],
208 [ 'rev_page' => $lastCurId, "rev_timestamp < " . $dbw->addQuotes( $emit ) ],
210 [ 'ORDER BY' => 'rev_timestamp DESC' ]
213 $lastOldId = intval( $row->rev_id
);
214 # Grab the last text size if available
215 $lastSize = !is_null( $row->rev_len
) ?
intval( $row->rev_len
) : null;
220 $new = 1; // probably true
224 if ( $lastCurId == 0 ) {
225 $this->output( "Uhhh, something wrong? No curid\n" );
227 # Grab the entry's text size
228 $size = (int)$dbw->selectField(
231 [ 'rev_id' => $obj->rc_this_oldid
],
238 'rc_last_oldid' => $lastOldId,
240 'rc_type' => $new ? RC_NEW
: RC_EDIT
,
241 'rc_source' => $new === 1
242 ?
$dbw->addQuotes( RecentChange
::SRC_NEW
)
243 : $dbw->addQuotes( RecentChange
::SRC_EDIT
),
244 'rc_old_len' => $lastSize,
245 'rc_new_len' => $size,
248 'rc_cur_id' => $lastCurId,
249 'rc_this_oldid' => $obj->rc_this_oldid
,
250 'rc_timestamp' => $obj->rc_timestamp
// index usage
255 $lastOldId = intval( $obj->rc_this_oldid
);
258 if ( ( ++
$updated %
$this->mBatchSize
) == 0 ) {
259 wfGetLBFactory()->waitForReplication();
266 * Rebuild pass 3: Insert `recentchanges` entries for action logs.
268 private function rebuildRecentChangesTablePass3() {
269 global $wgLogTypes, $wgLogRestrictions;
271 $dbw = $this->getDB( DB_MASTER
);
273 $this->output( "Loading from user, page, and logging tables...\n" );
276 [ 'user', 'logging', 'page' ],
292 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
293 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
295 // Some logs don't go in RC since they are private.
296 // @FIXME: core/extensions also have spammy logs that don't go in RC.
297 'log_type' => array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) ),
300 [ 'ORDER BY' => 'log_timestamp DESC' ],
303 [ 'LEFT JOIN', [ 'log_namespace=page_namespace', 'log_title=page_title' ] ]
308 foreach ( $res as $row ) {
312 'rc_timestamp' => $row->log_timestamp
,
313 'rc_user' => $row->log_user
,
314 'rc_user_text' => $row->user_name
,
315 'rc_namespace' => $row->log_namespace
,
316 'rc_title' => $row->log_title
,
317 'rc_comment' => $row->log_comment
,
322 'rc_this_oldid' => 0,
323 'rc_last_oldid' => 0,
325 'rc_source' => $dbw->addQuotes( RecentChange
::SRC_LOG
),
326 'rc_cur_id' => $dbw->cascadingDeletes()
328 : (int)$row->page_id
, // NULL => 0,
329 'rc_log_type' => $row->log_type
,
330 'rc_log_action' => $row->log_action
,
331 'rc_logid' => $row->log_id
,
332 'rc_params' => $row->log_params
,
333 'rc_deleted' => $row->log_deleted
338 if ( ( ++
$inserted %
$this->mBatchSize
) == 0 ) {
339 wfGetLBFactory()->waitForReplication();
345 * Rebuild pass 4: Mark bot and autopatrolled entries.
347 private function rebuildRecentChangesTablePass4() {
348 global $wgUseRCPatrol, $wgMiserMode;
350 $dbw = $this->getDB( DB_MASTER
);
352 list( $recentchanges, $usergroups, $user ) =
353 $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
355 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
356 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
357 # may be lost at this point (aside from joining on the patrol log table entries).
358 $botgroups = [ 'bot' ];
359 $autopatrolgroups = $wgUseRCPatrol ? User
::getGroupsWithPermission( 'autopatrol' ) : [];
361 # Flag our recent bot edits
363 $botwhere = $dbw->makeList( $botgroups );
365 $this->output( "Flagging bot account edits...\n" );
367 # Find all users that are bots
368 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
369 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
370 $res = $dbw->query( $sql, __METHOD__
);
373 foreach ( $res as $obj ) {
374 $botusers[] = $obj->user_name
;
377 # Fill in the rc_bot field
379 $rcids = $dbw->selectFieldValues(
383 'rc_user_text' => $botusers,
384 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
385 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
390 foreach ( array_chunk( $rcids, $this->mBatchSize
) as $rcidBatch ) {
394 [ 'rc_id' => $rcidBatch ],
397 wfGetLBFactory()->waitForReplication();
402 # Flag our recent autopatrolled edits
403 if ( !$wgMiserMode && $autopatrolgroups ) {
404 $patrolwhere = $dbw->makeList( $autopatrolgroups );
407 $this->output( "Flagging auto-patrolled edits...\n" );
409 # Find all users in RC with autopatrol rights
410 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
411 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
412 $res = $dbw->query( $sql, __METHOD__
);
414 foreach ( $res as $obj ) {
415 $patrolusers[] = $dbw->addQuotes( $obj->user_name
);
418 # Fill in the rc_patrolled field
419 if ( $patrolusers ) {
420 $patrolwhere = implode( ',', $patrolusers );
421 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
422 "WHERE rc_user_text IN($patrolwhere) " .
423 "AND rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ) . ' ' .
424 "AND rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) );
425 $dbw->query( $sql2 );
431 * Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log entry
432 * for a single action (upload only, at the moment, but potentially also move, protect, ...).
434 private function rebuildRecentChangesTablePass5() {
435 $dbw = wfGetDB( DB_MASTER
);
437 $this->output( "Removing duplicate revision and logging entries...\n" );
440 [ 'logging', 'log_search' ],
441 [ 'ls_value', 'ls_log_id' ],
443 'ls_log_id = log_id',
444 'ls_field' => 'associated_rev_id',
445 'log_type' => 'upload',
446 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
447 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
453 foreach ( $res as $obj ) {
454 $rev_id = $obj->ls_value
;
455 $log_id = $obj->ls_log_id
;
457 // Mark the logging row as having an associated rev id
460 /*SET*/ [ 'rc_this_oldid' => $rev_id ],
461 /*WHERE*/ [ 'rc_logid' => $log_id ],
465 // Delete the revision row
468 /*WHERE*/ [ 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ],
472 if ( ( ++
$updates %
$this->mBatchSize
) == 0 ) {
473 wfGetLBFactory()->waitForReplication();
479 * Purge cached feeds in $messageMemc
481 private function purgeFeeds() {
482 global $wgFeedClasses, $messageMemc;
484 $this->output( "Deleting feed timestamps.\n" );
486 foreach ( $wgFeedClasses as $feed => $className ) {
487 $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
492 $maintClass = "RebuildRecentchanges";
493 require_once RUN_MAINTENANCE_IF_MAIN
;