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