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