MediaWiki master
resetPageRandom.php
Go to the documentation of this file.
1<?php
24// @codeCoverageIgnoreStart
25require_once __DIR__ . '/Maintenance.php';
26// @codeCoverageIgnoreEnd
27
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;
134require_once RUN_MAINTENANCE_IF_MAIN;
135// @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...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
Maintenance script that resets page_random over a time range.
execute()
Do the actual work.
__construct()
Default constructor.