Update git submodules
[mediawiki.git] / maintenance / fixMergeHistoryCorruption.php
blob1db53c24968ed50ee19b3ee2dd68d1f21faf9fb9
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
19 * @ingroup Maintenance
22 require_once __DIR__ . '/Maintenance.php';
24 use MediaWiki\Title\Title;
26 /**
27 * Maintenance script that clears rows of pages corrupted by MergeHistory, those
28 * pages 'exist' but have no visible revision.
30 * These pages are completely inaccessible via the UI due to revision/title mismatch
31 * exceptions in RevisionStore and elsewhere.
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).
38 * @see https://phabricator.wikimedia.org/T263340
39 * @see https://phabricator.wikimedia.org/T259022
41 class FixMergeHistoryCorruption extends Maintenance {
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' );
51 public function execute() {
52 $dbr = $this->getDB( DB_REPLICA );
53 $dbw = $this->getDB( DB_PRIMARY );
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.' );
64 $conds = [ 'page_id<>rev_page' ];
65 if ( $this->hasOption( 'ns' ) ) {
66 $conds['page_namespace'] = (int)$this->getOption( 'ns' );
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();
77 $count = $res->numRows();
79 if ( !$count ) {
80 $this->output( "Nothing was found, no page matches the criteria.\n" );
81 return;
84 $numDeleted = 0;
85 $numUpdated = 0;
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;
93 $titleText = $title->getPrefixedDBkey();
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();
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->delete( 'page', [ 'page_id' => $row->page_id ], __METHOD__ );
110 $numDeleted++;
111 } else {
112 if ( $dryRun ) {
113 $this->output( "Would update page_id $row->page_id to page_latest $revId\n" );
114 } else {
115 $this->output( "Updating page_id $row->page_id to page_latest $revId\n" );
116 $dbw->update(
117 'page',
118 [ 'page_latest' => $revId ],
119 [ 'page_id' => $row->page_id ],
120 __METHOD__
123 $numUpdated++;
127 if ( !$dryRun ) {
128 $this->output( "Updated $numUpdated row(s), deleted $numDeleted row(s)\n" );
133 $maintClass = FixMergeHistoryCorruption::class;
134 require_once RUN_MAINTENANCE_IF_MAIN;