11use Wikimedia\Timestamp\TimestampFormat as TS;
14require_once __DIR__ .
'/Maintenance.php';
24 parent::__construct();
25 $this->
addDescription(
'Reset the page_random for articles within given date range' );
27 'From date range selector to select articles to update, ex: 20041011000000',
true,
true );
29 'To date range selector to select articles to update, ex: 20050708000000',
true,
true );
30 $this->
addOption(
'dry',
'Do not update column' );
32 'Optional: Use when you need to restart the reset process from a given page ID offset'
33 .
' in case a previous reset failed or was stopped'
47 if ( $from ===
null || $to ===
null ) {
48 $this->
output(
"--from and --to have to be provided" . PHP_EOL );
52 $this->
output(
"--from has to be smaller than --to" . PHP_EOL );
55 $batchStart = (int)$this->
getOption(
'batch-start', 0 );
59 $message =
"Resetting page_random column within date range from $from to $to";
60 if ( $batchStart > 0 ) {
61 $message .=
" starting from page ID $batchStart";
63 $message .= $dry ?
". dry run" :
'.';
65 $this->
output( $message . PHP_EOL );
67 $this->
output(
" ...doing chunk of $batchSize from $batchStart " . PHP_EOL );
74 $queryBuilder = $dbr->newSelectQueryBuilder()
77 ->where( $dbr->expr(
'page_id',
'>', $batchStart ) )
79 ->orderBy(
'page_id' );
80 $subquery = $queryBuilder->newSubquery()
81 ->select(
'MIN(rev_timestamp)' )
83 ->where(
'rev_page=page_id' );
84 $queryBuilder->andWhere(
85 '(' . $subquery->getSQL() .
') BETWEEN ' .
86 $dbr->addQuotes( $dbr->timestamp( $from ) ) .
' AND ' . $dbr->addQuotes( $dbr->timestamp( $to ) )
89 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
91 foreach ( $res as $row ) {
94 $dbw->newUpdateQueryBuilder()
96 ->set( [
'page_random' =>
wfRandom() ] )
97 ->where( [
'page_id' => $row->page_id ] )
98 ->caller( __METHOD__ )
100 $changed += $dbw->affectedRows();
106 $batchStart = $row->page_id;
115 }
while ( $res->numRows() === $batchSize );
116 $this->
output(
"page_random reset complete ... changed $changed rows" . PHP_EOL );
124require_once RUN_MAINTENANCE_IF_MAIN;
wfTimestampOrNull( $outputtype=TS::UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
getOption( $name, $default=null)
Get an option, or return the default.
getReplicaDB(string|false $virtualDomain=false)
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.
Maintenance script that resets page_random over a time range.
execute()
Do the actual work.All child classes will need to implement thisbool|null|void True for success,...
__construct()
Default constructor.