44 parent::__construct();
49 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
55 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
67 $this->
fatalError(
"Both 'from' and 'to' must be given, or neither" );
70 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
71 $this->rebuildRecentChangesTablePass1( $lbFactory );
72 $this->rebuildRecentChangesTablePass2( $lbFactory );
73 $this->rebuildRecentChangesTablePass3( $lbFactory );
74 $this->rebuildRecentChangesTablePass4( $lbFactory );
75 $this->rebuildRecentChangesTablePass5( $lbFactory );
79 $this->
output(
"Done.\n" );
87 private function rebuildRecentChangesTablePass1(
ILBFactory $lbFactory ) {
89 $commentStore = CommentStore::getStore();
95 $sec = $this->cutoffTo - $this->cutoffFrom;
96 $days = $sec / 24 / 3600;
97 $this->
output(
"Rebuilding range of $sec seconds ($days days)\n" );
102 $this->
output(
"Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
105 $this->cutoffTo = time();
108 $this->
output(
"Clearing recentchanges table for time range...\n" );
109 $rcids = $dbw->selectFieldValues(
113 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
114 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
118 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
119 $dbw->delete(
'recentchanges', [
'rc_id' => $rcidBatch ], __METHOD__ );
123 $this->
output(
"Loading from page and revision tables...\n" );
125 $commentQuery = $commentStore->getJoin(
'rev_comment' );
128 [
'revision',
'page' ] + $commentQuery[
'tables'] + $actorQuery[
'tables'],
138 ] + $commentQuery[
'fields'] + $actorQuery[
'fields'],
140 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
141 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
144 [
'ORDER BY' =>
'rev_timestamp DESC' ],
146 'page' => [
'JOIN',
'rev_page=page_id' ],
147 ] + $commentQuery[
'joins'] + $actorQuery[
'joins']
150 $this->
output(
"Inserting from page and revision tables...\n" );
152 foreach (
$res as $row ) {
153 $comment = $commentStore->getComment(
'rev_comment', $row );
157 'rc_timestamp' => $row->rev_timestamp,
158 'rc_actor' => $row->rev_actor,
159 'rc_namespace' => $row->page_namespace,
160 'rc_title' => $row->page_title,
161 'rc_minor' => $row->rev_minor_edit,
163 'rc_new' => $row->page_is_new,
164 'rc_cur_id' => $row->page_id,
165 'rc_this_oldid' => $row->rev_id,
166 'rc_last_oldid' => 0,
169 'rc_deleted' => $row->rev_deleted
170 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ),
174 $rcid = $dbw->insertId();
177 [
'ct_rc_id' => $rcid ],
178 [
'ct_rev_id' => $row->rev_id ],
194 private function rebuildRecentChangesTablePass2(
ILBFactory $lbFactory ) {
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 = $revRow->rev_len !==
null ? 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,
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 );
286 private function rebuildRecentChangesTablePass3(
ILBFactory $lbFactory ) {
295 $this->
output(
"Loading from user and logging tables...\n" );
297 $commentQuery = $commentStore->getJoin(
'log_comment' );
299 [
'logging' ] + $commentQuery[
'tables'],
311 ] + $commentQuery[
'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']
323 $field = $dbw->fieldInfo(
'recentchanges',
'rc_cur_id' );
326 foreach (
$res as $row ) {
327 $comment = $commentStore->getComment(
'log_comment', $row );
331 'rc_timestamp' => $row->log_timestamp,
332 'rc_actor' => $row->log_actor,
333 'rc_namespace' => $row->log_namespace,
334 'rc_title' => $row->log_title,
337 'rc_patrolled' => $row->log_type ==
'upload' ? 0 : 2,
339 'rc_this_oldid' => 0,
340 'rc_last_oldid' => 0,
343 'rc_cur_id' => $field->isNullable()
345 : (int)$row->log_page,
346 'rc_log_type' => $row->log_type,
347 'rc_log_action' => $row->log_action,
348 'rc_logid' => $row->log_id,
349 'rc_params' => $row->log_params,
350 'rc_deleted' => $row->log_deleted
351 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ),
355 $rcid = $dbw->insertId();
358 [
'ct_rc_id' => $rcid ],
359 [
'ct_log_id' => $row->log_id ],
377 private function findRcIdsWithGroups( $db, $groups, $conds = [] ) {
378 if ( !count( $groups ) ) {
381 return $db->selectFieldValues(
382 [
'recentchanges',
'actor',
'user_groups' ],
385 "rc_timestamp > " . $db->addQuotes( $db->timestamp( $this->cutoffFrom ) ),
386 "rc_timestamp < " . $db->addQuotes( $db->timestamp( $this->cutoffTo ) ),
387 'ug_group' => $groups
392 'actor' => [
'JOIN',
'actor_id=rc_actor' ],
393 'user_groups' => [
'JOIN',
'ug_user=actor_user' ]
403 private function rebuildRecentChangesTablePass4(
ILBFactory $lbFactory ) {
408 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
409 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
410 # may be lost at this point (aside from joining on the patrol log table entries).
411 $botgroups = [
'bot' ];
413 MediaWikiServices::getInstance()->getGroupPermissionsLookup()
414 ->getGroupsWithPermission(
'autopatrol' ) : [];
416 # Flag our recent bot edits
419 $this->
output(
"Flagging bot account edits...\n" );
421 # Fill in the rc_bot field
422 $rcids = $this->findRcIdsWithGroups( $dbw, $botgroups );
424 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
428 [
'rc_id' => $rcidBatch ],
435 # Flag our recent autopatrolled edits
437 $this->
output(
"Flagging auto-patrolled edits...\n" );
439 $conds = [
'rc_patrolled' => 0 ];
443 $subConds[] =
'rc_source = ' . $dbw->addQuotes( RecentChange::SRC_NEW );
446 $subConds[] =
'rc_log_type = ' . $dbw->addQuotes(
'upload' );
448 $conds[] = $dbw->makeList( $subConds, IDatabase::LIST_OR );
451 $rcids = $this->findRcIdsWithGroups( $dbw, $autopatrolgroups, $conds );
452 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
455 [
'rc_patrolled' => 2 ],
456 [
'rc_id' => $rcidBatch ],
470 private function rebuildRecentChangesTablePass5(
ILBFactory $lbFactory ) {
473 $this->
output(
"Removing duplicate revision and logging entries...\n" );
476 [
'logging',
'log_search' ],
477 [
'ls_value',
'ls_log_id' ],
479 'ls_log_id = log_id',
480 'ls_field' =>
'associated_rev_id',
481 'log_type != ' . $dbw->addQuotes(
'create' ),
482 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
483 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
489 foreach (
$res as $row ) {
490 $rev_id = $row->ls_value;
491 $log_id = $row->ls_log_id;
496 [
'rc_this_oldid' => $rev_id ],
497 [
'rc_logid' => $log_id ],
504 [
'rc_this_oldid' => $rev_id,
'rc_logid' => 0 ],
517 private function purgeFeeds() {
520 $this->
output(
"Deleting feed timestamps.\n" );
522 $wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
524 $wanCache->delete( $wanCache->makeKey(
'rcfeed', $feed,
'timestamp' ) ); # Good enough
for now.
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 was set.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.