47 parent::__construct();
52 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
58 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
70 $this->
fatalError(
"Both 'from' and 'to' must be given, or neither" );
73 $this->rebuildRecentChangesTablePass1();
74 $this->rebuildRecentChangesTablePass2();
75 $this->rebuildRecentChangesTablePass3();
76 $this->rebuildRecentChangesTablePass4();
77 $this->rebuildRecentChangesTablePass5();
81 $this->
output(
"Done.\n" );
87 private function rebuildRecentChangesTablePass1() {
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->newSelectQueryBuilder()
111 ->from(
'recentchanges' )
112 ->where( $dbw->expr(
'rc_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ) )
113 ->andWhere( $dbw->expr(
'rc_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ) )
114 ->caller( __METHOD__ )->fetchFieldValues();
115 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
116 $dbw->newDeleteQueryBuilder()
117 ->deleteFrom(
'recentchanges' )
118 ->where( [
'rc_id' => $rcidBatch ] )
119 ->caller( __METHOD__ )->execute();
123 $this->
output(
"Loading from page and revision tables...\n" );
125 $res = $dbw->newSelectQueryBuilder()
136 'rev_comment_text' =>
'comment_rev_comment.comment_text',
137 'rev_comment_data' =>
'comment_rev_comment.comment_data',
138 'rev_comment_cid' =>
'comment_rev_comment.comment_id',
139 'rev_user' =>
'actor_rev_user.actor_user',
140 'rev_user_text' =>
'actor_rev_user.actor_name',
141 'rev_actor' =>
'rev_actor',
145 ->join(
'page',
null,
'rev_page=page_id' )
146 ->join(
'comment',
'comment_rev_comment',
'comment_rev_comment.comment_id = rev_comment_id' )
147 ->join(
'actor',
'actor_rev_user',
'actor_rev_user.actor_id = rev_actor' )
150 $dbw->expr(
'rev_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
151 $dbw->expr(
'rev_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) )
154 ->orderBy(
'rev_timestamp', SelectQueryBuilder::SORT_DESC )
155 ->caller( __METHOD__ )->fetchResultSet();
157 $this->
output(
"Inserting from page and revision tables...\n" );
159 foreach ( $res as $row ) {
160 $comment = $commentStore->getComment(
'rev_comment', $row );
161 $dbw->newInsertQueryBuilder()
162 ->insertInto(
'recentchanges' )
164 'rc_timestamp' => $row->rev_timestamp,
165 'rc_actor' => $row->rev_actor,
166 'rc_namespace' => $row->page_namespace,
167 'rc_title' => $row->page_title,
168 'rc_minor' => $row->rev_minor_edit,
170 'rc_new' => $row->page_is_new,
171 'rc_cur_id' => $row->page_id,
172 'rc_this_oldid' => $row->rev_id,
173 'rc_last_oldid' => 0,
176 'rc_deleted' => $row->rev_deleted
177 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ) )
178 ->caller( __METHOD__ )->
execute();
180 $rcid = $dbw->insertId();
181 $dbw->newUpdateQueryBuilder()
182 ->update(
'change_tag' )
183 ->set( [
'ct_rc_id' => $rcid ] )
184 ->where( [
'ct_rev_id' => $row->rev_id ] )
185 ->caller( __METHOD__ )->execute();
197 private function rebuildRecentChangesTablePass2() {
200 $this->
output(
"Updating links and size differences...\n" );
202 # Fill in the rc_last_oldid field, which points to the previous edit
203 $res = $dbw->newSelectQueryBuilder()
204 ->select( [
'rc_cur_id',
'rc_this_oldid',
'rc_timestamp' ] )
205 ->from(
'recentchanges' )
206 ->where( $dbw->expr(
'rc_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ) )
207 ->andWhere( $dbw->expr(
'rc_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ) )
208 ->orderBy( [
'rc_cur_id',
'rc_timestamp' ] )
209 ->caller( __METHOD__ )->fetchResultSet();
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->newSelectQueryBuilder()
224 ->select( [
'rev_id',
'rev_len' ] )
226 ->where( [
'rev_page' => $lastCurId, $dbw->expr(
'rev_timestamp',
'<', $emit ) ] )
227 ->orderBy(
'rev_timestamp DESC' )
228 ->caller( __METHOD__ )->fetchRow();
230 $lastOldId = intval( $revRow->rev_id );
231 # Grab the last text size if available
232 $lastSize = $revRow->rev_len !==
null ? intval( $revRow->rev_len ) : null;
241 if ( $lastCurId == 0 ) {
242 $this->
output(
"Uhhh, something wrong? No curid\n" );
244 # Grab the entry's text size
245 $size = (int)$dbw->newSelectQueryBuilder()
246 ->select(
'rev_len' )
248 ->where( [
'rev_id' => $row->rc_this_oldid ] )
249 ->caller( __METHOD__ )->fetchField();
251 $dbw->newUpdateQueryBuilder()
252 ->update(
'recentchanges' )
254 'rc_last_oldid' => $lastOldId,
258 'rc_old_len' => $lastSize,
259 'rc_new_len' => $size,
262 'rc_cur_id' => $lastCurId,
263 'rc_this_oldid' => $row->rc_this_oldid,
265 'rc_timestamp' => $row->rc_timestamp,
267 ->caller( __METHOD__ )->
execute();
269 $lastOldId = intval( $row->rc_this_oldid );
282 private function rebuildRecentChangesTablePass3() {
287 $nonRCLogs = array_merge(
293 $this->
output(
"Loading from user and logging tables...\n" );
295 $res = $dbw->newSelectQueryBuilder()
308 'log_comment_text' =>
'comment_log_comment.comment_text',
309 'log_comment_data' =>
'comment_log_comment.comment_data',
310 'log_comment_cid' =>
'comment_log_comment.comment_id',
314 ->join(
'comment',
'comment_log_comment',
'comment_log_comment.comment_id = log_comment_id' )
317 $dbw->expr(
'log_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
318 $dbw->expr(
'log_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ),
320 'log_type' => array_diff( LogPage::validTypes(), $nonRCLogs ),
323 ->orderBy( [
'log_timestamp DESC',
'log_id DESC' ] )
324 ->caller( __METHOD__ )->fetchResultSet();
326 $field = $dbw->fieldInfo(
'recentchanges',
'rc_cur_id' );
329 foreach ( $res as $row ) {
330 $comment = $commentStore->getComment(
'log_comment', $row );
331 $dbw->newInsertQueryBuilder()
332 ->insertInto(
'recentchanges' )
334 'rc_timestamp' => $row->log_timestamp,
335 'rc_actor' => $row->log_actor,
336 'rc_namespace' => $row->log_namespace,
337 'rc_title' => $row->log_title,
340 'rc_patrolled' => $row->log_type ==
'upload' ? 0 : 2,
342 'rc_this_oldid' => 0,
343 'rc_last_oldid' => 0,
346 'rc_cur_id' => $field->isNullable()
348 : (int)$row->log_page,
349 'rc_log_type' => $row->log_type,
350 'rc_log_action' => $row->log_action,
351 'rc_logid' => $row->log_id,
352 'rc_params' => $row->log_params,
353 'rc_deleted' => $row->log_deleted
354 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ) )
355 ->caller( __METHOD__ )->
execute();
357 $rcid = $dbw->insertId();
358 $dbw->newUpdateQueryBuilder()
359 ->update(
'change_tag' )
360 ->set( [
'ct_rc_id' => $rcid ] )
361 ->where( [
'ct_log_id' => $row->log_id ] )
362 ->caller( __METHOD__ )->execute();
378 private function findRcIdsWithGroups( $db, $groups, $conds = [] ) {
379 if ( !count( $groups ) ) {
382 return $db->newSelectQueryBuilder()
385 ->from(
'recentchanges' )
386 ->join(
'actor',
null,
'actor_id=rc_actor' )
387 ->join(
'user_groups',
null,
'ug_user=actor_user' )
390 $db->expr(
'rc_timestamp',
'>', $db->timestamp( $this->cutoffFrom ) ),
391 $db->expr(
'rc_timestamp',
'<', $db->timestamp( $this->cutoffTo ) ),
392 'ug_group' => $groups
394 ->caller( __METHOD__ )->fetchFieldValues();
400 private function rebuildRecentChangesTablePass4() {
405 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
406 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
407 # may be lost at this point (aside from joining on the patrol log table entries).
408 $botgroups = [
'bot' ];
411 ->getGroupsWithPermission(
'autopatrol' ) : [];
413 # Flag our recent bot edits
416 $this->
output(
"Flagging bot account edits...\n" );
418 # Fill in the rc_bot field
419 $rcids = $this->findRcIdsWithGroups( $dbw, $botgroups );
421 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
422 $dbw->newUpdateQueryBuilder()
423 ->update(
'recentchanges' )
424 ->set( [
'rc_bot' => 1 ] )
425 ->where( [
'rc_id' => $rcidBatch ] )
426 ->caller( __METHOD__ )->execute();
431 # Flag our recent autopatrolled edits
432 if ( !$wgMiserMode && $autopatrolgroups ) {
433 $this->
output(
"Flagging auto-patrolled edits...\n" );
435 $conds = [
'rc_patrolled' => 0 ];
439 $subConds[] = $dbw->expr(
'rc_source',
'=', RecentChange::SRC_NEW );
442 $subConds[] = $dbw->expr(
'rc_log_type',
'=',
'upload' );
444 $conds[] = $dbw->makeList( $subConds, IDatabase::LIST_OR );
447 $rcids = $this->findRcIdsWithGroups( $dbw, $autopatrolgroups, $conds );
448 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
449 $dbw->newUpdateQueryBuilder()
450 ->update(
'recentchanges' )
451 ->set( [
'rc_patrolled' => 2 ] )
452 ->where( [
'rc_id' => $rcidBatch ] )
453 ->caller( __METHOD__ )->execute();
463 private function rebuildRecentChangesTablePass5() {
466 $this->
output(
"Removing duplicate revision and logging entries...\n" );
468 $res = $dbw->newSelectQueryBuilder()
469 ->select( [
'ls_value',
'ls_log_id' ] )
471 ->join(
'log_search',
null,
'ls_log_id = log_id' )
473 'ls_field' =>
'associated_rev_id',
474 $dbw->expr(
'log_type',
'!=',
'create' ),
475 $dbw->expr(
'log_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
476 $dbw->expr(
'log_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ),
478 ->caller( __METHOD__ )->fetchResultSet();
481 foreach ( $res as $row ) {
482 $rev_id = $row->ls_value;
483 $log_id = $row->ls_log_id;
486 $dbw->newUpdateQueryBuilder()
487 ->update(
'recentchanges' )
488 ->set( [
'rc_this_oldid' => $rev_id ] )
489 ->where( [
'rc_logid' => $log_id ] )
490 ->caller( __METHOD__ )->execute();
493 $dbw->newDeleteQueryBuilder()
494 ->deleteFrom(
'recentchanges' )
495 ->where( [
'rc_this_oldid' => $rev_id,
'rc_logid' => 0 ] )
496 ->caller( __METHOD__ )->execute();
507 private function purgeFeeds() {
510 $this->
output(
"Deleting feed timestamps.\n" );
513 foreach ( $wgFeedClasses as $feed => $className ) {
514 $wanCache->delete( $wanCache->makeKey(
'rcfeed', $feed,
'timestamp' ) ); # Good enough
for now.