Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FixMergeHistoryCorruption
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 2
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
182
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Maintenance
20 */
21
22require_once __DIR__ . '/Maintenance.php';
23
24use MediaWiki\Title\Title;
25
26/**
27 * Maintenance script that clears rows of pages corrupted by MergeHistory, those
28 * pages 'exist' but have no visible revision.
29 *
30 * These pages are completely inaccessible via the UI due to revision/title mismatch
31 * exceptions in RevisionStore and elsewhere.
32 *
33 * These are rows in page_table that have 'page_latest' entry with corresponding
34 * 'rev_id' but no associated 'rev_page' entry in revision table. Such rows create
35 * ghost pages because their 'page_latest' is actually living on different pages
36 * (which possess the associated 'rev_page' on revision table now).
37 *
38 * @see https://phabricator.wikimedia.org/T263340
39 * @see https://phabricator.wikimedia.org/T259022
40 */
41class FixMergeHistoryCorruption extends Maintenance {
42
43    public function __construct() {
44        parent::__construct();
45        $this->addDescription( 'Delete pages corrupted by MergeHistory' );
46        $this->addOption( 'ns', 'Namespace to restrict the query', false, true );
47        $this->addOption( 'dry-run', 'Run in dry-mode' );
48        $this->addOption( 'delete', 'Actually delete the found rows' );
49    }
50
51    public function execute() {
52        $dbr = $this->getReplicaDB();
53        $dbw = $this->getPrimaryDB();
54
55        $dryRun = true;
56        if ( $this->hasOption( 'dry-run' ) && $this->hasOption( 'delete' ) ) {
57            $this->fatalError( 'Cannot do both --dry-run and --delete.' );
58        } elseif ( $this->hasOption( 'delete' ) ) {
59            $dryRun = false;
60        } elseif ( !$this->hasOption( 'dry-run' ) ) {
61            $this->fatalError( 'Either --dry-run or --delete must be specified.' );
62        }
63
64        $conds = [ 'page_id<>rev_page' ];
65        if ( $this->hasOption( 'ns' ) ) {
66            $conds['page_namespace'] = (int)$this->getOption( 'ns' );
67        }
68
69        $res = $dbr->newSelectQueryBuilder()
70            ->from( 'page' )
71            ->join( 'revision', null, 'page_latest=rev_id' )
72            ->fields( [ 'page_namespace', 'page_title', 'page_id' ] )
73            ->where( $conds )
74            ->caller( __METHOD__ )
75            ->fetchResultSet();
76
77        $count = $res->numRows();
78
79        if ( !$count ) {
80            $this->output( "Nothing was found, no page matches the criteria.\n" );
81            return;
82        }
83
84        $numDeleted = 0;
85        $numUpdated = 0;
86
87        foreach ( $res as $row ) {
88            $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
89            if ( !$title ) {
90                $this->output( "Skipping invalid title with page_id: $row->page_id\n" );
91                continue;
92            }
93            $titleText = $title->getPrefixedDBkey();
94
95            // Check if there are any revisions that have this $row->page_id as their
96            // rev_page and select the largest which should be the newest revision.
97            $revId = $dbr->newSelectQueryBuilder()
98                ->select( 'MAX(rev_id)' )
99                ->from( 'revision' )
100                ->where( [ 'rev_page' => $row->page_id ] )
101                ->caller( __METHOD__ )->fetchField();
102
103            if ( !$revId ) {
104                if ( $dryRun ) {
105                    $this->output( "Would delete $titleText with page_id: $row->page_id\n" );
106                } else {
107                    $this->output( "Deleting $titleText with page_id: $row->page_id\n" );
108                    $dbw->newDeleteQueryBuilder()
109                        ->deleteFrom( 'page' )
110                        ->where( [ 'page_id' => $row->page_id ] )
111                        ->caller( __METHOD__ )->execute();
112                }
113                $numDeleted++;
114            } else {
115                if ( $dryRun ) {
116                    $this->output( "Would update page_id $row->page_id to page_latest $revId\n" );
117                } else {
118                    $this->output( "Updating page_id $row->page_id to page_latest $revId\n" );
119                    $dbw->newUpdateQueryBuilder()
120                        ->update( 'page' )
121                        ->set( [ 'page_latest' => $revId ] )
122                        ->where( [ 'page_id' => $row->page_id ] )
123                        ->caller( __METHOD__ )->execute();
124                }
125                $numUpdated++;
126            }
127        }
128
129        if ( !$dryRun ) {
130            $this->output( "Updated $numUpdated row(s), deleted $numDeleted row(s)\n" );
131        }
132    }
133}
134
135$maintClass = FixMergeHistoryCorruption::class;
136require_once RUN_MAINTENANCE_IF_MAIN;