MediaWiki master
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->getReplicaDB();
53 $dbw = $this->getPrimaryDB();
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->newSelectQueryBuilder()
98 ->select( 'MAX(rev_id)' )
99 ->from( 'revision' )
100 ->where( [ 'rev_page' => $row->page_id ] )
101 ->caller( __METHOD__ )->fetchField();
102
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->newDeleteQueryBuilder()
109 ->deleteFrom( 'page' )
110 ->where( [ 'page_id' => $row->page_id ] )
111 ->caller( __METHOD__ )->execute();
112 }
113 $numDeleted++;
114 } else {
115 if ( $dryRun ) {
116 $this->output( "Would update page_id $row->page_id to page_latest $revId\n" );
117 } else {
118 $this->output( "Updating page_id $row->page_id to page_latest $revId\n" );
119 $dbw->newUpdateQueryBuilder()
120 ->update( 'page' )
121 ->set( [ 'page_latest' => $revId ] )
122 ->where( [ 'page_id' => $row->page_id ] )
123 ->caller( __METHOD__ )->execute();
124 }
125 $numUpdated++;
126 }
127 }
128
129 if ( !$dryRun ) {
130 $this->output( "Updated $numUpdated row(s), deleted $numDeleted row(s)\n" );
131 }
132 }
133}
134
135$maintClass = FixMergeHistoryCorruption::class;
136require_once RUN_MAINTENANCE_IF_MAIN;
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:78