MediaWiki fundraising/REL1_35
resetPageRandom.php
Go to the documentation of this file.
1<?php
24require_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(
88 'revision',
89 'MIN(rev_timestamp)',
90 'rev_page=page_id',
91 __METHOD__
92 ) . ') ' .
93 'BETWEEN ' . $dbr->addQuotes( $dbr->timestamp( $from ) ) .
94 ' AND ' . $dbr->addQuotes( $dbr->timestamp( $to ) ),
95 'page_id > ' . $dbr->addQuotes( $batchStart )
96 ],
97 __METHOD__,
98 [ 'LIMIT' => $batchSize, 'ORDER BY' => 'page_id' ]
99 );
100
101 foreach ( $res as $row ) {
102 if ( !$dry ) {
103 # Update the row...
104 $dbw->update( 'page',
105 [ 'page_random' => wfRandom() ],
106 [ 'page_id' => $row->page_id ],
107 __METHOD__ );
108 $changed += $dbw->affectedRows();
109 } else {
110 $changed++;
111 }
112 }
113 if ( $row ) {
114 $batchStart = $row->page_id;
115 } else {
116 // We don't need to set the $batchStart as $res is empty,
117 // and we don't need to do another loop
118 // the while() condition will evaluate to false and
119 // we will leave the do{}while() block.
120 }
121
122 $lbFactory->waitForReplication();
123 } while ( $res->numRows() === $batchSize );
124 $this->output( "page_random reset complete ... changed $changed rows" . PHP_EOL );
125
126 return true;
127 }
128}
129
130$maintClass = ResetPageRandom::class;
131require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
const RUN_MAINTENANCE_IF_MAIN
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
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)
Set the batch size.
Maintenance script that resets page_random over a time range.
execute()
Do the actual work.
__construct()
Default constructor.
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29