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 $this->rebuildRecentChangesTablePass1();
71 $this->rebuildRecentChangesTablePass2();
72 $this->rebuildRecentChangesTablePass3();
73 $this->rebuildRecentChangesTablePass4();
74 $this->rebuildRecentChangesTablePass5();
78 $this->
output(
"Done.\n" );
84 private function rebuildRecentChangesTablePass1() {
86 $commentStore = MediaWikiServices::getInstance()->getCommentStore();
92 $sec = $this->cutoffTo - $this->cutoffFrom;
93 $days = $sec / 24 / 3600;
94 $this->
output(
"Rebuilding range of $sec seconds ($days days)\n" );
99 $this->
output(
"Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
102 $this->cutoffTo = time();
105 $this->
output(
"Clearing recentchanges table for time range...\n" );
106 $rcids = $dbw->selectFieldValues(
110 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
111 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
115 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
116 $dbw->delete(
'recentchanges', [
'rc_id' => $rcidBatch ], __METHOD__ );
120 $this->
output(
"Loading from page and revision tables...\n" );
122 $commentQuery = $commentStore->getJoin(
'rev_comment' );
123 $actorQuery = ActorMigration::newMigration()->getJoin(
'rev_user' );
125 [
'revision',
'page' ] + $commentQuery[
'tables'] + $actorQuery[
'tables'],
135 ] + $commentQuery[
'fields'] + $actorQuery[
'fields'],
137 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
138 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
141 [
'ORDER BY' =>
'rev_timestamp DESC' ],
143 'page' => [
'JOIN',
'rev_page=page_id' ],
144 ] + $commentQuery[
'joins'] + $actorQuery[
'joins']
147 $this->
output(
"Inserting from page and revision tables...\n" );
149 foreach (
$res as $row ) {
150 $comment = $commentStore->getComment(
'rev_comment', $row );
154 'rc_timestamp' => $row->rev_timestamp,
155 'rc_actor' => $row->rev_actor,
156 'rc_namespace' => $row->page_namespace,
157 'rc_title' => $row->page_title,
158 'rc_minor' => $row->rev_minor_edit,
160 'rc_new' => $row->page_is_new,
161 'rc_cur_id' => $row->page_id,
162 'rc_this_oldid' => $row->rev_id,
163 'rc_last_oldid' => 0,
166 'rc_deleted' => $row->rev_deleted
167 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ),
171 $rcid = $dbw->insertId();
174 [
'ct_rc_id' => $rcid ],
175 [
'ct_rev_id' => $row->rev_id ],
189 private function rebuildRecentChangesTablePass2() {
192 $this->
output(
"Updating links and size differences...\n" );
194 # Fill in the rc_last_oldid field, which points to the previous edit
197 [
'rc_cur_id',
'rc_this_oldid',
'rc_timestamp' ],
199 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
200 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
203 [
'ORDER BY' => [
'rc_cur_id',
'rc_timestamp' ] ]
210 foreach (
$res as $row ) {
213 if ( $row->rc_cur_id != $lastCurId ) {
214 # Switch! Look up the previous last edit, if any
215 $lastCurId = intval( $row->rc_cur_id );
216 $emit = $row->rc_timestamp;
218 $revRow = $dbw->selectRow(
220 [
'rev_id',
'rev_len' ],
221 [
'rev_page' => $lastCurId,
"rev_timestamp < " . $dbw->addQuotes( $emit ) ],
223 [
'ORDER BY' =>
'rev_timestamp DESC' ]
226 $lastOldId = intval( $revRow->rev_id );
227 # Grab the last text size if available
228 $lastSize = $revRow->rev_len !==
null ? intval( $revRow->rev_len ) : null;
237 if ( $lastCurId == 0 ) {
238 $this->
output(
"Uhhh, something wrong? No curid\n" );
240 # Grab the entry's text size
241 $size = (int)$dbw->selectField(
244 [
'rev_id' => $row->rc_this_oldid ],
251 'rc_last_oldid' => $lastOldId,
255 'rc_old_len' => $lastSize,
256 'rc_new_len' => $size,
259 'rc_cur_id' => $lastCurId,
260 'rc_this_oldid' => $row->rc_this_oldid,
261 'rc_timestamp' => $row->rc_timestamp
266 $lastOldId = intval( $row->rc_this_oldid );
279 private function rebuildRecentChangesTablePass3() {
283 $commentStore = MediaWikiServices::getInstance()->getCommentStore();
288 $this->
output(
"Loading from user and logging tables...\n" );
290 $commentQuery = $commentStore->getJoin(
'log_comment' );
292 [
'logging' ] + $commentQuery[
'tables'],
304 ] + $commentQuery[
'fields'],
306 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
307 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
309 'log_type' => array_diff( LogPage::validTypes(), $nonRCLogs ),
312 [
'ORDER BY' => [
'log_timestamp DESC',
'log_id DESC' ] ],
313 $commentQuery[
'joins']
316 $field = $dbw->fieldInfo(
'recentchanges',
'rc_cur_id' );
319 foreach (
$res as $row ) {
320 $comment = $commentStore->getComment(
'log_comment', $row );
324 'rc_timestamp' => $row->log_timestamp,
325 'rc_actor' => $row->log_actor,
326 'rc_namespace' => $row->log_namespace,
327 'rc_title' => $row->log_title,
330 'rc_patrolled' => $row->log_type ==
'upload' ? 0 : 2,
332 'rc_this_oldid' => 0,
333 'rc_last_oldid' => 0,
336 'rc_cur_id' => $field->isNullable()
338 : (int)$row->log_page,
339 'rc_log_type' => $row->log_type,
340 'rc_log_action' => $row->log_action,
341 'rc_logid' => $row->log_id,
342 'rc_params' => $row->log_params,
343 'rc_deleted' => $row->log_deleted
344 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ),
348 $rcid = $dbw->insertId();
351 [
'ct_rc_id' => $rcid ],
352 [
'ct_log_id' => $row->log_id ],
370 private function findRcIdsWithGroups( $db, $groups, $conds = [] ) {
371 if ( !count( $groups ) ) {
374 return $db->selectFieldValues(
375 [
'recentchanges',
'actor',
'user_groups' ],
378 "rc_timestamp > " . $db->addQuotes( $db->timestamp( $this->cutoffFrom ) ),
379 "rc_timestamp < " . $db->addQuotes( $db->timestamp( $this->cutoffTo ) ),
380 'ug_group' => $groups
385 'actor' => [
'JOIN',
'actor_id=rc_actor' ],
386 'user_groups' => [
'JOIN',
'ug_user=actor_user' ]
394 private function rebuildRecentChangesTablePass4() {
399 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
400 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
401 # may be lost at this point (aside from joining on the patrol log table entries).
402 $botgroups = [
'bot' ];
404 MediaWikiServices::getInstance()->getGroupPermissionsLookup()
405 ->getGroupsWithPermission(
'autopatrol' ) : [];
407 # Flag our recent bot edits
410 $this->
output(
"Flagging bot account edits...\n" );
412 # Fill in the rc_bot field
413 $rcids = $this->findRcIdsWithGroups( $dbw, $botgroups );
415 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
419 [
'rc_id' => $rcidBatch ],
426 # Flag our recent autopatrolled edits
427 if ( !$wgMiserMode && $autopatrolgroups ) {
428 $this->
output(
"Flagging auto-patrolled edits...\n" );
430 $conds = [
'rc_patrolled' => 0 ];
434 $subConds[] =
'rc_source = ' . $dbw->addQuotes( RecentChange::SRC_NEW );
437 $subConds[] =
'rc_log_type = ' . $dbw->addQuotes(
'upload' );
439 $conds[] = $dbw->makeList( $subConds, IDatabase::LIST_OR );
442 $rcids = $this->findRcIdsWithGroups( $dbw, $autopatrolgroups, $conds );
443 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
446 [
'rc_patrolled' => 2 ],
447 [
'rc_id' => $rcidBatch ],
459 private function rebuildRecentChangesTablePass5() {
462 $this->
output(
"Removing duplicate revision and logging entries...\n" );
465 [
'logging',
'log_search' ],
466 [
'ls_value',
'ls_log_id' ],
468 'ls_log_id = log_id',
469 'ls_field' =>
'associated_rev_id',
470 'log_type != ' . $dbw->addQuotes(
'create' ),
471 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
472 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
478 foreach (
$res as $row ) {
479 $rev_id = $row->ls_value;
480 $log_id = $row->ls_log_id;
485 [
'rc_this_oldid' => $rev_id ],
486 [
'rc_logid' => $log_id ],
493 [
'rc_this_oldid' => $rev_id,
'rc_logid' => 0 ],
506 private function purgeFeeds() {
509 $this->
output(
"Deleting feed timestamps.\n" );
511 $wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
513 $wanCache->delete( $wanCache->makeKey(
'rcfeed', $feed,
'timestamp' ) ); # Good enough
for now.
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.