MediaWiki REL1_40
fixMergeHistoryCorruption.php
Go to the documentation of this file.
1<?php
22require_once __DIR__ . '/Maintenance.php';
23
25
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->getDB( DB_REPLICA );
53 $dbw = $this->getDB( DB_PRIMARY );
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->selectField(
98 'revision',
99 'MAX(rev_id)',
100 [ 'rev_page' => $row->page_id ],
101 __METHOD__
102 );
103
104 if ( !$revId ) {
105 if ( $dryRun ) {
106 $this->output( "Would delete $titleText with page_id: $row->page_id\n" );
107 } else {
108 $this->output( "Deleting $titleText with page_id: $row->page_id\n" );
109 $dbw->delete( 'page', [ 'page_id' => $row->page_id ], __METHOD__ );
110 }
111 $numDeleted++;
112 } else {
113 if ( $dryRun ) {
114 $this->output( "Would update page_id $row->page_id to page_latest $revId\n" );
115 } else {
116 $this->output( "Updating page_id $row->page_id to page_latest $revId\n" );
117 $dbw->update(
118 'page',
119 [ 'page_latest' => $revId ],
120 [ 'page_id' => $row->page_id ],
121 __METHOD__
122 );
123 }
124 $numUpdated++;
125 }
126 }
127
128 if ( !$dryRun ) {
129 $this->output( "Updated $numUpdated row(s), deleted $numDeleted row(s)\n" );
130 }
131 }
132}
133
134$maintClass = FixMergeHistoryCorruption::class;
135require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Maintenance script that clears rows of pages corrupted by MergeHistory, those pages 'exist' but have ...
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Represents a title within MediaWiki.
Definition Title.php:82
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28