Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / maintenance / fixInconsistentRedirects.php
blobe84f60516c5fff1c2e81aba130e8833d183a381b
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\Maintenance\LoggedUpdateMaintenance;
23 // @codeCoverageIgnoreStart
24 require_once __DIR__ . '/Maintenance.php';
25 // @codeCoverageIgnoreEnd
27 /**
28 * Fix redirect pages with missing or incomplete row in the redirect table.
30 * @ingroup Maintenance
31 * @since 1.41
33 class FixInconsistentRedirects extends LoggedUpdateMaintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Fix redirect pages with missing or incomplete row in the redirect table' );
38 $this->setBatchSize( 100 );
41 protected function getUpdateKey() {
42 return __CLASS__;
45 protected function doDBUpdates() {
46 $dbr = $this->getReplicaDB();
48 $builder = $dbr->newSelectQueryBuilder()
49 ->caller( __METHOD__ )
50 ->from( 'page' )
51 ->where( [ 'page_is_redirect' => 1 ] );
53 $this->output( "Fixing inconsistent redirects ...\n" );
55 $estimateCount = $builder->estimateRowCount();
56 $this->output( "Estimated redirect page count: $estimateCount\n" );
58 $builder
59 ->limit( $this->getBatchSize() )
60 ->leftJoin( 'redirect', null, 'page_id=rd_from' )
61 ->select( [ 'rd_from', 'rd_interwiki', 'rd_fragment' ] );
63 // Using the page_redirect_namespace_len index to skip non-redirects
64 $index = [ 'page_is_redirect', 'page_namespace', 'page_len', 'page_id' ];
65 $builder->select( $index )->orderBy( $index );
66 $prevRow = [];
68 $total = 0;
69 $updated = 0;
70 do {
71 $res = ( clone $builder )
72 ->where( $prevRow ? [ $dbr->buildComparison( '>', $prevRow ) ] : [] )
73 ->caller( __METHOD__ )->fetchResultSet();
75 foreach ( $res as $row ) {
76 // Only attempt write queries if the row or rd_interwiki/rd_fragment fields are missing
77 // (we don't include this condition in the query to avoid slow queries and bad estimates)
78 if ( $row->rd_from === null || $row->rd_interwiki === null || $row->rd_fragment === null ) {
79 RefreshLinks::fixRedirect( $this, $row->page_id );
80 $updated++;
83 if ( isset( $row ) ) {
84 // Update the conditions to select the next batch
85 foreach ( $index as $field ) {
86 $prevRow[ $field ] = $row->$field;
90 $this->waitForReplication();
91 $total += $res->numRows();
92 $this->output( "$updated/$total\n" );
94 } while ( $res->numRows() == $this->getBatchSize() );
96 $this->output( "Done, updated $updated of $total rows.\n" );
97 return true;
101 // @codeCoverageIgnoreStart
102 $maintClass = FixInconsistentRedirects::class;
103 require_once RUN_MAINTENANCE_IF_MAIN;
104 // @codeCoverageIgnoreEnd