MediaWiki master
resetPageRandom.php
Go to the documentation of this file.
1<?php
11use Wikimedia\Timestamp\TimestampFormat as TS;
12
13// @codeCoverageIgnoreStart
14require_once __DIR__ . '/Maintenance.php';
15// @codeCoverageIgnoreEnd
16
23 public function __construct() {
24 parent::__construct();
25 $this->addDescription( 'Reset the page_random for articles within given date range' );
26 $this->addOption( 'from',
27 'From date range selector to select articles to update, ex: 20041011000000', true, true );
28 $this->addOption( 'to',
29 'To date range selector to select articles to update, ex: 20050708000000', true, true );
30 $this->addOption( 'dry', 'Do not update column' );
31 $this->addOption( 'batch-start',
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'
34 );
35 // Initialize batch size to a good default value and enable the batch size option.
36 $this->setBatchSize( 200 );
37 }
38
40 public function execute() {
41 $batchSize = $this->getBatchSize();
42 $dbw = $this->getPrimaryDB();
43 $dbr = $this->getReplicaDB();
44 $from = wfTimestampOrNull( TS::MW, $this->getOption( 'from' ) );
45 $to = wfTimestampOrNull( TS::MW, $this->getOption( 'to' ) );
46
47 if ( $from === null || $to === null ) {
48 $this->output( "--from and --to have to be provided" . PHP_EOL );
49 return false;
50 }
51 if ( $from >= $to ) {
52 $this->output( "--from has to be smaller than --to" . PHP_EOL );
53 return false;
54 }
55 $batchStart = (int)$this->getOption( 'batch-start', 0 );
56 $changed = 0;
57 $dry = (bool)$this->getOption( 'dry' );
58
59 $message = "Resetting page_random column within date range from $from to $to";
60 if ( $batchStart > 0 ) {
61 $message .= " starting from page ID $batchStart";
62 }
63 $message .= $dry ? ". dry run" : '.';
64
65 $this->output( $message . PHP_EOL );
66 do {
67 $this->output( " ...doing chunk of $batchSize from $batchStart " . PHP_EOL );
68
69 // Find the oldest page revision associated with each page_id. Iff it falls in the given
70 // time range AND it's greater than $batchStart, yield the page ID. If it falls outside the
71 // time range, it was created before or after the occurrence of T208909 and its page_random
72 // is considered valid. The replica is used for this read since page_id and the rev_timestamp
73 // will not change between queries.
74 $queryBuilder = $dbr->newSelectQueryBuilder()
75 ->select( 'page_id' )
76 ->from( 'page' )
77 ->where( $dbr->expr( 'page_id', '>', $batchStart ) )
78 ->limit( $batchSize )
79 ->orderBy( 'page_id' );
80 $subquery = $queryBuilder->newSubquery()
81 ->select( 'MIN(rev_timestamp)' )
82 ->from( 'revision' )
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 ) )
87 );
88
89 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
90 $row = null;
91 foreach ( $res as $row ) {
92 if ( !$dry ) {
93 # Update the row...
94 $dbw->newUpdateQueryBuilder()
95 ->update( 'page' )
96 ->set( [ 'page_random' => wfRandom() ] )
97 ->where( [ 'page_id' => $row->page_id ] )
98 ->caller( __METHOD__ )
99 ->execute();
100 $changed += $dbw->affectedRows();
101 } else {
102 $changed++;
103 }
104 }
105 if ( $row ) {
106 $batchStart = $row->page_id;
107 } else {
108 // We don't need to set the $batchStart as $res is empty,
109 // and we don't need to do another loop
110 // the while() condition will evaluate to false and
111 // we will leave the do{}while() block.
112 }
113
114 $this->waitForReplication();
115 } while ( $res->numRows() === $batchSize );
116 $this->output( "page_random reset complete ... changed $changed rows" . PHP_EOL );
117
118 return true;
119 }
120}
121
122// @codeCoverageIgnoreStart
123$maintClass = ResetPageRandom::class;
124require_once RUN_MAINTENANCE_IF_MAIN;
125// @codeCoverageIgnoreEnd
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.