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
19 * @ingroup Maintenance
22 // @codeCoverageIgnoreStart
23 require_once __DIR__
. '/Maintenance.php';
24 // @codeCoverageIgnoreEnd
26 use MediaWiki\Maintenance\Maintenance
;
27 use MediaWiki\Title\Title
;
30 * Maintenance script that clears rows of pages corrupted by MergeHistory, those
31 * pages 'exist' but have no visible revision.
33 * These pages are completely inaccessible via the UI due to revision/title mismatch
34 * exceptions in RevisionStore and elsewhere.
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).
41 * @see https://phabricator.wikimedia.org/T263340
42 * @see https://phabricator.wikimedia.org/T259022
44 class FixMergeHistoryCorruption
extends Maintenance
{
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' );
54 public function execute() {
55 $dbr = $this->getReplicaDB();
56 $dbw = $this->getPrimaryDB();
59 if ( $this->hasOption( 'dry-run' ) && $this->hasOption( 'delete' ) ) {
60 $this->fatalError( 'Cannot do both --dry-run and --delete.' );
61 } elseif ( $this->hasOption( 'delete' ) ) {
63 } elseif ( !$this->hasOption( 'dry-run' ) ) {
64 $this->fatalError( 'Either --dry-run or --delete must be specified.' );
67 $conds = [ 'page_id<>rev_page' ];
68 if ( $this->hasOption( 'ns' ) ) {
69 $conds['page_namespace'] = (int)$this->getOption( 'ns' );
72 $res = $dbr->newSelectQueryBuilder()
74 ->join( 'revision', null, 'page_latest=rev_id' )
75 ->fields( [ 'page_namespace', 'page_title', 'page_id' ] )
77 ->caller( __METHOD__
)
80 $count = $res->numRows();
83 $this->output( "Nothing was found, no page matches the criteria.\n" );
90 foreach ( $res as $row ) {
91 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
93 $this->output( "Skipping invalid title with page_id: $row->page_id\n" );
96 $titleText = $title->getPrefixedDBkey();
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)' )
103 ->where( [ 'rev_page' => $row->page_id
] )
104 ->caller( __METHOD__
)->fetchField();
108 $this->output( "Would delete $titleText with page_id: $row->page_id\n" );
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();
119 $this->output( "Would update page_id $row->page_id to page_latest $revId\n" );
121 $this->output( "Updating page_id $row->page_id to page_latest $revId\n" );
122 $dbw->newUpdateQueryBuilder()
124 ->set( [ 'page_latest' => $revId ] )
125 ->where( [ 'page_id' => $row->page_id
] )
126 ->caller( __METHOD__
)->execute();
133 $this->output( "Updated $numUpdated row(s), deleted $numDeleted row(s)\n" );
138 // @codeCoverageIgnoreStart
139 $maintClass = FixMergeHistoryCorruption
::class;
140 require_once RUN_MAINTENANCE_IF_MAIN
;
141 // @codeCoverageIgnoreEnd