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 $this->rebuildRecentChangesTablePass1();
70 $this->rebuildRecentChangesTablePass2();
71 $this->rebuildRecentChangesTablePass3();
72 $this->rebuildRecentChangesTablePass4();
73 $this->rebuildRecentChangesTablePass5();
77 $this->
output(
"Done.\n" );
83 private function rebuildRecentChangesTablePass1() {
91 $sec = $this->cutoffTo - $this->cutoffFrom;
92 $days = $sec / 24 / 3600;
93 $this->
output(
"Rebuilding range of $sec seconds ($days days)\n" );
98 $this->
output(
"Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
101 $this->cutoffTo = time();
104 $this->
output(
"Clearing recentchanges table for time range...\n" );
105 $rcids = $dbw->newSelectQueryBuilder()
107 ->from(
'recentchanges' )
108 ->where( $dbw->expr(
'rc_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ) )
109 ->andWhere( $dbw->expr(
'rc_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ) )
110 ->caller( __METHOD__ )->fetchFieldValues();
111 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
112 $dbw->newDeleteQueryBuilder()
113 ->deleteFrom(
'recentchanges' )
114 ->where( [
'rc_id' => $rcidBatch ] )
115 ->caller( __METHOD__ )->execute();
119 $this->
output(
"Loading from page and revision tables...\n" );
121 $res = $dbw->newSelectQueryBuilder()
132 'rev_comment_text' =>
'comment_rev_comment.comment_text',
133 'rev_comment_data' =>
'comment_rev_comment.comment_data',
134 'rev_comment_cid' =>
'comment_rev_comment.comment_id',
135 'rev_user' =>
'actor_rev_user.actor_user',
136 'rev_user_text' =>
'actor_rev_user.actor_name',
137 'rev_actor' =>
'rev_actor',
141 ->join(
'page',
null,
'rev_page=page_id' )
142 ->join(
'comment',
'comment_rev_comment',
'comment_rev_comment.comment_id = rev_comment_id' )
143 ->join(
'actor',
'actor_rev_user',
'actor_rev_user.actor_id = rev_actor' )
146 $dbw->expr(
'rev_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
147 $dbw->expr(
'rev_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) )
150 ->orderBy(
'rev_timestamp', SelectQueryBuilder::SORT_DESC )
151 ->caller( __METHOD__ )->fetchResultSet();
153 $this->
output(
"Inserting from page and revision tables...\n" );
155 foreach ( $res as $row ) {
156 $comment = $commentStore->getComment(
'rev_comment', $row );
157 $dbw->newInsertQueryBuilder()
158 ->insertInto(
'recentchanges' )
160 'rc_timestamp' => $row->rev_timestamp,
161 'rc_actor' => $row->rev_actor,
162 'rc_namespace' => $row->page_namespace,
163 'rc_title' => $row->page_title,
164 'rc_minor' => $row->rev_minor_edit,
166 'rc_new' => $row->page_is_new,
167 'rc_cur_id' => $row->page_id,
168 'rc_this_oldid' => $row->rev_id,
169 'rc_last_oldid' => 0,
172 'rc_deleted' => $row->rev_deleted
173 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ) )
174 ->caller( __METHOD__ )->
execute();
176 $rcid = $dbw->insertId();
177 $dbw->newUpdateQueryBuilder()
178 ->update(
'change_tag' )
179 ->set( [
'ct_rc_id' => $rcid ] )
180 ->where( [
'ct_rev_id' => $row->rev_id ] )
181 ->caller( __METHOD__ )->execute();
193 private function rebuildRecentChangesTablePass2() {
196 $this->
output(
"Updating links and size differences...\n" );
198 # Fill in the rc_last_oldid field, which points to the previous edit
199 $res = $dbw->newSelectQueryBuilder()
200 ->select( [
'rc_cur_id',
'rc_this_oldid',
'rc_timestamp' ] )
201 ->from(
'recentchanges' )
202 ->where( $dbw->expr(
'rc_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ) )
203 ->andWhere( $dbw->expr(
'rc_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ) )
204 ->orderBy( [
'rc_cur_id',
'rc_timestamp' ] )
205 ->caller( __METHOD__ )->fetchResultSet();
211 foreach ( $res as $row ) {
214 if ( $row->rc_cur_id != $lastCurId ) {
215 # Switch! Look up the previous last edit, if any
216 $lastCurId = intval( $row->rc_cur_id );
217 $emit = $row->rc_timestamp;
219 $revRow = $dbw->newSelectQueryBuilder()
220 ->select( [
'rev_id',
'rev_len' ] )
222 ->where( [
'rev_page' => $lastCurId, $dbw->expr(
'rev_timestamp',
'<', $emit ) ] )
223 ->orderBy(
'rev_timestamp DESC' )
224 ->caller( __METHOD__ )->fetchRow();
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->newSelectQueryBuilder()
242 ->select(
'rev_len' )
244 ->where( [
'rev_id' => $row->rc_this_oldid ] )
245 ->caller( __METHOD__ )->fetchField();
247 $dbw->newUpdateQueryBuilder()
248 ->update(
'recentchanges' )
250 'rc_last_oldid' => $lastOldId,
254 'rc_old_len' => $lastSize,
255 'rc_new_len' => $size,
258 'rc_cur_id' => $lastCurId,
259 'rc_this_oldid' => $row->rc_this_oldid,
261 'rc_timestamp' => $row->rc_timestamp,
263 ->caller( __METHOD__ )->
execute();
265 $lastOldId = intval( $row->rc_this_oldid );
278 private function rebuildRecentChangesTablePass3() {
283 $nonRCLogs = array_merge(
289 $this->
output(
"Loading from user and logging tables...\n" );
291 $res = $dbw->newSelectQueryBuilder()
304 'log_comment_text' =>
'comment_log_comment.comment_text',
305 'log_comment_data' =>
'comment_log_comment.comment_data',
306 'log_comment_cid' =>
'comment_log_comment.comment_id',
310 ->join(
'comment',
'comment_log_comment',
'comment_log_comment.comment_id = log_comment_id' )
313 $dbw->expr(
'log_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
314 $dbw->expr(
'log_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ),
316 'log_type' => array_diff( LogPage::validTypes(), $nonRCLogs ),
319 ->orderBy( [
'log_timestamp DESC',
'log_id DESC' ] )
320 ->caller( __METHOD__ )->fetchResultSet();
322 $field = $dbw->fieldInfo(
'recentchanges',
'rc_cur_id' );
325 foreach ( $res as $row ) {
326 $comment = $commentStore->getComment(
'log_comment', $row );
327 $dbw->newInsertQueryBuilder()
328 ->insertInto(
'recentchanges' )
330 'rc_timestamp' => $row->log_timestamp,
331 'rc_actor' => $row->log_actor,
332 'rc_namespace' => $row->log_namespace,
333 'rc_title' => $row->log_title,
336 'rc_patrolled' => $row->log_type ==
'upload' ? 0 : 2,
338 'rc_this_oldid' => 0,
339 'rc_last_oldid' => 0,
342 'rc_cur_id' => $field->isNullable()
344 : (int)$row->log_page,
345 'rc_log_type' => $row->log_type,
346 'rc_log_action' => $row->log_action,
347 'rc_logid' => $row->log_id,
348 'rc_params' => $row->log_params,
349 'rc_deleted' => $row->log_deleted
350 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ) )
351 ->caller( __METHOD__ )->
execute();
353 $rcid = $dbw->insertId();
354 $dbw->newUpdateQueryBuilder()
355 ->update(
'change_tag' )
356 ->set( [
'ct_rc_id' => $rcid ] )
357 ->where( [
'ct_log_id' => $row->log_id ] )
358 ->caller( __METHOD__ )->execute();
374 private function findRcIdsWithGroups( $db, $groups, $conds = [] ) {
375 if ( !count( $groups ) ) {
378 return $db->newSelectQueryBuilder()
381 ->from(
'recentchanges' )
382 ->join(
'actor',
null,
'actor_id=rc_actor' )
383 ->join(
'user_groups',
null,
'ug_user=actor_user' )
386 $db->expr(
'rc_timestamp',
'>', $db->timestamp( $this->cutoffFrom ) ),
387 $db->expr(
'rc_timestamp',
'<', $db->timestamp( $this->cutoffTo ) ),
388 'ug_group' => $groups
390 ->caller( __METHOD__ )->fetchFieldValues();
396 private function rebuildRecentChangesTablePass4() {
401 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
402 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
403 # may be lost at this point (aside from joining on the patrol log table entries).
404 $botgroups = [
'bot' ];
407 ->getGroupsWithPermission(
'autopatrol' ) : [];
409 # Flag our recent bot edits
412 $this->
output(
"Flagging bot account edits...\n" );
414 # Fill in the rc_bot field
415 $rcids = $this->findRcIdsWithGroups( $dbw, $botgroups );
417 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
418 $dbw->newUpdateQueryBuilder()
419 ->update(
'recentchanges' )
420 ->set( [
'rc_bot' => 1 ] )
421 ->where( [
'rc_id' => $rcidBatch ] )
422 ->caller( __METHOD__ )->execute();
427 # Flag our recent autopatrolled edits
428 if ( !$wgMiserMode && $autopatrolgroups ) {
429 $this->
output(
"Flagging auto-patrolled edits...\n" );
431 $conds = [
'rc_patrolled' => 0 ];
435 $subConds[] = $dbw->expr(
'rc_source',
'=', RecentChange::SRC_NEW );
438 $subConds[] = $dbw->expr(
'rc_log_type',
'=',
'upload' );
440 $conds[] = $dbw->makeList( $subConds, IDatabase::LIST_OR );
443 $rcids = $this->findRcIdsWithGroups( $dbw, $autopatrolgroups, $conds );
444 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
445 $dbw->newUpdateQueryBuilder()
446 ->update(
'recentchanges' )
447 ->set( [
'rc_patrolled' => 2 ] )
448 ->where( [
'rc_id' => $rcidBatch ] )
449 ->caller( __METHOD__ )->execute();
459 private function rebuildRecentChangesTablePass5() {
462 $this->
output(
"Removing duplicate revision and logging entries...\n" );
464 $res = $dbw->newSelectQueryBuilder()
465 ->select( [
'ls_value',
'ls_log_id' ] )
467 ->join(
'log_search',
null,
'ls_log_id = log_id' )
469 'ls_field' =>
'associated_rev_id',
470 $dbw->expr(
'log_type',
'!=',
'create' ),
471 $dbw->expr(
'log_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
472 $dbw->expr(
'log_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ),
474 ->caller( __METHOD__ )->fetchResultSet();
477 foreach ( $res as $row ) {
478 $rev_id = $row->ls_value;
479 $log_id = $row->ls_log_id;
482 $dbw->newUpdateQueryBuilder()
483 ->update(
'recentchanges' )
484 ->set( [
'rc_this_oldid' => $rev_id ] )
485 ->where( [
'rc_logid' => $log_id ] )
486 ->caller( __METHOD__ )->execute();
489 $dbw->newDeleteQueryBuilder()
490 ->deleteFrom(
'recentchanges' )
491 ->where( [
'rc_this_oldid' => $rev_id,
'rc_logid' => 0 ] )
492 ->caller( __METHOD__ )->execute();
503 private function purgeFeeds() {
506 $this->
output(
"Deleting feed timestamps.\n" );
509 foreach ( $wgFeedClasses as $feed => $className ) {
510 $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...
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.