Debian Codegraph @

maintenance/resetPageRandom.php

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