Debian Codegraph @

maintenance/fixMergeHistoryCorruption.php

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