MediaWiki  1.33.0
resetPageRandom.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Reset the page_random for articles within given date range' );
35  $this->addOption( 'from',
36  'From date range selector to select articles to update, ex: 20041011000000' );
37  $this->addOption( 'to',
38  'To date range selector to select articles to update, ex: 20050708000000' );
39  $this->addOption( 'dry', 'Do not update column' );
40  $this->addOption( 'batch-start',
41  'Optional: Use when you need to restart the reset process from a given page ID offset'
42  . ' in case a previous reset failed or was stopped'
43  );
44  // Initialize batch size to a good default value and enable the batch size option.
45  $this->setBatchSize( 200 );
46  }
47 
48  public function execute() {
49  $batchSize = $this->getBatchSize();
50  $dbw = $this->getDB( DB_MASTER );
51  $lbFactory = \MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
52  $dbr = $this->getDB( DB_REPLICA );
53  $from = wfTimestampOrNull( TS_MW, $this->getOption( 'from' ) );
54  $to = wfTimestampOrNull( TS_MW, $this->getOption( 'to' ) );
55 
56  if ( $from === null || $to === null ) {
57  $this->output( "--from and --to have to be provided" . PHP_EOL );
58  return false;
59  }
60  if ( $from >= $to ) {
61  $this->output( "--from has to be smaller than --to" . PHP_EOL );
62  return false;
63  }
64  $batchStart = (int)$this->getOption( 'batch-start', 0 );
65  $changed = 0;
66  $dry = (bool)$this->getOption( 'dry' );
67 
68  $message = "Resetting page_random column within date range from $from to $to";
69  if ( $batchStart > 0 ) {
70  $message .= " starting from page ID $batchStart";
71  }
72  $message .= $dry ? ". dry run" : '.';
73 
74  $this->output( $message . PHP_EOL );
75  do {
76  $this->output( " ...doing chunk of $batchSize from $batchStart " . PHP_EOL );
77 
78  // Find the oldest page revision associated with each page_id. Iff it falls in the given
79  // time range AND it's greater than $batchStart, yield the page ID. If it falls outside the
80  // time range, it was created before or after the occurrence of T208909 and its page_random
81  // is considered valid. The replica is used for this read since page_id and the rev_timestamp
82  // will not change between queries.
83  $res = $dbr->select(
84  'page',
85  'page_id',
86  [
87  '(' . $dbr->selectSQLText( 'revision', 'MIN(rev_timestamp)', 'rev_page=page_id' ) . ') ' .
88  'BETWEEN ' . $dbr->addQuotes( $dbr->timestamp( $from ) ) .
89  ' AND ' . $dbr->addQuotes( $dbr->timestamp( $to ) ),
90  'page_id > ' . $dbr->addQuotes( $batchStart )
91  ],
92  __METHOD__,
93  [ 'LIMIT' => $batchSize, 'ORDER BY' => 'page_id' ]
94  );
95 
96  foreach ( $res as $row ) {
97  if ( !$dry ) {
98  # Update the row...
99  $dbw->update( 'page',
100  [ 'page_random' => wfRandom() ],
101  [ 'page_id' => $row->page_id ],
102  __METHOD__ );
103  $changed += $dbw->affectedRows();
104  } else {
105  $changed++;
106  }
107  }
108  if ( $row ) {
109  $batchStart = $row->page_id;
110  } else {
111  // We don't need to set the $batchStart as $res is empty,
112  // and we don't need to do another loop
113  // the while() condition will evaluate to false and
114  // we will leave the do{}while() block.
115  }
116 
117  $lbFactory->waitForReplication();
118  } while ( $res->numRows() === $batchSize );
119  $this->output( "page_random reset complete ... changed $changed rows" . PHP_EOL );
120 
121  return true;
122  }
123 }
124 
126 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
$maintClass
$maintClass
Definition: resetPageRandom.php:125
ResetPageRandom\__construct
__construct()
Default constructor.
Definition: resetPageRandom.php:32
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
php
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
$dbr
$dbr
Definition: testCompression.php:50
wfTimestampOrNull
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
Definition: GlobalFunctions.php:1928
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:248
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
ResetPageRandom
Maintenance script that resets page_random over a time range.
Definition: resetPageRandom.php:31
ResetPageRandom\execute
execute()
Do the actual work.
Definition: resetPageRandom.php:48
wfRandom
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
Definition: GlobalFunctions.php:280
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:283
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:367
as
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
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1373
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:375