| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
require_once __DIR__ . '/Maintenance.php'; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class FixInconsistentRedirects extends LoggedUpdateMaintenance { |
| 32 |
|
| 33 |
public function __construct() { |
| 34 |
parent::__construct(); |
| 35 |
$this->addDescription( 'Fix redirect pages with missing or incomplete row in the redirect table' ); |
| 36 |
$this->setBatchSize( 100 ); |
| 37 |
} |
| 38 |
|
| 39 |
protected function getUpdateKey() { |
| 40 |
return __CLASS__; |
| 41 |
} |
| 42 |
|
| 43 |
protected function doDBUpdates() { |
| 44 |
$dbr = $this->getReplicaDB(); |
| 45 |
|
| 46 |
$builder = $dbr->newSelectQueryBuilder() |
| 47 |
->caller( __METHOD__ ) |
| 48 |
->from( 'page' ) |
| 49 |
->where( [ 'page_is_redirect' => 1 ] ); |
| 50 |
|
| 51 |
$this->output( "Fixing inconsistent redirects ...\n" ); |
| 52 |
|
| 53 |
$estimateCount = $builder->estimateRowCount(); |
| 54 |
$this->output( "Estimated redirect page count: $estimateCount\n" ); |
| 55 |
|
| 56 |
$builder |
| 57 |
->limit( $this->getBatchSize() ) |
| 58 |
->leftJoin( 'redirect', null, 'page_id=rd_from' ) |
| 59 |
->select( [ 'rd_from', 'rd_interwiki', 'rd_fragment' ] ); |
| 60 |
|
| 61 |
|
| 62 |
$index = [ 'page_is_redirect', 'page_namespace', 'page_len', 'page_id' ]; |
| 63 |
$builder->select( $index )->orderBy( $index ); |
| 64 |
$prevRow = []; |
| 65 |
|
| 66 |
$total = 0; |
| 67 |
$updated = 0; |
| 68 |
do { |
| 69 |
$res = ( clone $builder ) |
| 70 |
->where( $prevRow ? [ $dbr->buildComparison( '>', $prevRow ) ] : [] ) |
| 71 |
->caller( __METHOD__ )->fetchResultSet(); |
| 72 |
|
| 73 |
foreach ( $res as $row ) { |
| 74 |
|
| 75 |
|
| 76 |
if ( $row->rd_from === null || $row->rd_interwiki === null || $row->rd_fragment === null ) { |
| 77 |
RefreshLinks::fixRedirect( $this, $row->page_id ); |
| 78 |
$updated++; |
| 79 |
} |
| 80 |
} |
| 81 |
if ( isset( $row ) ) { |
| 82 |
|
| 83 |
foreach ( $index as $field ) { |
| 84 |
$prevRow[ $field ] = $row->$field; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
$this->waitForReplication(); |
| 89 |
$total += $res->numRows(); |
| 90 |
$this->output( "$updated/$total\n" ); |
| 91 |
|
| 92 |
} while ( $res->numRows() == $this->getBatchSize() ); |
| 93 |
|
| 94 |
$this->output( "Done, updated $updated of $total rows.\n" ); |
| 95 |
return true; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
$maintClass = FixInconsistentRedirects::class; |
| 101 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 102 |
|