26 require_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" );
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' );
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" );
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,
168 'rc_deleted' => $row->rev_deleted
169 ] + $commentStore->insert( $dbw,
'rc_comment', $comment )
170 + $actorMigration->getInsertValues( $dbw,
'rc_user', $user ),
188 $this->
output(
"Updating links and size differences...\n" );
190 # Fill in the rc_last_oldid field, which points to the previous edit
193 [
'rc_cur_id',
'rc_this_oldid',
'rc_timestamp' ],
195 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
196 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
199 [
'ORDER BY' =>
'rc_cur_id,rc_timestamp' ]
206 foreach (
$res as $row ) {
209 if ( $row->rc_cur_id != $lastCurId ) {
210 # Switch! Look up the previous last edit, if any
211 $lastCurId = intval( $row->rc_cur_id );
212 $emit = $row->rc_timestamp;
214 $revRow = $dbw->selectRow(
216 [
'rev_id',
'rev_len' ],
217 [
'rev_page' => $lastCurId,
"rev_timestamp < " . $dbw->addQuotes( $emit ) ],
219 [
'ORDER BY' =>
'rev_timestamp DESC' ]
222 $lastOldId = intval( $revRow->rev_id );
223 # Grab the last text size if available
224 $lastSize = !is_null( $revRow->rev_len ) ? intval( $revRow->rev_len ) :
null;
233 if ( $lastCurId == 0 ) {
234 $this->
output(
"Uhhh, something wrong? No curid\n" );
236 # Grab the entry's text size
237 $size = (int)$dbw->selectField(
240 [
'rev_id' => $row->rc_this_oldid ],
247 'rc_last_oldid' => $lastOldId,
251 'rc_old_len' => $lastSize,
252 'rc_new_len' => $size,
255 'rc_cur_id' => $lastCurId,
256 'rc_this_oldid' => $row->rc_this_oldid,
257 'rc_timestamp' => $row->rc_timestamp
262 $lastOldId = intval( $row->rc_this_oldid );
286 $this->
output(
"Loading from user and logging tables...\n" );
288 $commentQuery = $commentStore->getJoin(
'log_comment' );
291 [
'logging' ] + $commentQuery[
'tables'] + $actorQuery[
'tables'],
302 ] + $commentQuery[
'fields'] + $actorQuery[
'fields'],
304 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
305 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
310 [
'ORDER BY' =>
'log_timestamp DESC' ],
311 $commentQuery[
'joins'] + $actorQuery[
'joins']
314 $field = $dbw->fieldInfo(
'recentchanges',
'rc_cur_id' );
318 foreach (
$res as $row ) {
319 $comment = $commentStore->getComment(
'log_comment', $row );
324 'rc_timestamp' => $row->log_timestamp,
325 'rc_namespace' => $row->log_namespace,
326 'rc_title' => $row->log_title,
331 'rc_this_oldid' => 0,
332 'rc_last_oldid' => 0,
335 'rc_cur_id' => $field->isNullable()
337 : (int)$row->log_page,
338 'rc_log_type' => $row->log_type,
339 'rc_log_action' => $row->log_action,
340 'rc_logid' => $row->log_id,
341 'rc_params' => $row->log_params,
342 'rc_deleted' => $row->log_deleted
343 ] + $commentStore->insert( $dbw,
'rc_comment', $comment )
344 + $actorMigration->getInsertValues( $dbw,
'rc_user', $user ),
366 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
367 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
368 # may be lost at this point (aside from joining on the patrol log table entries).
369 $botgroups = [
'bot' ];
370 $autopatrolgroups =
$wgUseRCPatrol ? MediaWikiServices::getInstance()
371 ->getPermissionManager()
372 ->getGroupsWithPermission(
'autopatrol' ) : [];
374 # Flag our recent bot edits
376 $this->
output(
"Flagging bot account edits...\n" );
378 # Find all users that are bots
380 array_merge( [
'user_groups' ], $userQuery[
'tables'] ),
381 $userQuery[
'fields'],
382 [
'ug_group' => $botgroups ],
385 [
'user_groups' => [
'JOIN',
'user_id = ug_user' ] ] + $userQuery[
'joins']
389 foreach (
$res as $row ) {
393 # Fill in the rc_bot field
397 foreach ( $actorQuery[
'orconds'] as $cond ) {
398 $rcids = array_merge( $rcids, $dbw->selectFieldValues(
399 [
'recentchanges' ] + $actorQuery[
'tables'],
402 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
403 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
411 $rcids = array_values( array_unique( $rcids ) );
413 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
417 [
'rc_id' => $rcidBatch ],
425 # Flag our recent autopatrolled edits
429 $this->
output(
"Flagging auto-patrolled edits...\n" );
431 # Find all users in RC with autopatrol rights
433 array_merge( [
'user_groups' ], $userQuery[
'tables'] ),
434 $userQuery[
'fields'],
435 [
'ug_group' => $autopatrolgroups ],
438 [
'user_groups' => [
'JOIN',
'user_id = ug_user' ] ] + $userQuery[
'joins']
441 foreach (
$res as $row ) {
445 # Fill in the rc_patrolled field
446 if ( $patrolusers ) {
448 foreach ( $actorQuery[
'orconds'] as $cond ) {
451 [
'rc_patrolled' => 1 ],
454 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
455 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
474 $this->
output(
"Removing duplicate revision and logging entries...\n" );
477 [
'logging',
'log_search' ],
478 [
'ls_value',
'ls_log_id' ],
480 'ls_log_id = log_id',
481 'ls_field' =>
'associated_rev_id',
482 'log_type' =>
'upload',
483 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
484 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
490 foreach (
$res as $row ) {
491 $rev_id = $row->ls_value;
492 $log_id = $row->ls_log_id;
497 [
'rc_this_oldid' => $rev_id ],
498 [
'rc_logid' => $log_id ],
505 [
'rc_this_oldid' => $rev_id,
'rc_logid' => 0 ],
521 $this->
output(
"Deleting feed timestamps.\n" );
523 $wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
525 $wanCache->delete( $wanCache->makeKey(
'rcfeed', $feed,
'timestamp' ) ); # Good enough
for now.