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