MediaWiki  1.28.0
rebuildrecentchanges.php
Go to the documentation of this file.
1 <?php
26 require_once __DIR__ . '/Maintenance.php';
27 
35  private $cutoffFrom;
37  private $cutoffTo;
38 
39  public function __construct() {
40  parent::__construct();
41  $this->addDescription( 'Rebuild recent changes' );
42 
43  $this->addOption(
44  'from',
45  "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
46  false,
47  true
48  );
49  $this->addOption(
50  'to',
51  "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
52  false,
53  true
54  );
55  $this->setBatchSize( 200 );
56  }
57 
58  public function execute() {
59  if (
60  ( $this->hasOption( 'from' ) && !$this->hasOption( 'to' ) ) ||
61  ( !$this->hasOption( 'from' ) && $this->hasOption( 'to' ) )
62  ) {
63  $this->error( "Both 'from' and 'to' must be given, or neither", 1 );
64  }
65 
71  if ( !( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) ) {
72  $this->purgeFeeds();
73  }
74  $this->output( "Done.\n" );
75  }
76 
80  private function rebuildRecentChangesTablePass1() {
81  $dbw = $this->getDB( DB_MASTER );
82 
83  if ( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) {
84  $this->cutoffFrom = wfTimestamp( TS_UNIX, $this->getOption( 'from' ) );
85  $this->cutoffTo = wfTimestamp( TS_UNIX, $this->getOption( 'to' ) );
86 
87  $sec = $this->cutoffTo - $this->cutoffFrom;
88  $days = $sec / 24 / 3600;
89  $this->output( "Rebuilding range of $sec seconds ($days days)\n" );
90  } else {
91  global $wgRCMaxAge;
92 
93  $days = $wgRCMaxAge / 24 / 3600;
94  $this->output( "Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
95 
96  $this->cutoffFrom = time() - $wgRCMaxAge;
97  $this->cutoffTo = time();
98  }
99 
100  $this->output( "Clearing recentchanges table for time range...\n" );
101  $rcids = $dbw->selectFieldValues(
102  'recentchanges',
103  'rc_id',
104  [
105  'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
106  'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
107  ]
108  );
109  foreach ( array_chunk( $rcids, $this->mBatchSize ) as $rcidBatch ) {
110  $dbw->delete( 'recentchanges', [ 'rc_id' => $rcidBatch ], __METHOD__ );
111  wfGetLBFactory()->waitForReplication();
112  }
113 
114  $this->output( "Loading from page and revision tables...\n" );
115  $res = $dbw->select(
116  [ 'page', 'revision' ],
117  [
118  'rev_timestamp',
119  'rev_user',
120  'rev_user_text',
121  'rev_comment',
122  'rev_minor_edit',
123  'rev_id',
124  'rev_deleted',
125  'page_namespace',
126  'page_title',
127  'page_is_new',
128  'page_id'
129  ],
130  [
131  'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
132  'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
133  'rev_page=page_id'
134  ],
135  __METHOD__,
136  [ 'ORDER BY' => 'rev_timestamp DESC' ]
137  );
138 
139  $this->output( "Inserting from page and revision tables...\n" );
140  $inserted = 0;
141  foreach ( $res as $row ) {
142  $dbw->insert(
143  'recentchanges',
144  [
145  'rc_timestamp' => $row->rev_timestamp,
146  'rc_user' => $row->rev_user,
147  'rc_user_text' => $row->rev_user_text,
148  'rc_namespace' => $row->page_namespace,
149  'rc_title' => $row->page_title,
150  'rc_comment' => $row->rev_comment,
151  'rc_minor' => $row->rev_minor_edit,
152  'rc_bot' => 0,
153  'rc_new' => $row->page_is_new,
154  'rc_cur_id' => $row->page_id,
155  'rc_this_oldid' => $row->rev_id,
156  'rc_last_oldid' => 0, // is this ok?
157  'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
158  'rc_source' => $row->page_is_new
159  ? $dbw->addQuotes( RecentChange::SRC_NEW )
160  : $dbw->addQuotes( RecentChange::SRC_EDIT )
161  ,
162  'rc_deleted' => $row->rev_deleted
163  ],
164  __METHOD__
165  );
166  if ( ( ++$inserted % $this->mBatchSize ) == 0 ) {
167  wfGetLBFactory()->waitForReplication();
168  }
169  }
170  }
171 
176  private function rebuildRecentChangesTablePass2() {
177  $dbw = $this->getDB( DB_MASTER );
178 
179  $this->output( "Updating links and size differences...\n" );
180 
181  # Fill in the rc_last_oldid field, which points to the previous edit
182  $res = $dbw->select(
183  'recentchanges',
184  [ 'rc_cur_id', 'rc_this_oldid', 'rc_timestamp' ],
185  [
186  "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
187  "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
188  ],
189  __METHOD__,
190  [ 'ORDER BY' => 'rc_cur_id,rc_timestamp' ]
191  );
192 
193  $lastCurId = 0;
194  $lastOldId = 0;
195  $lastSize = null;
196  $updated = 0;
197  foreach ( $res as $obj ) {
198  $new = 0;
199 
200  if ( $obj->rc_cur_id != $lastCurId ) {
201  # Switch! Look up the previous last edit, if any
202  $lastCurId = intval( $obj->rc_cur_id );
203  $emit = $obj->rc_timestamp;
204 
205  $row = $dbw->selectRow(
206  'revision',
207  [ 'rev_id', 'rev_len' ],
208  [ 'rev_page' => $lastCurId, "rev_timestamp < " . $dbw->addQuotes( $emit ) ],
209  __METHOD__,
210  [ 'ORDER BY' => 'rev_timestamp DESC' ]
211  );
212  if ( $row ) {
213  $lastOldId = intval( $row->rev_id );
214  # Grab the last text size if available
215  $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : null;
216  } else {
217  # No previous edit
218  $lastOldId = 0;
219  $lastSize = null;
220  $new = 1; // probably true
221  }
222  }
223 
224  if ( $lastCurId == 0 ) {
225  $this->output( "Uhhh, something wrong? No curid\n" );
226  } else {
227  # Grab the entry's text size
228  $size = (int)$dbw->selectField(
229  'revision',
230  'rev_len',
231  [ 'rev_id' => $obj->rc_this_oldid ],
232  __METHOD__
233  );
234 
235  $dbw->update(
236  'recentchanges',
237  [
238  'rc_last_oldid' => $lastOldId,
239  'rc_new' => $new,
240  'rc_type' => $new ? RC_NEW : RC_EDIT,
241  'rc_source' => $new === 1
242  ? $dbw->addQuotes( RecentChange::SRC_NEW )
243  : $dbw->addQuotes( RecentChange::SRC_EDIT ),
244  'rc_old_len' => $lastSize,
245  'rc_new_len' => $size,
246  ],
247  [
248  'rc_cur_id' => $lastCurId,
249  'rc_this_oldid' => $obj->rc_this_oldid,
250  'rc_timestamp' => $obj->rc_timestamp // index usage
251  ],
252  __METHOD__
253  );
254 
255  $lastOldId = intval( $obj->rc_this_oldid );
256  $lastSize = $size;
257 
258  if ( ( ++$updated % $this->mBatchSize ) == 0 ) {
259  wfGetLBFactory()->waitForReplication();
260  }
261  }
262  }
263  }
264 
268  private function rebuildRecentChangesTablePass3() {
269  global $wgLogTypes, $wgLogRestrictions;
270 
271  $dbw = $this->getDB( DB_MASTER );
272 
273  $this->output( "Loading from user, page, and logging tables...\n" );
274 
275  $res = $dbw->select(
276  [ 'user', 'logging', 'page' ],
277  [
278  'log_timestamp',
279  'log_user',
280  'user_name',
281  'log_namespace',
282  'log_title',
283  'log_comment',
284  'page_id',
285  'log_type',
286  'log_action',
287  'log_id',
288  'log_params',
289  'log_deleted'
290  ],
291  [
292  'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
293  'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
294  'log_user=user_id',
295  // Some logs don't go in RC since they are private.
296  // @FIXME: core/extensions also have spammy logs that don't go in RC.
297  'log_type' => array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) ),
298  ],
299  __METHOD__,
300  [ 'ORDER BY' => 'log_timestamp DESC' ],
301  [
302  'page' =>
303  [ 'LEFT JOIN', [ 'log_namespace=page_namespace', 'log_title=page_title' ] ]
304  ]
305  );
306 
307  $field = $dbw->fieldInfo( 'recentchanges', 'rc_cur_id' );
308 
309  $inserted = 0;
310  foreach ( $res as $row ) {
311  $dbw->insert(
312  'recentchanges',
313  [
314  'rc_timestamp' => $row->log_timestamp,
315  'rc_user' => $row->log_user,
316  'rc_user_text' => $row->user_name,
317  'rc_namespace' => $row->log_namespace,
318  'rc_title' => $row->log_title,
319  'rc_comment' => $row->log_comment,
320  'rc_minor' => 0,
321  'rc_bot' => 0,
322  'rc_patrolled' => 1,
323  'rc_new' => 0,
324  'rc_this_oldid' => 0,
325  'rc_last_oldid' => 0,
326  'rc_type' => RC_LOG,
327  'rc_source' => $dbw->addQuotes( RecentChange::SRC_LOG ),
328  'rc_cur_id' => $field->isNullable()
329  ? $row->page_id
330  : (int)$row->page_id, // NULL => 0,
331  'rc_log_type' => $row->log_type,
332  'rc_log_action' => $row->log_action,
333  'rc_logid' => $row->log_id,
334  'rc_params' => $row->log_params,
335  'rc_deleted' => $row->log_deleted
336  ],
337  __METHOD__
338  );
339 
340  if ( ( ++$inserted % $this->mBatchSize ) == 0 ) {
341  wfGetLBFactory()->waitForReplication();
342  }
343  }
344  }
345 
349  private function rebuildRecentChangesTablePass4() {
350  global $wgUseRCPatrol, $wgMiserMode;
351 
352  $dbw = $this->getDB( DB_MASTER );
353 
354  list( $recentchanges, $usergroups, $user ) =
355  $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
356 
357  # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
358  # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
359  # may be lost at this point (aside from joining on the patrol log table entries).
360  $botgroups = [ 'bot' ];
361  $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : [];
362 
363  # Flag our recent bot edits
364  if ( $botgroups ) {
365  $botwhere = $dbw->makeList( $botgroups );
366 
367  $this->output( "Flagging bot account edits...\n" );
368 
369  # Find all users that are bots
370  $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
371  "WHERE ug_group IN($botwhere) AND user_id = ug_user";
372  $res = $dbw->query( $sql, __METHOD__ );
373 
374  $botusers = [];
375  foreach ( $res as $obj ) {
376  $botusers[] = $obj->user_name;
377  }
378 
379  # Fill in the rc_bot field
380  if ( $botusers ) {
381  $rcids = $dbw->selectFieldValues(
382  'recentchanges',
383  'rc_id',
384  [
385  'rc_user_text' => $botusers,
386  "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
387  "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
388  ],
389  __METHOD__
390  );
391 
392  foreach ( array_chunk( $rcids, $this->mBatchSize ) as $rcidBatch ) {
393  $dbw->update(
394  'recentchanges',
395  [ 'rc_bot' => 1 ],
396  [ 'rc_id' => $rcidBatch ],
397  __METHOD__
398  );
399  wfGetLBFactory()->waitForReplication();
400  }
401  }
402  }
403 
404  # Flag our recent autopatrolled edits
405  if ( !$wgMiserMode && $autopatrolgroups ) {
406  $patrolwhere = $dbw->makeList( $autopatrolgroups );
407  $patrolusers = [];
408 
409  $this->output( "Flagging auto-patrolled edits...\n" );
410 
411  # Find all users in RC with autopatrol rights
412  $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
413  "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
414  $res = $dbw->query( $sql, __METHOD__ );
415 
416  foreach ( $res as $obj ) {
417  $patrolusers[] = $dbw->addQuotes( $obj->user_name );
418  }
419 
420  # Fill in the rc_patrolled field
421  if ( $patrolusers ) {
422  $patrolwhere = implode( ',', $patrolusers );
423  $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
424  "WHERE rc_user_text IN($patrolwhere) " .
425  "AND rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ) . ' ' .
426  "AND rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) );
427  $dbw->query( $sql2 );
428  }
429  }
430  }
431 
436  private function rebuildRecentChangesTablePass5() {
437  $dbw = wfGetDB( DB_MASTER );
438 
439  $this->output( "Removing duplicate revision and logging entries...\n" );
440 
441  $res = $dbw->select(
442  [ 'logging', 'log_search' ],
443  [ 'ls_value', 'ls_log_id' ],
444  [
445  'ls_log_id = log_id',
446  'ls_field' => 'associated_rev_id',
447  'log_type' => 'upload',
448  'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
449  'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
450  ],
451  __METHOD__
452  );
453 
454  $updates = 0;
455  foreach ( $res as $obj ) {
456  $rev_id = $obj->ls_value;
457  $log_id = $obj->ls_log_id;
458 
459  // Mark the logging row as having an associated rev id
460  $dbw->update(
461  'recentchanges',
462  /*SET*/ [ 'rc_this_oldid' => $rev_id ],
463  /*WHERE*/ [ 'rc_logid' => $log_id ],
464  __METHOD__
465  );
466 
467  // Delete the revision row
468  $dbw->delete(
469  'recentchanges',
470  /*WHERE*/ [ 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ],
471  __METHOD__
472  );
473 
474  if ( ( ++$updates % $this->mBatchSize ) == 0 ) {
475  wfGetLBFactory()->waitForReplication();
476  }
477  }
478  }
479 
483  private function purgeFeeds() {
484  global $wgFeedClasses, $messageMemc;
485 
486  $this->output( "Deleting feed timestamps.\n" );
487 
488  foreach ( $wgFeedClasses as $feed => $className ) {
489  $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
490  }
491  }
492 }
493 
494 $maintClass = "RebuildRecentchanges";
495 require_once RUN_MAINTENANCE_IF_MAIN;
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
rebuildRecentChangesTablePass3()
Rebuild pass 3: Insert recentchanges entries for action logs.
Maintenance script that rebuilds recent changes from scratch.
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $messageMemc
Definition: globals.txt:25
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
getDB($db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
hasOption($name)
Checks to see if a particular param exists.
rebuildRecentChangesTablePass2()
Rebuild pass 2: Enhance entries for page revisions with references to the previous revision (rc_last_...
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
rebuildRecentChangesTablePass1()
Rebuild pass 1: Insert recentchanges entries for page revisions.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
const DB_MASTER
Definition: defines.php:23
purgeFeeds()
Purge cached feeds in $messageMemc.
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: defines.php:6
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
addOption($name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
rebuildRecentChangesTablePass5()
Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log entry for a...
$res
Definition: database.txt:21
$wgMiserMode
Disable database-intensive features.
integer $cutoffFrom
UNIX timestamp.
addDescription($text)
Set the description text.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
getOption($name, $default=null)
Get an option, or return the default.
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
output($out, $channel=null)
Throw some output to the user.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
wfGetLBFactory()
Get the load balancer factory object.
error($err, $die=0)
Throw an error to the user.
wfMemcKey()
Make a cache key for the local wiki.
const RC_NEW
Definition: Defines.php:137
integer $cutoffTo
UNIX timestamp.
rebuildRecentChangesTablePass4()
Rebuild pass 4: Mark bot and autopatrolled entries.
setBatchSize($s=0)
Set the batch size.
static getGroupsWithPermission($role)
Get all the groups who have a given permission.
Definition: User.php:4602
const RC_EDIT
Definition: Defines.php:136
const RC_LOG
Definition: Defines.php:138