MediaWiki REL1_41
rebuildrecentchanges.php
Go to the documentation of this file.
1<?php
26require_once __DIR__ . '/Maintenance.php';
27
30
38 private $cutoffFrom;
40 private $cutoffTo;
41
42 public function __construct() {
43 parent::__construct();
44 $this->addDescription( 'Rebuild recent changes' );
45
46 $this->addOption(
47 'from',
48 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
49 false,
50 true
51 );
52 $this->addOption(
53 'to',
54 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
55 false,
56 true
57 );
58 $this->setBatchSize( 200 );
59 }
60
61 public function execute() {
62 if (
63 ( $this->hasOption( 'from' ) && !$this->hasOption( 'to' ) ) ||
64 ( !$this->hasOption( 'from' ) && $this->hasOption( 'to' ) )
65 ) {
66 $this->fatalError( "Both 'from' and 'to' must be given, or neither" );
67 }
68
69 $this->rebuildRecentChangesTablePass1();
70 $this->rebuildRecentChangesTablePass2();
71 $this->rebuildRecentChangesTablePass3();
72 $this->rebuildRecentChangesTablePass4();
73 $this->rebuildRecentChangesTablePass5();
74 if ( !( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) ) {
75 $this->purgeFeeds();
76 }
77 $this->output( "Done.\n" );
78 }
79
83 private function rebuildRecentChangesTablePass1() {
84 $dbw = $this->getDB( DB_PRIMARY );
85 $commentStore = $this->getServiceContainer()->getCommentStore();
86
87 if ( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) {
88 $this->cutoffFrom = (int)wfTimestamp( TS_UNIX, $this->getOption( 'from' ) );
89 $this->cutoffTo = (int)wfTimestamp( TS_UNIX, $this->getOption( 'to' ) );
90
91 $sec = $this->cutoffTo - $this->cutoffFrom;
92 $days = $sec / 24 / 3600;
93 $this->output( "Rebuilding range of $sec seconds ($days days)\n" );
94 } else {
95 global $wgRCMaxAge;
96
97 $days = $wgRCMaxAge / 24 / 3600;
98 $this->output( "Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
99
100 $this->cutoffFrom = time() - $wgRCMaxAge;
101 $this->cutoffTo = time();
102 }
103
104 $this->output( "Clearing recentchanges table for time range...\n" );
105 $rcids = $dbw->newSelectQueryBuilder()
106 ->select( 'rc_id' )
107 ->from( 'recentchanges' )
108 ->where( [ 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ) ] )
109 ->andWhere( [ 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ) ] )
110 ->caller( __METHOD__ )->fetchFieldValues();
111 foreach ( array_chunk( $rcids, $this->getBatchSize() ) as $rcidBatch ) {
112 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcidBatch ], __METHOD__ );
113 $this->waitForReplication();
114 }
115
116 $this->output( "Loading from page and revision tables...\n" );
117
118 $commentQuery = $commentStore->getJoin( 'rev_comment' );
119 $actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' );
120 $res = $dbw->select(
121 [ 'revision', 'page' ] + $commentQuery['tables'] + $actorQuery['tables'],
122 [
123 'rev_timestamp',
124 'rev_minor_edit',
125 'rev_id',
126 'rev_deleted',
127 'page_namespace',
128 'page_title',
129 'page_is_new',
130 'page_id'
131 ] + $commentQuery['fields'] + $actorQuery['fields'],
132 [
133 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
134 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
135 ],
136 __METHOD__,
137 [ 'ORDER BY' => 'rev_timestamp DESC' ],
138 [
139 'page' => [ 'JOIN', 'rev_page=page_id' ],
140 ] + $commentQuery['joins'] + $actorQuery['joins']
141 );
142
143 $this->output( "Inserting from page and revision tables...\n" );
144 $inserted = 0;
145 foreach ( $res as $row ) {
146 $comment = $commentStore->getComment( 'rev_comment', $row );
147 $dbw->insert(
148 'recentchanges',
149 [
150 'rc_timestamp' => $row->rev_timestamp,
151 'rc_actor' => $row->rev_actor,
152 'rc_namespace' => $row->page_namespace,
153 'rc_title' => $row->page_title,
154 'rc_minor' => $row->rev_minor_edit,
155 'rc_bot' => 0,
156 'rc_new' => $row->page_is_new,
157 'rc_cur_id' => $row->page_id,
158 'rc_this_oldid' => $row->rev_id,
159 'rc_last_oldid' => 0, // is this ok?
160 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
161 'rc_source' => $row->page_is_new ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT,
162 'rc_deleted' => $row->rev_deleted
163 ] + $commentStore->insert( $dbw, 'rc_comment', $comment ),
164 __METHOD__
165 );
166
167 $rcid = $dbw->insertId();
168 $dbw->update(
169 'change_tag',
170 [ 'ct_rc_id' => $rcid ],
171 [ 'ct_rev_id' => $row->rev_id ],
172 __METHOD__
173 );
174
175 if ( ( ++$inserted % $this->getBatchSize() ) == 0 ) {
176 $this->waitForReplication();
177 }
178 }
179 }
180
185 private function rebuildRecentChangesTablePass2() {
186 $dbw = $this->getDB( DB_PRIMARY );
187
188 $this->output( "Updating links and size differences...\n" );
189
190 # Fill in the rc_last_oldid field, which points to the previous edit
191 $res = $dbw->newSelectQueryBuilder()
192 ->select( [ 'rc_cur_id', 'rc_this_oldid', 'rc_timestamp' ] )
193 ->from( 'recentchanges' )
194 ->where( [ "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ) ] )
195 ->andWhere( [ "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ) ] )
196 ->orderBy( [ 'rc_cur_id', 'rc_timestamp' ] )
197 ->caller( __METHOD__ )->fetchResultSet();
198
199 $lastCurId = 0;
200 $lastOldId = 0;
201 $lastSize = null;
202 $updated = 0;
203 foreach ( $res as $row ) {
204 $new = 0;
205
206 if ( $row->rc_cur_id != $lastCurId ) {
207 # Switch! Look up the previous last edit, if any
208 $lastCurId = intval( $row->rc_cur_id );
209 $emit = $row->rc_timestamp;
210
211 $revRow = $dbw->newSelectQueryBuilder()
212 ->select( [ 'rev_id', 'rev_len' ] )
213 ->from( 'revision' )
214 ->where( [ 'rev_page' => $lastCurId, "rev_timestamp < " . $dbw->addQuotes( $emit ) ] )
215 ->orderBy( 'rev_timestamp DESC' )
216 ->caller( __METHOD__ )->fetchRow();
217 if ( $revRow ) {
218 $lastOldId = intval( $revRow->rev_id );
219 # Grab the last text size if available
220 $lastSize = $revRow->rev_len !== null ? intval( $revRow->rev_len ) : null;
221 } else {
222 # No previous edit
223 $lastOldId = 0;
224 $lastSize = 0;
225 $new = 1; // probably true
226 }
227 }
228
229 if ( $lastCurId == 0 ) {
230 $this->output( "Uhhh, something wrong? No curid\n" );
231 } else {
232 # Grab the entry's text size
233 $size = (int)$dbw->newSelectQueryBuilder()
234 ->select( 'rev_len' )
235 ->from( 'revision' )
236 ->where( [ 'rev_id' => $row->rc_this_oldid ] )
237 ->caller( __METHOD__ )->fetchField();
238
239 $dbw->update(
240 'recentchanges',
241 [
242 'rc_last_oldid' => $lastOldId,
243 'rc_new' => $new,
244 'rc_type' => $new ? RC_NEW : RC_EDIT,
245 'rc_source' => $new === 1 ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT,
246 'rc_old_len' => $lastSize,
247 'rc_new_len' => $size,
248 ],
249 [
250 'rc_cur_id' => $lastCurId,
251 'rc_this_oldid' => $row->rc_this_oldid,
252 'rc_timestamp' => $row->rc_timestamp // index usage
253 ],
254 __METHOD__
255 );
256
257 $lastOldId = intval( $row->rc_this_oldid );
258 $lastSize = $size;
259
260 if ( ( ++$updated % $this->getBatchSize() ) == 0 ) {
261 $this->waitForReplication();
262 }
263 }
264 }
265 }
266
270 private function rebuildRecentChangesTablePass3() {
272
273 $dbw = $this->getDB( DB_PRIMARY );
274 $commentStore = $this->getServiceContainer()->getCommentStore();
275 $nonRCLogs = array_merge( array_keys( $wgLogRestrictions ),
276 array_keys( $wgFilterLogTypes ),
277 [ 'create' ] );
278
279 $this->output( "Loading from user and logging tables...\n" );
280
281 $commentQuery = $commentStore->getJoin( 'log_comment' );
282 $res = $dbw->select(
283 [ 'logging' ] + $commentQuery['tables'],
284 [
285 'log_timestamp',
286 'log_actor',
287 'log_namespace',
288 'log_title',
289 'log_page',
290 'log_type',
291 'log_action',
292 'log_id',
293 'log_params',
294 'log_deleted'
295 ] + $commentQuery['fields'],
296 [
297 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
298 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
299 // Some logs don't go in RC since they are private, or are included in the filterable log types.
300 'log_type' => array_diff( LogPage::validTypes(), $nonRCLogs ),
301 ],
302 __METHOD__,
303 [ 'ORDER BY' => [ 'log_timestamp DESC', 'log_id DESC' ] ],
304 $commentQuery['joins']
305 );
306
307 $field = $dbw->fieldInfo( 'recentchanges', 'rc_cur_id' );
308
309 $inserted = 0;
310 foreach ( $res as $row ) {
311 $comment = $commentStore->getComment( 'log_comment', $row );
312 $dbw->insert(
313 'recentchanges',
314 [
315 'rc_timestamp' => $row->log_timestamp,
316 'rc_actor' => $row->log_actor,
317 'rc_namespace' => $row->log_namespace,
318 'rc_title' => $row->log_title,
319 'rc_minor' => 0,
320 'rc_bot' => 0,
321 'rc_patrolled' => $row->log_type == 'upload' ? 0 : 2,
322 'rc_new' => 0,
323 'rc_this_oldid' => 0,
324 'rc_last_oldid' => 0,
325 'rc_type' => RC_LOG,
326 'rc_source' => RecentChange::SRC_LOG,
327 'rc_cur_id' => $field->isNullable()
328 ? $row->log_page
329 : (int)$row->log_page, // NULL => 0,
330 'rc_log_type' => $row->log_type,
331 'rc_log_action' => $row->log_action,
332 'rc_logid' => $row->log_id,
333 'rc_params' => $row->log_params,
334 'rc_deleted' => $row->log_deleted
335 ] + $commentStore->insert( $dbw, 'rc_comment', $comment ),
336 __METHOD__
337 );
338
339 $rcid = $dbw->insertId();
340 $dbw->update(
341 'change_tag',
342 [ 'ct_rc_id' => $rcid ],
343 [ 'ct_log_id' => $row->log_id ],
344 __METHOD__
345 );
346
347 if ( ( ++$inserted % $this->getBatchSize() ) == 0 ) {
348 $this->waitForReplication();
349 }
350 }
351 }
352
361 private function findRcIdsWithGroups( $db, $groups, $conds = [] ) {
362 if ( !count( $groups ) ) {
363 return [];
364 }
365 return $db->newSelectQueryBuilder()
366 ->select( 'rc_id' )
367 ->distinct()
368 ->from( 'recentchanges' )
369 ->join( 'actor', null, 'actor_id=rc_actor' )
370 ->join( 'user_groups', null, 'ug_user=actor_user' )
371 ->where( $conds )
372 ->andWhere( [
373 "rc_timestamp > " . $db->addQuotes( $db->timestamp( $this->cutoffFrom ) ),
374 "rc_timestamp < " . $db->addQuotes( $db->timestamp( $this->cutoffTo ) ),
375 'ug_group' => $groups
376 ] )
377 ->caller( __METHOD__ )->fetchFieldValues();
378 }
379
383 private function rebuildRecentChangesTablePass4() {
385
386 $dbw = $this->getDB( DB_PRIMARY );
387
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' ];
392 $autopatrolgroups = ( $wgUseRCPatrol || $wgUseNPPatrol || $wgUseFilePatrol ) ?
393 $this->getServiceContainer()->getGroupPermissionsLookup()
394 ->getGroupsWithPermission( 'autopatrol' ) : [];
395
396 # Flag our recent bot edits
397 // @phan-suppress-next-line PhanRedundantCondition
398 if ( $botgroups ) {
399 $this->output( "Flagging bot account edits...\n" );
400
401 # Fill in the rc_bot field
402 $rcids = $this->findRcIdsWithGroups( $dbw, $botgroups );
403
404 foreach ( array_chunk( $rcids, $this->getBatchSize() ) as $rcidBatch ) {
405 $dbw->update(
406 'recentchanges',
407 [ 'rc_bot' => 1 ],
408 [ 'rc_id' => $rcidBatch ],
409 __METHOD__
410 );
411 $this->waitForReplication();
412 }
413 }
414
415 # Flag our recent autopatrolled edits
416 if ( !$wgMiserMode && $autopatrolgroups ) {
417 $this->output( "Flagging auto-patrolled edits...\n" );
418
419 $conds = [ 'rc_patrolled' => 0 ];
420 if ( !$wgUseRCPatrol ) {
421 $subConds = [];
422 if ( $wgUseNPPatrol ) {
423 $subConds[] = 'rc_source = ' . $dbw->addQuotes( RecentChange::SRC_NEW );
424 }
425 if ( $wgUseFilePatrol ) {
426 $subConds[] = 'rc_log_type = ' . $dbw->addQuotes( 'upload' );
427 }
428 $conds[] = $dbw->makeList( $subConds, IDatabase::LIST_OR );
429 }
430
431 $rcids = $this->findRcIdsWithGroups( $dbw, $autopatrolgroups, $conds );
432 foreach ( array_chunk( $rcids, $this->getBatchSize() ) as $rcidBatch ) {
433 $dbw->update(
434 'recentchanges',
435 [ 'rc_patrolled' => 2 ],
436 [ 'rc_id' => $rcidBatch ],
437 __METHOD__
438 );
439 $this->waitForReplication();
440 }
441 }
442 }
443
448 private function rebuildRecentChangesTablePass5() {
449 $dbw = $this->getDB( DB_PRIMARY );
450
451 $this->output( "Removing duplicate revision and logging entries...\n" );
452
453 $res = $dbw->newSelectQueryBuilder()
454 ->select( [ 'ls_value', 'ls_log_id' ] )
455 ->from( 'logging' )
456 ->join( 'log_search', null, 'ls_log_id = log_id' )
457 ->where( [
458 'ls_field' => 'associated_rev_id',
459 'log_type != ' . $dbw->addQuotes( 'create' ),
460 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
461 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
462 ] )
463 ->caller( __METHOD__ )->fetchResultSet();
464
465 $updates = 0;
466 foreach ( $res as $row ) {
467 $rev_id = $row->ls_value;
468 $log_id = $row->ls_log_id;
469
470 // Mark the logging row as having an associated rev id
471 $dbw->update(
472 'recentchanges',
473 /*SET*/ [ 'rc_this_oldid' => $rev_id ],
474 /*WHERE*/ [ 'rc_logid' => $log_id ],
475 __METHOD__
476 );
477
478 // Delete the revision row
479 $dbw->delete(
480 'recentchanges',
481 /*WHERE*/ [ 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ],
482 __METHOD__
483 );
484
485 if ( ( ++$updates % $this->getBatchSize() ) == 0 ) {
486 $this->waitForReplication();
487 }
488 }
489 }
490
494 private function purgeFeeds() {
495 global $wgFeedClasses;
496
497 $this->output( "Deleting feed timestamps.\n" );
498
499 $wanCache = $this->getServiceContainer()->getMainWANObjectCache();
500 foreach ( $wgFeedClasses as $feed => $className ) {
501 $wanCache->delete( $wanCache->makeKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
502 }
503 }
504}
505
506$maintClass = RebuildRecentchanges::class;
507require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const RC_NEW
Definition Defines.php:117
const RC_LOG
Definition Defines.php:118
const RC_EDIT
Definition Defines.php:116
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
Maintenance script that rebuilds recent changes from scratch.
execute()
Do the actual work.
__construct()
Default constructor.
Utility class for creating new RC entries.
$wgUseFilePatrol
Config variable stub for the UseFilePatrol setting, for use by phpdoc and IDEs.
$wgLogRestrictions
Config variable stub for the LogRestrictions setting, for use by phpdoc and IDEs.
$wgUseRCPatrol
Config variable stub for the UseRCPatrol setting, for use by phpdoc and IDEs.
$wgUseNPPatrol
Config variable stub for the UseNPPatrol setting, for use by phpdoc and IDEs.
$wgRCMaxAge
Config variable stub for the RCMaxAge setting, for use by phpdoc and IDEs.
$wgFeedClasses
Config variable stub for the FeedClasses setting, for use by phpdoc and IDEs.
$wgFilterLogTypes
Config variable stub for the FilterLogTypes setting, for use by phpdoc and IDEs.
$wgMiserMode
Config variable stub for the MiserMode setting, for use by phpdoc and IDEs.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36
const DB_PRIMARY
Definition defines.php:28