46 parent::__construct();
51 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
57 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
69 $this->
fatalError(
"Both 'from' and 'to' must be given, or neither" );
72 $this->rebuildRecentChangesTablePass1();
73 $this->rebuildRecentChangesTablePass2();
74 $this->rebuildRecentChangesTablePass3();
75 $this->rebuildRecentChangesTablePass4();
76 $this->rebuildRecentChangesTablePass5();
80 $this->
output(
"Done.\n" );
86 private function rebuildRecentChangesTablePass1() {
94 $sec = $this->cutoffTo - $this->cutoffFrom;
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->newSelectQueryBuilder()
110 ->from(
'recentchanges' )
111 ->where( $dbw->expr(
'rc_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ) )
112 ->andWhere( $dbw->expr(
'rc_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ) )
113 ->caller( __METHOD__ )->fetchFieldValues();
114 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
115 $dbw->newDeleteQueryBuilder()
116 ->deleteFrom(
'recentchanges' )
117 ->where( [
'rc_id' => $rcidBatch ] )
118 ->caller( __METHOD__ )->execute();
122 $this->
output(
"Loading from page and revision tables...\n" );
124 $res = $dbw->newSelectQueryBuilder()
135 'rev_comment_text' =>
'comment_rev_comment.comment_text',
136 'rev_comment_data' =>
'comment_rev_comment.comment_data',
137 'rev_comment_cid' =>
'comment_rev_comment.comment_id',
138 'rev_user' =>
'actor_rev_user.actor_user',
139 'rev_user_text' =>
'actor_rev_user.actor_name',
140 'rev_actor' =>
'rev_actor',
144 ->join(
'page',
null,
'rev_page=page_id' )
145 ->join(
'comment',
'comment_rev_comment',
'comment_rev_comment.comment_id = rev_comment_id' )
146 ->join(
'actor',
'actor_rev_user',
'actor_rev_user.actor_id = rev_actor' )
149 $dbw->expr(
'rev_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
150 $dbw->expr(
'rev_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) )
153 ->orderBy(
'rev_timestamp', SelectQueryBuilder::SORT_DESC )
154 ->caller( __METHOD__ )->fetchResultSet();
156 $this->
output(
"Inserting from page and revision tables...\n" );
158 foreach ( $res as $row ) {
159 $comment = $commentStore->getComment(
'rev_comment', $row );
160 $dbw->newInsertQueryBuilder()
161 ->insertInto(
'recentchanges' )
163 'rc_timestamp' => $row->rev_timestamp,
164 'rc_actor' => $row->rev_actor,
165 'rc_namespace' => $row->page_namespace,
166 'rc_title' => $row->page_title,
167 'rc_minor' => $row->rev_minor_edit,
169 'rc_new' => $row->page_is_new,
170 'rc_cur_id' => $row->page_id,
171 'rc_this_oldid' => $row->rev_id,
172 'rc_last_oldid' => 0,
175 'rc_deleted' => $row->rev_deleted
176 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ) )
177 ->caller( __METHOD__ )->
execute();
179 $rcid = $dbw->insertId();
180 $dbw->newUpdateQueryBuilder()
181 ->update(
'change_tag' )
182 ->set( [
'ct_rc_id' => $rcid ] )
183 ->where( [
'ct_rev_id' => $row->rev_id ] )
184 ->caller( __METHOD__ )->execute();
196 private function rebuildRecentChangesTablePass2() {
199 $this->
output(
"Updating links and size differences...\n" );
201 # Fill in the rc_last_oldid field, which points to the previous edit
202 $res = $dbw->newSelectQueryBuilder()
203 ->select( [
'rc_cur_id',
'rc_this_oldid',
'rc_timestamp' ] )
204 ->from(
'recentchanges' )
205 ->where( $dbw->expr(
'rc_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ) )
206 ->andWhere( $dbw->expr(
'rc_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ) )
207 ->orderBy( [
'rc_cur_id',
'rc_timestamp' ] )
208 ->caller( __METHOD__ )->fetchResultSet();
214 foreach ( $res as $row ) {
217 if ( $row->rc_cur_id != $lastCurId ) {
218 # Switch! Look up the previous last edit, if any
219 $lastCurId = intval( $row->rc_cur_id );
220 $emit = $row->rc_timestamp;
222 $revRow = $dbw->newSelectQueryBuilder()
223 ->select( [
'rev_id',
'rev_len' ] )
225 ->where( [
'rev_page' => $lastCurId, $dbw->expr(
'rev_timestamp',
'<', $emit ) ] )
226 ->orderBy(
'rev_timestamp DESC' )
227 ->caller( __METHOD__ )->fetchRow();
229 $lastOldId = intval( $revRow->rev_id );
230 # Grab the last text size if available
231 $lastSize = $revRow->rev_len !==
null ? intval( $revRow->rev_len ) : null;
240 if ( $lastCurId == 0 ) {
241 $this->
output(
"Uhhh, something wrong? No curid\n" );
243 # Grab the entry's text size
244 $size = (int)$dbw->newSelectQueryBuilder()
245 ->select(
'rev_len' )
247 ->where( [
'rev_id' => $row->rc_this_oldid ] )
248 ->caller( __METHOD__ )->fetchField();
250 $dbw->newUpdateQueryBuilder()
251 ->update(
'recentchanges' )
253 'rc_last_oldid' => $lastOldId,
257 'rc_old_len' => $lastSize,
258 'rc_new_len' => $size,
261 'rc_cur_id' => $lastCurId,
262 'rc_this_oldid' => $row->rc_this_oldid,
264 'rc_timestamp' => $row->rc_timestamp,
266 ->caller( __METHOD__ )->
execute();
268 $lastOldId = intval( $row->rc_this_oldid );
281 private function rebuildRecentChangesTablePass3() {
286 $nonRCLogs = array_merge(
292 $this->
output(
"Loading from user and logging tables...\n" );
294 $res = $dbw->newSelectQueryBuilder()
307 'log_comment_text' =>
'comment_log_comment.comment_text',
308 'log_comment_data' =>
'comment_log_comment.comment_data',
309 'log_comment_cid' =>
'comment_log_comment.comment_id',
313 ->join(
'comment',
'comment_log_comment',
'comment_log_comment.comment_id = log_comment_id' )
316 $dbw->expr(
'log_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
317 $dbw->expr(
'log_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ),
319 'log_type' => array_diff( LogPage::validTypes(), $nonRCLogs ),
322 ->orderBy( [
'log_timestamp DESC',
'log_id DESC' ] )
323 ->caller( __METHOD__ )->fetchResultSet();
325 $field = $dbw->fieldInfo(
'recentchanges',
'rc_cur_id' );
328 foreach ( $res as $row ) {
329 $comment = $commentStore->getComment(
'log_comment', $row );
330 $dbw->newInsertQueryBuilder()
331 ->insertInto(
'recentchanges' )
333 'rc_timestamp' => $row->log_timestamp,
334 'rc_actor' => $row->log_actor,
335 'rc_namespace' => $row->log_namespace,
336 'rc_title' => $row->log_title,
339 'rc_patrolled' => $row->log_type ==
'upload' ? 0 : 2,
341 'rc_this_oldid' => 0,
342 'rc_last_oldid' => 0,
345 'rc_cur_id' => $field->isNullable()
347 : (int)$row->log_page,
348 'rc_log_type' => $row->log_type,
349 'rc_log_action' => $row->log_action,
350 'rc_logid' => $row->log_id,
351 'rc_params' => $row->log_params,
352 'rc_deleted' => $row->log_deleted
353 ] + $commentStore->insert( $dbw,
'rc_comment', $comment ) )
354 ->caller( __METHOD__ )->
execute();
356 $rcid = $dbw->insertId();
357 $dbw->newUpdateQueryBuilder()
358 ->update(
'change_tag' )
359 ->set( [
'ct_rc_id' => $rcid ] )
360 ->where( [
'ct_log_id' => $row->log_id ] )
361 ->caller( __METHOD__ )->execute();
377 private function findRcIdsWithGroups( $db, $groups, $conds = [] ) {
378 if ( !count( $groups ) ) {
381 return $db->newSelectQueryBuilder()
384 ->from(
'recentchanges' )
385 ->join(
'actor',
null,
'actor_id=rc_actor' )
386 ->join(
'user_groups',
null,
'ug_user=actor_user' )
389 $db->expr(
'rc_timestamp',
'>', $db->timestamp( $this->cutoffFrom ) ),
390 $db->expr(
'rc_timestamp',
'<', $db->timestamp( $this->cutoffTo ) ),
391 'ug_group' => $groups
393 ->caller( __METHOD__ )->fetchFieldValues();
399 private function rebuildRecentChangesTablePass4() {
404 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
405 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
406 # may be lost at this point (aside from joining on the patrol log table entries).
407 $botgroups = [
'bot' ];
410 ->getGroupsWithPermission(
'autopatrol' ) : [];
412 # Flag our recent bot edits
415 $this->
output(
"Flagging bot account edits...\n" );
417 # Fill in the rc_bot field
418 $rcids = $this->findRcIdsWithGroups( $dbw, $botgroups );
420 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
421 $dbw->newUpdateQueryBuilder()
422 ->update(
'recentchanges' )
423 ->set( [
'rc_bot' => 1 ] )
424 ->where( [
'rc_id' => $rcidBatch ] )
425 ->caller( __METHOD__ )->execute();
430 # Flag our recent autopatrolled edits
431 if ( !$wgMiserMode && $autopatrolgroups ) {
432 $this->
output(
"Flagging auto-patrolled edits...\n" );
434 $conds = [
'rc_patrolled' => 0 ];
438 $subConds[] = $dbw->expr(
'rc_source',
'=', RecentChange::SRC_NEW );
441 $subConds[] = $dbw->expr(
'rc_log_type',
'=',
'upload' );
443 $conds[] = $dbw->makeList( $subConds, IDatabase::LIST_OR );
446 $rcids = $this->findRcIdsWithGroups( $dbw, $autopatrolgroups, $conds );
447 foreach ( array_chunk( $rcids, $this->
getBatchSize() ) as $rcidBatch ) {
448 $dbw->newUpdateQueryBuilder()
449 ->update(
'recentchanges' )
450 ->set( [
'rc_patrolled' => 2 ] )
451 ->where( [
'rc_id' => $rcidBatch ] )
452 ->caller( __METHOD__ )->execute();
462 private function rebuildRecentChangesTablePass5() {
465 $this->
output(
"Removing duplicate revision and logging entries...\n" );
467 $res = $dbw->newSelectQueryBuilder()
468 ->select( [
'ls_value',
'ls_log_id' ] )
470 ->join(
'log_search',
null,
'ls_log_id = log_id' )
472 'ls_field' =>
'associated_rev_id',
473 $dbw->expr(
'log_type',
'!=',
'create' ),
474 $dbw->expr(
'log_timestamp',
'>', $dbw->timestamp( $this->cutoffFrom ) ),
475 $dbw->expr(
'log_timestamp',
'<', $dbw->timestamp( $this->cutoffTo ) ),
477 ->caller( __METHOD__ )->fetchResultSet();
480 foreach ( $res as $row ) {
481 $rev_id = $row->ls_value;
482 $log_id = $row->ls_log_id;
485 $dbw->newUpdateQueryBuilder()
486 ->update(
'recentchanges' )
487 ->set( [
'rc_this_oldid' => $rev_id ] )
488 ->where( [
'rc_logid' => $log_id ] )
489 ->caller( __METHOD__ )->execute();
492 $dbw->newDeleteQueryBuilder()
493 ->deleteFrom(
'recentchanges' )
494 ->where( [
'rc_this_oldid' => $rev_id,
'rc_logid' => 0 ] )
495 ->caller( __METHOD__ )->execute();
506 private function purgeFeeds() {
509 $this->
output(
"Deleting feed timestamps.\n" );
512 foreach ( $wgFeedClasses as $feed => $className ) {
513 $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...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.