Update git submodules
[mediawiki.git] / maintenance / fixInconsistentRedirects.php
blob689453c6783c78cddccea2226e4fcf96676084f8
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 require_once __DIR__ . '/Maintenance.php';
23 /**
24 * Fix redirect pages with missing or incomplete row in the redirect table.
26 * @ingroup Maintenance
27 * @since 1.41
29 class FixInconsistentRedirects extends LoggedUpdateMaintenance {
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 );
37 protected function getUpdateKey() {
38 return __CLASS__;
41 protected function doDBUpdates() {
42 $dbr = $this->getDB( DB_REPLICA );
44 $builder = $dbr->newSelectQueryBuilder()
45 ->from( 'page' )
46 ->where( [ 'page_is_redirect' => 1 ] );
48 $this->output( "Fixing inconsistent redirects ...\n" );
50 $estimateCount = $builder->estimateRowCount();
51 $this->output( "Estimated redirect page count: $estimateCount\n" );
53 $builder
54 ->limit( $this->getBatchSize() )
55 ->leftJoin( 'redirect', null, 'page_id=rd_from' )
56 ->select( [ 'rd_from', 'rd_interwiki', 'rd_fragment' ] );
58 // Using the page_redirect_namespace_len index to skip non-redirects
59 $index = [ 'page_is_redirect', 'page_namespace', 'page_len', 'page_id' ];
60 $builder->select( $index )->orderBy( $index );
61 $prevRow = [];
63 $total = 0;
64 $updated = 0;
65 do {
66 $res = ( clone $builder )
67 ->where( $prevRow ? [ $dbr->buildComparison( '>', $prevRow ) ] : [] )
68 ->caller( __METHOD__ )->fetchResultSet();
70 foreach ( $res as $row ) {
71 // Only attempt write queries if the row or rd_interwiki/rd_fragment fields are missing
72 // (we don't include this condition in the query to avoid slow queries and bad estimates)
73 if ( $row->rd_from === null || $row->rd_interwiki === null || $row->rd_fragment === null ) {
74 RefreshLinks::fixRedirect( $this, $row->page_id );
75 $updated++;
78 if ( isset( $row ) ) {
79 // Update the conditions to select the next batch
80 foreach ( $index as $field ) {
81 $prevRow[ $field ] = $row->$field;
85 $this->waitForReplication();
86 $total += $res->numRows();
87 $this->output( "$updated/$total\n" );
89 } while ( $res->numRows() == $this->getBatchSize() );
91 $this->output( "Done, updated $updated of $total rows.\n" );
92 return true;
96 $maintClass = FixInconsistentRedirects::class;
97 require_once RUN_MAINTENANCE_IF_MAIN;