Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.46% covered (success)
98.46%
64 / 65
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ResetPageRandom
98.46% covered (success)
98.46%
64 / 65
50.00% covered (danger)
50.00%
1 / 2
10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 execute
98.11% covered (success)
98.11%
52 / 53
0.00% covered (danger)
0.00%
0 / 1
9
1<?php
2/**
3 * Resets the page_random field for articles in the provided time range.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Maintenance
8 */
9
10use MediaWiki\Maintenance\Maintenance;
11use Wikimedia\Timestamp\TimestampFormat as TS;
12
13// @codeCoverageIgnoreStart
14require_once __DIR__ . '/Maintenance.php';
15// @codeCoverageIgnoreEnd
16
17/**
18 * Maintenance script that resets page_random over a time range.
19 *
20 * @ingroup Maintenance
21 */
22class ResetPageRandom extends Maintenance {
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
39    /** @inheritDoc */
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                    $update = $dbw->newUpdateQueryBuilder()
95                        ->update( 'page' )
96                        ->set( [ 'page_random' => wfRandom() ] )
97                        ->where( [ 'page_id' => $row->page_id ] )
98                        ->caller( __METHOD__ );
99                    $update->execute();
100                    $this->getServiceContainer()->getLinkWriteDuplicator()->duplicate( $update );
101                    $changed += $dbw->affectedRows();
102                } else {
103                    $changed++;
104                }
105            }
106            if ( $row ) {
107                $batchStart = $row->page_id;
108            } else {
109                // We don't need to set the $batchStart as $res is empty,
110                // and we don't need to do another loop
111                // the while() condition will evaluate to false and
112                // we will leave the do{}while() block.
113            }
114
115            $this->waitForReplication();
116        } while ( $res->numRows() === $batchSize );
117        $this->output( "page_random reset complete ... changed $changed rows" . PHP_EOL );
118
119        return true;
120    }
121}
122
123// @codeCoverageIgnoreStart
124$maintClass = ResetPageRandom::class;
125require_once RUN_MAINTENANCE_IF_MAIN;
126// @codeCoverageIgnoreEnd