26require_once __DIR__ .
'/Maintenance.php';
43 parent::__construct();
48 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
54 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
66 $this->
fatalError(
"Both 'from' and 'to' must be given, or neither" );
69 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
78 $this->
output(
"Done.\n" );
88 $commentStore = CommentStore::getStore();
95 $days = $sec / 24 / 3600;
96 $this->
output(
"Rebuilding range of $sec seconds ($days days)\n" );
101 $this->
output(
"Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
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__ );
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 );
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,
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 ),
174 $rcid = $dbw->insertId();
177 [
'ct_rc_id' => $rcid ],
178 [
'ct_rev_id' => $row->rev_id ],
197 $this->
output(
"Updating links and size differences...\n" );
199 # Fill in the rc_last_oldid field, which points to the previous edit
202 [
'rc_cur_id',
'rc_this_oldid',
'rc_timestamp' ],
204 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
205 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
208 [
'ORDER BY' =>
'rc_cur_id,rc_timestamp' ]
215 foreach (
$res as $row ) {
218 if ( $row->rc_cur_id != $lastCurId ) {
219 # Switch! Look up the previous last edit, if any
220 $lastCurId = intval( $row->rc_cur_id );
221 $emit = $row->rc_timestamp;
223 $revRow = $dbw->selectRow(
225 [
'rev_id',
'rev_len' ],
226 [
'rev_page' => $lastCurId,
"rev_timestamp < " . $dbw->addQuotes( $emit ) ],
228 [
'ORDER BY' =>
'rev_timestamp DESC' ]
231 $lastOldId = intval( $revRow->rev_id );
232 # Grab the last text size if available
233 $lastSize = !is_null( $revRow->rev_len ) ? intval( $revRow->rev_len ) :
null;
242 if ( $lastCurId == 0 ) {
243 $this->
output(
"Uhhh, something wrong? No curid\n" );
245 # Grab the entry's text size
246 $size = (int)$dbw->selectField(
249 [
'rev_id' => $row->rc_this_oldid ],
256 'rc_last_oldid' => $lastOldId,
259 'rc_source' => $new === 1 ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT,
260 'rc_old_len' => $lastSize,
261 'rc_new_len' => $size,
264 'rc_cur_id' => $lastCurId,
265 'rc_this_oldid' => $row->rc_this_oldid,
266 'rc_timestamp' => $row->rc_timestamp
271 $lastOldId = intval( $row->rc_this_oldid );
290 $commentStore = CommentStore::getStore();
295 $this->
output(
"Loading from user and logging tables...\n" );
297 $commentQuery = $commentStore->getJoin(
'log_comment' );
298 $actorQuery = ActorMigration::newMigration()->getJoin(
'log_user' );
300 [
'logging' ] + $commentQuery[
'tables'] + $actorQuery[
'tables'],
311 ] + $commentQuery[
'fields'] + $actorQuery[
'fields'],
313 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
314 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
319 [
'ORDER BY' =>
'log_timestamp DESC' ],
320 $commentQuery[
'joins'] + $actorQuery[
'joins']
323 $field = $dbw->fieldInfo(
'recentchanges',
'rc_cur_id' );
326 $actorMigration = ActorMigration::newMigration();
327 foreach (
$res as $row ) {
328 $comment = $commentStore->getComment(
'log_comment', $row );
333 'rc_timestamp' => $row->log_timestamp,
334 'rc_namespace' => $row->log_namespace,
335 'rc_title' => $row->log_title,
338 'rc_patrolled' => $row->log_type ==
'upload' ? 0 : 2,
340 'rc_this_oldid' => 0,
341 'rc_last_oldid' => 0,
343 'rc_source' => RecentChange::SRC_LOG,
344 'rc_cur_id' => $field->isNullable()
346 : (int)$row->log_page,
347 'rc_log_type' => $row->log_type,
348 'rc_log_action' => $row->log_action,
349 'rc_logid' => $row->log_id,
350 'rc_params' => $row->log_params,
351 'rc_deleted' => $row->log_deleted
352 ] + $commentStore->insert( $dbw,
'rc_comment', $comment )
353 + $actorMigration->getInsertValues( $dbw,
'rc_user', $user ),
357 $rcid = $dbw->insertId();
360 [
'ct_rc_id' => $rcid ],
361 [
'ct_log_id' => $row->log_id ],
383 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
384 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
385 # may be lost at this point (aside from joining on the patrol log table entries).
386 $botgroups = [
'bot' ];
387 $autopatrolgroups =
$wgUseRCPatrol ? MediaWikiServices::getInstance()
388 ->getPermissionManager()
389 ->getGroupsWithPermission(
'autopatrol' ) : [];
391 # Flag our recent bot edits
393 $this->
output(
"Flagging bot account edits...\n" );
395 # Find all users that are bots
397 array_merge( [
'user_groups' ], $userQuery[
'tables'] ),
398 $userQuery[
'fields'],
399 [
'ug_group' => $botgroups ],
402 [
'user_groups' => [
'JOIN',
'user_id = ug_user' ] ] + $userQuery[
'joins']
406 foreach (
$res as $row ) {
410 # Fill in the rc_bot field
412 $actorQuery = ActorMigration::newMigration()->getWhere( $dbw,
'rc_user', $botusers,
false );
414 foreach ( $actorQuery[
'orconds'] as $cond ) {
415 $rcids = array_merge( $rcids, $dbw->selectFieldValues(
416 [
'recentchanges' ] + $actorQuery[
'tables'],
419 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
420 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
428 $rcids = array_values( array_unique( $rcids ) );
430 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
434 [
'rc_id' => $rcidBatch ],
442 # Flag our recent autopatrolled edits
446 $this->
output(
"Flagging auto-patrolled edits...\n" );
448 # Find all users in RC with autopatrol rights
450 array_merge( [
'user_groups' ], $userQuery[
'tables'] ),
451 $userQuery[
'fields'],
452 [
'ug_group' => $autopatrolgroups ],
455 [
'user_groups' => [
'JOIN',
'user_id = ug_user' ] ] + $userQuery[
'joins']
458 foreach (
$res as $row ) {
462 # Fill in the rc_patrolled field
463 if ( $patrolusers ) {
464 $actorQuery = ActorMigration::newMigration()->getWhere( $dbw,
'rc_user', $patrolusers,
false );
465 foreach ( $actorQuery[
'orconds'] as $cond ) {
468 [
'rc_patrolled' => 2 ],
471 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
472 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
492 $this->
output(
"Removing duplicate revision and logging entries...\n" );
495 [
'logging',
'log_search' ],
496 [
'ls_value',
'ls_log_id' ],
498 'ls_log_id = log_id',
499 'ls_field' =>
'associated_rev_id',
500 'log_type' =>
'upload',
501 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
502 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
508 foreach (
$res as $row ) {
509 $rev_id = $row->ls_value;
510 $log_id = $row->ls_log_id;
515 [
'rc_this_oldid' => $rev_id ],
516 [
'rc_logid' => $log_id ],
523 [
'rc_this_oldid' => $rev_id,
'rc_logid' => 0 ],
539 $this->
output(
"Deleting feed timestamps.\n" );
541 $wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
543 $wanCache->delete( $wanCache->makeKey(
'rcfeed', $feed,
'timestamp' ) ); # Good enough
for now.
$wgLogRestrictions
This restricts log access to those who have a certain right Users without this will not see it in the...
$wgUseRCPatrol
Use RC Patrolling to check for vandalism (from recent changes and watchlists) New pages and new files...
$wgRCMaxAge
Recentchanges items are periodically purged; entries older than this many seconds will go.
$wgFeedClasses
Available feeds objects.
$wgFilterLogTypes
Show/hide links on Special:Log will be shown for these log types.
$wgMiserMode
Disable database-intensive features.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
const RUN_MAINTENANCE_IF_MAIN
static validTypes()
Get the list of valid log types.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option exists.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
Set the batch size.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Maintenance script that rebuilds recent changes from scratch.
execute()
Do the actual work.
rebuildRecentChangesTablePass5(ILBFactory $lbFactory)
Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log entry for a...
int $cutoffFrom
UNIX timestamp.
rebuildRecentChangesTablePass3(ILBFactory $lbFactory)
Rebuild pass 3: Insert recentchanges entries for action logs.
rebuildRecentChangesTablePass4(ILBFactory $lbFactory)
Rebuild pass 4: Mark bot and autopatrolled entries.
int $cutoffTo
UNIX timestamp.
__construct()
Default constructor.
rebuildRecentChangesTablePass1(ILBFactory $lbFactory)
Rebuild pass 1: Insert recentchanges entries for page revisions.
rebuildRecentChangesTablePass2(ILBFactory $lbFactory)
Rebuild pass 2: Enhance entries for page revisions with references to the previous revision (rc_last_...
purgeFeeds()
Purge cached feeds in $wanCache.
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new user object.
static newFromRow( $row, $data=null)
Create a new user object from a user row.
static newFromAnyId( $userId, $userName, $actorId, $dbDomain=false)
Static factory method for creation from an ID, name, and/or actor ID.