3 * Resets the page_random field for articles in the provided time range.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 use MediaWiki\Maintenance\Maintenance
;
26 // @codeCoverageIgnoreStart
27 require_once __DIR__
. '/Maintenance.php';
28 // @codeCoverageIgnoreEnd
31 * Maintenance script that resets page_random over a time range.
33 * @ingroup Maintenance
35 class ResetPageRandom
extends Maintenance
{
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'
48 // Initialize batch size to a good default value and enable the batch size option.
49 $this->setBatchSize( 200 );
52 public function execute() {
53 $batchSize = $this->getBatchSize();
54 $dbw = $this->getPrimaryDB();
55 $dbr = $this->getReplicaDB();
56 $from = wfTimestampOrNull( TS_MW
, $this->getOption( 'from' ) );
57 $to = wfTimestampOrNull( TS_MW
, $this->getOption( 'to' ) );
59 if ( $from === null ||
$to === null ) {
60 $this->output( "--from and --to have to be provided" . PHP_EOL
);
64 $this->output( "--from has to be smaller than --to" . PHP_EOL
);
67 $batchStart = (int)$this->getOption( 'batch-start', 0 );
69 $dry = (bool)$this->getOption( 'dry' );
71 $message = "Resetting page_random column within date range from $from to $to";
72 if ( $batchStart > 0 ) {
73 $message .= " starting from page ID $batchStart";
75 $message .= $dry ?
". dry run" : '.';
77 $this->output( $message . PHP_EOL
);
79 $this->output( " ...doing chunk of $batchSize from $batchStart " . PHP_EOL
);
81 // Find the oldest page revision associated with each page_id. Iff it falls in the given
82 // time range AND it's greater than $batchStart, yield the page ID. If it falls outside the
83 // time range, it was created before or after the occurrence of T208909 and its page_random
84 // is considered valid. The replica is used for this read since page_id and the rev_timestamp
85 // will not change between queries.
86 $queryBuilder = $dbr->newSelectQueryBuilder()
89 ->where( $dbr->expr( 'page_id', '>', $batchStart ) )
91 ->orderBy( 'page_id' );
92 $subquery = $queryBuilder->newSubquery()
93 ->select( 'MIN(rev_timestamp)' )
95 ->where( 'rev_page=page_id' );
96 $queryBuilder->andWhere(
97 '(' . $subquery->getSQL() . ') BETWEEN ' .
98 $dbr->addQuotes( $dbr->timestamp( $from ) ) . ' AND ' . $dbr->addQuotes( $dbr->timestamp( $to ) )
101 $res = $queryBuilder->caller( __METHOD__
)->fetchResultSet();
103 foreach ( $res as $row ) {
106 $dbw->newUpdateQueryBuilder()
108 ->set( [ 'page_random' => wfRandom() ] )
109 ->where( [ 'page_id' => $row->page_id
] )
110 ->caller( __METHOD__
)
112 $changed +
= $dbw->affectedRows();
118 $batchStart = $row->page_id
;
120 // We don't need to set the $batchStart as $res is empty,
121 // and we don't need to do another loop
122 // the while() condition will evaluate to false and
123 // we will leave the do{}while() block.
126 $this->waitForReplication();
127 } while ( $res->numRows() === $batchSize );
128 $this->output( "page_random reset complete ... changed $changed rows" . PHP_EOL
);
134 // @codeCoverageIgnoreStart
135 $maintClass = ResetPageRandom
::class;
136 require_once RUN_MAINTENANCE_IF_MAIN
;
137 // @codeCoverageIgnoreEnd