MediaWiki master
fixInconsistentRedirects.php
Go to the documentation of this file.
1<?php
21require_once __DIR__ . '/Maintenance.php';
22
30
31 public function __construct() {
32 parent::__construct();
33 $this->addDescription( 'Fix redirect pages with missing or incomplete row in the redirect table' );
34 $this->setBatchSize( 100 );
35 }
36
37 protected function getUpdateKey() {
38 return __CLASS__;
39 }
40
41 protected function doDBUpdates() {
42 $dbr = $this->getReplicaDB();
43
44 $builder = $dbr->newSelectQueryBuilder()
45 ->caller( __METHOD__ )
46 ->from( 'page' )
47 ->where( [ 'page_is_redirect' => 1 ] );
48
49 $this->output( "Fixing inconsistent redirects ...\n" );
50
51 $estimateCount = $builder->estimateRowCount();
52 $this->output( "Estimated redirect page count: $estimateCount\n" );
53
54 $builder
55 ->limit( $this->getBatchSize() )
56 ->leftJoin( 'redirect', null, 'page_id=rd_from' )
57 ->select( [ 'rd_from', 'rd_interwiki', 'rd_fragment' ] );
58
59 // Using the page_redirect_namespace_len index to skip non-redirects
60 $index = [ 'page_is_redirect', 'page_namespace', 'page_len', 'page_id' ];
61 $builder->select( $index )->orderBy( $index );
62 $prevRow = [];
63
64 $total = 0;
65 $updated = 0;
66 do {
67 $res = ( clone $builder )
68 ->where( $prevRow ? [ $dbr->buildComparison( '>', $prevRow ) ] : [] )
69 ->caller( __METHOD__ )->fetchResultSet();
70
71 foreach ( $res as $row ) {
72 // Only attempt write queries if the row or rd_interwiki/rd_fragment fields are missing
73 // (we don't include this condition in the query to avoid slow queries and bad estimates)
74 if ( $row->rd_from === null || $row->rd_interwiki === null || $row->rd_fragment === null ) {
75 RefreshLinks::fixRedirect( $this, $row->page_id );
76 $updated++;
77 }
78 }
79 if ( isset( $row ) ) {
80 // Update the conditions to select the next batch
81 foreach ( $index as $field ) {
82 $prevRow[ $field ] = $row->$field;
83 }
84 }
85
86 $this->waitForReplication();
87 $total += $res->numRows();
88 $this->output( "$updated/$total\n" );
89
90 } while ( $res->numRows() == $this->getBatchSize() );
91
92 $this->output( "Done, updated $updated of $total rows.\n" );
93 return true;
94 }
95}
96
97$maintClass = FixInconsistentRedirects::class;
98require_once RUN_MAINTENANCE_IF_MAIN;
Fix redirect pages with missing or incomplete row in the redirect table.
getUpdateKey()
Get the update key name to go in the update log table.
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
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.
setBatchSize( $s=0)