MediaWiki master
fixMergeHistoryCorruption.php
Go to the documentation of this file.
1<?php
22// @codeCoverageIgnoreStart
23require_once __DIR__ . '/Maintenance.php';
24// @codeCoverageIgnoreEnd
25
28
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;
140require_once RUN_MAINTENANCE_IF_MAIN;
141// @codeCoverageIgnoreEnd
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:78