49 parent::__construct();
54 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
60 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
72 $this->
fatalError(
"Both 'from' and 'to' must be given, or neither" );
75 $this->rebuildRecentChangesTablePass1();
76 $this->rebuildRecentChangesTablePass2();
77 $this->rebuildRecentChangesTablePass3();
78 $this->rebuildRecentChangesTablePass4();
79 $this->rebuildRecentChangesTablePass5();
83 $this->
output(
"Done.\n" );
89 private function rebuildRecentChangesTablePass1() {
97 $sec = $this->cutoffTo - $this->cutoffFrom;
98 $days = $sec / 24 / 3600;
99 $this->
output(
"Rebuilding range of $sec seconds ($days days)\n" );
104 $this->
output(
"Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
107 $this->cutoffTo = time();
110 $this->
output(
"Clearing recentchanges table for time range...\n" );
111 $rcids = $dbw->newSelectQueryBuilder()
113 ->from(
'recentchanges' )
114 ->where( $dbw->expr(
'rc_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ) )
115 ->andWhere( $dbw->expr(
'rc_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ) )
116 ->caller( __METHOD__ )->fetchFieldValues();
117 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
118 $dbw->newDeleteQueryBuilder()
119 ->deleteFrom(
'recentchanges' )
120 ->where( [
'rc_id' => $rcidBatch ] )
121 ->caller( __METHOD__ )->execute();
125 $this->
output(
"Loading from page and revision tables...\n" );
127 $res = $dbw->newSelectQueryBuilder()
138 'rev_comment_text' =>
'comment_rev_comment.comment_text',
139 'rev_comment_data' =>
'comment_rev_comment.comment_data',
140 'rev_comment_cid' =>
'comment_rev_comment.comment_id',
141 'rev_user' =>
'actor_rev_user.actor_user',
142 'rev_user_text' =>
'actor_rev_user.actor_name',
143 'rev_actor' =>
'rev_actor',
147 ->join(
'page',
null,
'rev_page=page_id' )
148 ->join(
'comment',
'comment_rev_comment',
'comment_rev_comment.comment_id = rev_comment_id' )
149 ->join(
'actor',
'actor_rev_user',
'actor_rev_user.actor_id = rev_actor' )
152 $dbw->expr(
'rev_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
153 $dbw->expr(
'rev_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) )
156 ->orderBy(
'rev_timestamp', SelectQueryBuilder::SORT_DESC )
157 ->caller( __METHOD__ )->fetchResultSet();
159 $this->
output(
"Inserting from page and revision tables...\n" );
161 foreach ( $res as $row ) {
162 $comment = $commentStore->getComment(
'rev_comment', $row );
163 $dbw->newInsertQueryBuilder()
164 ->insertInto(
'recentchanges' )
166 'rc_timestamp' => $row->rev_timestamp,
167 'rc_actor' => $row->rev_actor,
168 'rc_namespace' => $row->page_namespace,
169 'rc_title' => $row->page_title,
170 'rc_minor' => $row->rev_minor_edit,
172 'rc_new' => $row->page_is_new,
173 'rc_cur_id' => $row->page_id,
174 'rc_this_oldid' => $row->rev_id,
175 'rc_last_oldid' => 0,
178 'rc_deleted' => $row->rev_deleted
179 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ) )
180 ->caller( __METHOD__ )->
execute();
182 $rcid = $dbw->insertId();
183 $dbw->newUpdateQueryBuilder()
184 ->update(
'change_tag' )
185 ->set( [
'ct_rc_id' => $rcid ] )
186 ->where( [
'ct_rev_id' => $row->rev_id ] )
187 ->caller( __METHOD__ )->execute();
199 private function rebuildRecentChangesTablePass2() {
202 $this->
output(
"Updating links and size differences...\n" );
204 # Fill in the rc_last_oldid field, which points to the previous edit
205 $res = $dbw->newSelectQueryBuilder()
206 ->select( [
'rc_cur_id',
'rc_this_oldid',
'rc_timestamp' ] )
207 ->from(
'recentchanges' )
208 ->where( $dbw->expr(
'rc_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ) )
209 ->andWhere( $dbw->expr(
'rc_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ) )
210 ->orderBy( [
'rc_cur_id',
'rc_timestamp' ] )
211 ->caller( __METHOD__ )->fetchResultSet();
217 foreach ( $res as $row ) {
220 if ( $row->rc_cur_id != $lastCurId ) {
221 # Switch! Look up the previous last edit, if any
222 $lastCurId = intval( $row->rc_cur_id );
223 $emit = $row->rc_timestamp;
225 $revRow = $dbw->newSelectQueryBuilder()
226 ->select( [
'rev_id',
'rev_len' ] )
228 ->where( [
'rev_page' => $lastCurId, $dbw->expr(
'rev_timestamp',
'<', $emit ) ] )
229 ->orderBy(
'rev_timestamp DESC' )
230 ->caller( __METHOD__ )->fetchRow();
232 $lastOldId = intval( $revRow->rev_id );
233 # Grab the last text size if available
234 $lastSize = $revRow->rev_len !==
null ? intval( $revRow->rev_len ) : null;
243 if ( $lastCurId == 0 ) {
244 $this->
output(
"Uhhh, something wrong? No curid\n" );
246 # Grab the entry's text size
247 $size = (int)$dbw->newSelectQueryBuilder()
248 ->select(
'rev_len' )
250 ->where( [
'rev_id' => $row->rc_this_oldid ] )
251 ->caller( __METHOD__ )->fetchField();
253 $dbw->newUpdateQueryBuilder()
254 ->update(
'recentchanges' )
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,
267 'rc_timestamp' => $row->rc_timestamp,
269 ->caller( __METHOD__ )->
execute();
271 $lastOldId = intval( $row->rc_this_oldid );
284 private function rebuildRecentChangesTablePass3() {
289 $nonRCLogs = array_merge(
295 $this->
output(
"Loading from user and logging tables...\n" );
297 $res = $dbw->newSelectQueryBuilder()
310 'log_comment_text' =>
'comment_log_comment.comment_text',
311 'log_comment_data' =>
'comment_log_comment.comment_data',
312 'log_comment_cid' =>
'comment_log_comment.comment_id',
316 ->join(
'comment',
'comment_log_comment',
'comment_log_comment.comment_id = log_comment_id' )
319 $dbw->expr(
'log_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
320 $dbw->expr(
'log_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ),
322 'log_type' => array_diff( LogPage::validTypes(), $nonRCLogs ),
325 ->orderBy( [
'log_timestamp DESC',
'log_id DESC' ] )
326 ->caller( __METHOD__ )->fetchResultSet();
328 $field = $dbw->fieldInfo(
'recentchanges',
'rc_cur_id' );
331 foreach ( $res as $row ) {
332 $comment = $commentStore->getComment(
'log_comment', $row );
333 $dbw->newInsertQueryBuilder()
334 ->insertInto(
'recentchanges' )
336 'rc_timestamp' => $row->log_timestamp,
337 'rc_actor' => $row->log_actor,
338 'rc_namespace' => $row->log_namespace,
339 'rc_title' => $row->log_title,
342 'rc_patrolled' => $row->log_type ==
'upload' ? 0 : 2,
344 'rc_this_oldid' => 0,
345 'rc_last_oldid' => 0,
348 'rc_cur_id' => $field->isNullable()
350 : (int)$row->log_page,
351 'rc_log_type' => $row->log_type,
352 'rc_log_action' => $row->log_action,
353 'rc_logid' => $row->log_id,
354 'rc_params' => $row->log_params,
355 'rc_deleted' => $row->log_deleted
356 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ) )
357 ->caller( __METHOD__ )->
execute();
359 $rcid = $dbw->insertId();
360 $dbw->newUpdateQueryBuilder()
361 ->update(
'change_tag' )
362 ->set( [
'ct_rc_id' => $rcid ] )
363 ->where( [
'ct_log_id' => $row->log_id ] )
364 ->caller( __METHOD__ )->execute();
380 private function findRcIdsWithGroups( $db, $groups, $conds = [] ) {
381 if ( !count( $groups ) ) {
384 return $db->newSelectQueryBuilder()
387 ->from(
'recentchanges' )
388 ->join(
'actor',
null,
'actor_id=rc_actor' )
389 ->join(
'user_groups',
null,
'ug_user=actor_user' )
392 $db->expr(
'rc_timestamp',
'>', $db->timestamp( $this->cutoffFrom ) ),
393 $db->expr(
'rc_timestamp',
'<', $db->timestamp( $this->cutoffTo ) ),
394 'ug_group' => $groups
396 ->caller( __METHOD__ )->fetchFieldValues();
402 private function rebuildRecentChangesTablePass4() {
407 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
408 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
409 # may be lost at this point (aside from joining on the patrol log table entries).
410 $botgroups = [
'bot' ];
413 ->getGroupsWithPermission(
'autopatrol' ) : [];
415 # Flag our recent bot edits
418 $this->
output(
"Flagging bot account edits...\n" );
420 # Fill in the rc_bot field
421 $rcids = $this->findRcIdsWithGroups( $dbw, $botgroups );
423 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
424 $dbw->newUpdateQueryBuilder()
425 ->update(
'recentchanges' )
426 ->set( [
'rc_bot' => 1 ] )
427 ->where( [
'rc_id' => $rcidBatch ] )
428 ->caller( __METHOD__ )->execute();
433 # Flag our recent autopatrolled edits
434 if ( !$wgMiserMode && $autopatrolgroups ) {
435 $this->
output(
"Flagging auto-patrolled edits...\n" );
437 $conds = [
'rc_patrolled' => 0 ];
441 $subConds[] = $dbw->expr(
'rc_source',
'=', RecentChange::SRC_NEW );
444 $subConds[] = $dbw->expr(
'rc_log_type',
'=',
'upload' );
446 $conds[] = $dbw->makeList( $subConds, IDatabase::LIST_OR );
449 $rcids = $this->findRcIdsWithGroups( $dbw, $autopatrolgroups, $conds );
450 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
451 $dbw->newUpdateQueryBuilder()
452 ->update(
'recentchanges' )
453 ->set( [
'rc_patrolled' => 2 ] )
454 ->where( [
'rc_id' => $rcidBatch ] )
455 ->caller( __METHOD__ )->execute();
465 private function rebuildRecentChangesTablePass5() {
468 $this->
output(
"Removing duplicate revision and logging entries...\n" );
470 $res = $dbw->newSelectQueryBuilder()
471 ->select( [
'ls_value',
'ls_log_id' ] )
473 ->join(
'log_search',
null,
'ls_log_id = log_id' )
475 'ls_field' =>
'associated_rev_id',
476 $dbw->expr(
'log_type',
'!=',
'create' ),
477 $dbw->expr(
'log_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
478 $dbw->expr(
'log_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ),
480 ->caller( __METHOD__ )->fetchResultSet();
483 foreach ( $res as $row ) {
484 $rev_id = $row->ls_value;
485 $log_id = $row->ls_log_id;
488 $dbw->newUpdateQueryBuilder()
489 ->update(
'recentchanges' )
490 ->set( [
'rc_this_oldid' => $rev_id ] )
491 ->where( [
'rc_logid' => $log_id ] )
492 ->caller( __METHOD__ )->execute();
495 $dbw->newDeleteQueryBuilder()
496 ->deleteFrom(
'recentchanges' )
497 ->where( [
'rc_this_oldid' => $rev_id,
'rc_logid' => 0 ] )
498 ->caller( __METHOD__ )->execute();
509 private function purgeFeeds() {
512 $this->
output(
"Deleting feed timestamps.\n" );
515 foreach ( $wgFeedClasses as $feed => $className ) {
516 $wanCache->delete( $wanCache->makeKey(
'rcfeed', $feed,
'timestamp' ) ); # Good enough
for now.