| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 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 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 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 |
|
| 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 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 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 |
|
| 133 |
$maintClass = ResetPageRandom::class; |
| 134 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 135 |
|