36 parent::__construct();
41 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
47 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
59 $this->
fatalError(
"Both 'from' and 'to' must be given, or neither" );
62 $this->rebuildRecentChangesTablePass1();
63 $this->rebuildRecentChangesTablePass2();
64 $this->rebuildRecentChangesTablePass3();
65 $this->rebuildRecentChangesTablePass4();
66 $this->rebuildRecentChangesTablePass5();
70 $this->
output(
"Done.\n" );
76 private function rebuildRecentChangesTablePass1() {
84 $sec = $this->cutoffTo - $this->cutoffFrom;
85 $days = $sec / 24 / 3600;
86 $this->
output(
"Rebuilding range of $sec seconds ($days days)\n" );
91 $this->
output(
"Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
94 $this->cutoffTo = time();
97 $this->
output(
"Clearing recentchanges table for time range...\n" );
98 $rcids = $dbw->newSelectQueryBuilder()
100 ->from(
'recentchanges' )
101 ->where( $dbw->expr(
'rc_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ) )
102 ->andWhere( $dbw->expr(
'rc_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ) )
103 ->caller( __METHOD__ )->fetchFieldValues();
104 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
105 $dbw->newDeleteQueryBuilder()
106 ->deleteFrom(
'recentchanges' )
107 ->where( [
'rc_id' => $rcidBatch ] )
108 ->caller( __METHOD__ )->execute();
112 $this->
output(
"Loading from page and revision tables...\n" );
114 $res = $dbw->newSelectQueryBuilder()
125 'rev_comment_text' =>
'comment_rev_comment.comment_text',
126 'rev_comment_data' =>
'comment_rev_comment.comment_data',
127 'rev_comment_cid' =>
'comment_rev_comment.comment_id',
128 'rev_user' =>
'actor_rev_user.actor_user',
129 'rev_user_text' =>
'actor_rev_user.actor_name',
130 'rev_actor' =>
'rev_actor',
134 ->join(
'page',
null,
'rev_page=page_id' )
135 ->join(
'comment',
'comment_rev_comment',
'comment_rev_comment.comment_id = rev_comment_id' )
136 ->join(
'actor',
'actor_rev_user',
'actor_rev_user.actor_id = rev_actor' )
139 $dbw->expr(
'rev_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
140 $dbw->expr(
'rev_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) )
143 ->orderBy(
'rev_timestamp', SelectQueryBuilder::SORT_DESC )
144 ->caller( __METHOD__ )->fetchResultSet();
146 $this->
output(
"Inserting from page and revision tables...\n" );
148 foreach ( $res as $row ) {
149 $comment = $commentStore->getComment(
'rev_comment', $row );
150 $dbw->newInsertQueryBuilder()
151 ->insertInto(
'recentchanges' )
153 'rc_timestamp' => $row->rev_timestamp,
154 'rc_actor' => $row->rev_actor,
155 'rc_namespace' => $row->page_namespace,
156 'rc_title' => $row->page_title,
157 'rc_minor' => $row->rev_minor_edit,
159 'rc_cur_id' => $row->page_id,
160 'rc_this_oldid' => $row->rev_id,
161 'rc_last_oldid' => 0,
162 'rc_source' => $row->page_is_new ? RecentChange::SRC_NEW :
RecentChange::SRC_EDIT,
163 'rc_deleted' => $row->rev_deleted
164 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ) )
165 ->caller( __METHOD__ )->
execute();
167 $rcid = $dbw->insertId();
168 $dbw->newUpdateQueryBuilder()
169 ->update(
'change_tag' )
170 ->set( [
'ct_rc_id' => $rcid ] )
171 ->where( [
'ct_rev_id' => $row->rev_id ] )
172 ->caller( __METHOD__ )->execute();
184 private function rebuildRecentChangesTablePass2() {
187 $this->
output(
"Updating links and size differences...\n" );
189 # Fill in the rc_last_oldid field, which points to the previous edit
190 $res = $dbw->newSelectQueryBuilder()
191 ->select( [
'rc_cur_id',
'rc_this_oldid',
'rc_timestamp' ] )
192 ->from(
'recentchanges' )
193 ->where( $dbw->expr(
'rc_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ) )
194 ->andWhere( $dbw->expr(
'rc_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ) )
195 ->orderBy( [
'rc_cur_id',
'rc_timestamp' ] )
196 ->caller( __METHOD__ )->fetchResultSet();
202 foreach ( $res as $row ) {
205 if ( $row->rc_cur_id != $lastCurId ) {
206 # Switch! Look up the previous last edit, if any
207 $lastCurId = intval( $row->rc_cur_id );
208 $emit = $row->rc_timestamp;
210 $revRow = $dbw->newSelectQueryBuilder()
211 ->select( [
'rev_id',
'rev_len' ] )
213 ->where( [
'rev_page' => $lastCurId, $dbw->expr(
'rev_timestamp',
'<', $emit ) ] )
214 ->orderBy(
'rev_timestamp DESC' )
215 ->caller( __METHOD__ )->fetchRow();
217 $lastOldId = intval( $revRow->rev_id );
218 # Grab the last text size if available
219 $lastSize = $revRow->rev_len !==
null ? intval( $revRow->rev_len ) : null;
228 if ( $lastCurId == 0 ) {
229 $this->
output(
"Uhhh, something wrong? No curid\n" );
231 # Grab the entry's text size
232 $size = (int)$dbw->newSelectQueryBuilder()
233 ->select(
'rev_len' )
235 ->where( [
'rev_id' => $row->rc_this_oldid ] )
236 ->caller( __METHOD__ )->fetchField();
238 $dbw->newUpdateQueryBuilder()
239 ->update(
'recentchanges' )
241 'rc_last_oldid' => $lastOldId,
242 'rc_source' => $new === 1 ? RecentChange::SRC_NEW :
RecentChange::SRC_EDIT,
243 'rc_old_len' => $lastSize,
244 'rc_new_len' => $size,
247 'rc_cur_id' => $lastCurId,
248 'rc_this_oldid' => $row->rc_this_oldid,
250 'rc_timestamp' => $row->rc_timestamp,
252 ->caller( __METHOD__ )->
execute();
254 $lastOldId = intval( $row->rc_this_oldid );
267 private function rebuildRecentChangesTablePass3() {
272 $nonRCLogs = array_merge(
278 $this->
output(
"Loading from user and logging tables...\n" );
280 $res = $dbw->newSelectQueryBuilder()
293 'log_comment_text' =>
'comment_log_comment.comment_text',
294 'log_comment_data' =>
'comment_log_comment.comment_data',
295 'log_comment_cid' =>
'comment_log_comment.comment_id',
299 ->join(
'comment',
'comment_log_comment',
'comment_log_comment.comment_id = log_comment_id' )
302 $dbw->expr(
'log_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
303 $dbw->expr(
'log_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ),
305 'log_type' => array_diff( LogPage::validTypes(), $nonRCLogs ),
308 ->orderBy( [
'log_timestamp DESC',
'log_id DESC' ] )
309 ->caller( __METHOD__ )->fetchResultSet();
311 $field = $dbw->fieldInfo(
'recentchanges',
'rc_cur_id' );
314 foreach ( $res as $row ) {
315 $comment = $commentStore->getComment(
'log_comment', $row );
316 $dbw->newInsertQueryBuilder()
317 ->insertInto(
'recentchanges' )
319 'rc_timestamp' => $row->log_timestamp,
320 'rc_actor' => $row->log_actor,
321 'rc_namespace' => $row->log_namespace,
322 'rc_title' => $row->log_title,
325 'rc_patrolled' => $row->log_type ==
'upload' ? 0 : 2,
326 'rc_this_oldid' => 0,
327 'rc_last_oldid' => 0,
329 'rc_cur_id' => $field->isNullable()
331 : (int)$row->log_page,
332 'rc_log_type' => $row->log_type,
333 'rc_log_action' => $row->log_action,
334 'rc_logid' => $row->log_id,
335 'rc_params' => $row->log_params,
336 'rc_deleted' => $row->log_deleted
337 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ) )
338 ->caller( __METHOD__ )->
execute();
340 $rcid = $dbw->insertId();
341 $dbw->newUpdateQueryBuilder()
342 ->update(
'change_tag' )
343 ->set( [
'ct_rc_id' => $rcid ] )
344 ->where( [
'ct_log_id' => $row->log_id ] )
345 ->caller( __METHOD__ )->execute();
361 private function findRcIdsWithGroups( $db, $groups, $conds = [] ) {
362 if ( !count( $groups ) ) {
365 return $db->newSelectQueryBuilder()
368 ->from(
'recentchanges' )
369 ->join(
'actor',
null,
'actor_id=rc_actor' )
370 ->join(
'user_groups',
null,
'ug_user=actor_user' )
373 $db->expr(
'rc_timestamp',
'>', $db->timestamp( $this->cutoffFrom ) ),
374 $db->expr(
'rc_timestamp',
'<', $db->timestamp( $this->cutoffTo ) ),
375 'ug_group' => $groups
377 ->caller( __METHOD__ )->fetchFieldValues();
383 private function rebuildRecentChangesTablePass4() {
388 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
389 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
390 # may be lost at this point (aside from joining on the patrol log table entries).
391 $botgroups = [
'bot' ];
394 ->getGroupsWithPermission(
'autopatrol' ) : [];
396 # Flag our recent bot edits
399 $this->
output(
"Flagging bot account edits...\n" );
401 # Fill in the rc_bot field
402 $rcids = $this->findRcIdsWithGroups( $dbw, $botgroups );
404 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
405 $dbw->newUpdateQueryBuilder()
406 ->update(
'recentchanges' )
407 ->set( [
'rc_bot' => 1 ] )
408 ->where( [
'rc_id' => $rcidBatch ] )
409 ->caller( __METHOD__ )->execute();
414 # Flag our recent autopatrolled edits
415 if ( !$wgMiserMode && $autopatrolgroups ) {
416 $this->
output(
"Flagging auto-patrolled edits...\n" );
418 $conds = [
'rc_patrolled' => 0 ];
422 $subConds[] = $dbw->expr(
'rc_source',
'=', RecentChange::SRC_NEW );
425 $subConds[] = $dbw->expr(
'rc_log_type',
'=',
'upload' );
427 $conds[] = $dbw->makeList( $subConds, IDatabase::LIST_OR );
430 $rcids = $this->findRcIdsWithGroups( $dbw, $autopatrolgroups, $conds );
431 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
432 $dbw->newUpdateQueryBuilder()
433 ->update(
'recentchanges' )
434 ->set( [
'rc_patrolled' => 2 ] )
435 ->where( [
'rc_id' => $rcidBatch ] )
436 ->caller( __METHOD__ )->execute();
446 private function rebuildRecentChangesTablePass5() {
449 $this->
output(
"Removing duplicate revision and logging entries...\n" );
451 $res = $dbw->newSelectQueryBuilder()
452 ->select( [
'ls_value',
'ls_log_id' ] )
454 ->join(
'log_search',
null,
'ls_log_id = log_id' )
456 'ls_field' =>
'associated_rev_id',
457 $dbw->expr(
'log_type',
'!=',
'create' ),
458 $dbw->expr(
'log_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
459 $dbw->expr(
'log_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ),
461 ->caller( __METHOD__ )->fetchResultSet();
464 foreach ( $res as $row ) {
465 $rev_id = $row->ls_value;
466 $log_id = $row->ls_log_id;
469 $dbw->newUpdateQueryBuilder()
470 ->update(
'recentchanges' )
471 ->set( [
'rc_this_oldid' => $rev_id ] )
472 ->where( [
'rc_logid' => $log_id ] )
473 ->caller( __METHOD__ )->execute();
476 $dbw->newDeleteQueryBuilder()
477 ->deleteFrom(
'recentchanges' )
478 ->where( [
'rc_this_oldid' => $rev_id,
'rc_logid' => 0 ] )
479 ->caller( __METHOD__ )->execute();
490 private function purgeFeeds() {
493 $this->
output(
"Deleting feed timestamps.\n" );
496 foreach ( $wgFeedClasses as $feed => $className ) {
497 $wanCache->delete( $wanCache->makeKey(
'rcfeed', $feed,
'timestamp' ) ); # Good enough
for now.