MediaWiki  1.23.2
fixSlaveDesync.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
32 class FixSlaveDesync extends Maintenance {
33  public function __construct() {
34  parent::__construct();
35  $this->mDescription = "";
36  }
37 
38  public function getDbType() {
39  return Maintenance::DB_ADMIN;
40  }
41 
42  public function execute() {
43  $this->slaveIndexes = array();
44  for ( $i = 1; $i < wfGetLB()->getServerCount(); $i++ ) {
45  if ( wfGetLB()->isNonZeroLoad( $i ) ) {
46  $this->slaveIndexes[] = $i;
47  }
48  }
49 
50  if ( $this->hasArg() ) {
51  $this->desyncFixPage( $this->getArg() );
52  } else {
53  $corrupt = $this->findPageLatestCorruption();
54  foreach ( $corrupt as $id => $dummy ) {
55  $this->desyncFixPage( $id );
56  }
57  }
58  }
59 
64  private function findPageLatestCorruption() {
65  $desync = array();
66  $n = 0;
67  $dbw = wfGetDB( DB_MASTER );
68  $masterIDs = array();
69  $res = $dbw->select( 'page', array( 'page_id', 'page_latest' ), array( 'page_id<6054123' ), __METHOD__ );
70  $this->output( "Number of pages: " . $res->numRows() . "\n" );
71  foreach ( $res as $row ) {
72  $masterIDs[$row->page_id] = $row->page_latest;
73  if ( !( ++$n % 10000 ) ) {
74  $this->output( "$n\r" );
75  }
76  }
77  $this->output( "\n" );
78 
79  foreach ( $this->slaveIndexes as $i ) {
80  $db = wfGetDB( $i );
81  $res = $db->select( 'page', array( 'page_id', 'page_latest' ), array( 'page_id<6054123' ), __METHOD__ );
82  foreach ( $res as $row ) {
83  if ( isset( $masterIDs[$row->page_id] ) && $masterIDs[$row->page_id] != $row->page_latest ) {
84  $desync[$row->page_id] = true;
85  $this->output( $row->page_id . "\t" );
86  }
87  }
88  }
89  $this->output( "\n" );
90  return $desync;
91  }
92 
97  private function desyncFixPage( $pageID ) {
98  # Check for a corrupted page_latest
99  $dbw = wfGetDB( DB_MASTER );
100  $dbw->begin( __METHOD__ );
101  $realLatest = $dbw->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ),
102  __METHOD__, 'FOR UPDATE' );
103  # list( $masterFile, $masterPos ) = $dbw->getMasterPos();
104  $found = false;
105  foreach ( $this->slaveIndexes as $i ) {
106  $db = wfGetDB( $i );
107  /*
108  if ( !$db->masterPosWait( $masterFile, $masterPos, 10 ) ) {
109  $this->output( "Slave is too lagged, aborting\n" );
110  $dbw->commit( __METHOD__ );
111  sleep(10);
112  return;
113  }*/
114  $latest = $db->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ), __METHOD__ );
115  $max = $db->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__ );
116  if ( $latest != $realLatest && $realLatest < $max ) {
117  $this->output( "page_latest corrupted in page $pageID, server $i\n" );
118  $found = true;
119  break;
120  }
121  }
122  if ( !$found ) {
123  $this->output( "page_id $pageID seems fine\n" );
124  $dbw->commit( __METHOD__ );
125  return;
126  }
127 
128  # Find the missing revisions
129  $res = $dbw->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ),
130  __METHOD__, 'FOR UPDATE' );
131  $masterIDs = array();
132  foreach ( $res as $row ) {
133  $masterIDs[] = $row->rev_id;
134  }
135 
136  $res = $dbw->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ), __METHOD__ );
137  $slaveIDs = array();
138  foreach ( $res as $row ) {
139  $slaveIDs[] = $row->rev_id;
140  }
141  if ( count( $masterIDs ) < count( $slaveIDs ) ) {
142  $missingIDs = array_diff( $slaveIDs, $masterIDs );
143  if ( count( $missingIDs ) ) {
144  $this->output( "Found " . count( $missingIDs ) . " lost in master, copying from slave... " );
145  $dbFrom = $dbw;
146  $found = true;
147  $toMaster = true;
148  } else {
149  $found = false;
150  }
151  } else {
152  $missingIDs = array_diff( $masterIDs, $slaveIDs );
153  if ( count( $missingIDs ) ) {
154  $this->output( "Found " . count( $missingIDs ) . " missing revision(s), copying from master... " );
155  $dbFrom = $dbw;
156  $found = true;
157  $toMaster = false;
158  } else {
159  $found = false;
160  }
161  }
162 
163  if ( $found ) {
164  foreach ( $missingIDs as $rid ) {
165  $this->output( "$rid " );
166  # Revision
167  $row = $dbFrom->selectRow( 'revision', '*', array( 'rev_id' => $rid ), __METHOD__ );
168  if ( $toMaster ) {
169  $id = $dbw->selectField( 'revision', 'rev_id', array( 'rev_id' => $rid ),
170  __METHOD__, 'FOR UPDATE' );
171  if ( $id ) {
172  $this->output( "Revision already exists\n" );
173  $found = false;
174  break;
175  } else {
176  $dbw->insert( 'revision', get_object_vars( $row ), __METHOD__, 'IGNORE' );
177  }
178  } else {
179  foreach ( $this->slaveIndexes as $i ) {
180  $db = wfGetDB( $i );
181  $db->insert( 'revision', get_object_vars( $row ), __METHOD__, 'IGNORE' );
182  }
183  }
184 
185  # Text
186  $row = $dbFrom->selectRow( 'text', '*', array( 'old_id' => $row->rev_text_id ), __METHOD__ );
187  if ( $toMaster ) {
188  $dbw->insert( 'text', get_object_vars( $row ), __METHOD__, 'IGNORE' );
189  } else {
190  foreach ( $this->slaveIndexes as $i ) {
191  $db = wfGetDB( $i );
192  $db->insert( 'text', get_object_vars( $row ), __METHOD__, 'IGNORE' );
193  }
194  }
195  }
196  $this->output( "done\n" );
197  }
198 
199  if ( $found ) {
200  $this->output( "Fixing page_latest... " );
201  if ( $toMaster ) {
202  # $dbw->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), __METHOD__ );
203  } else {
204  foreach ( $this->slaveIndexes as $i ) {
205  $db = wfGetDB( $i );
206  $db->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), __METHOD__ );
207  }
208  }
209  $this->output( "done\n" );
210  }
211  $dbw->commit( __METHOD__ );
212  }
213 }
214 
215 $maintClass = "FixSlaveDesync";
216 require_once RUN_MAINTENANCE_IF_MAIN;
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
$maintClass
$maintClass
Definition: fixSlaveDesync.php:215
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfGetLB
wfGetLB( $wiki=false)
Get a load balancer object.
Definition: GlobalFunctions.php:3660
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
$n
$n
Definition: RandomTest.php:76
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance\hasArg
hasArg( $argId=0)
Does a given argument exist?
Definition: Maintenance.php:236
FixSlaveDesync
Maintenance script that fixes erroneous page_latest values due to slave desynchronisation.
Definition: fixSlaveDesync.php:32
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
FixSlaveDesync\findPageLatestCorruption
findPageLatestCorruption()
Find all pages that have a corrupted page_latest.
Definition: fixSlaveDesync.php:64
FixSlaveDesync\__construct
__construct()
Default constructor.
Definition: fixSlaveDesync.php:33
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
Maintenance\DB_ADMIN
const DB_ADMIN
Definition: Maintenance.php:59
FixSlaveDesync\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: fixSlaveDesync.php:38
FixSlaveDesync\desyncFixPage
desyncFixPage( $pageID)
Fix a broken page entry.
Definition: fixSlaveDesync.php:97
FixSlaveDesync\execute
execute()
Do the actual work.
Definition: fixSlaveDesync.php:42
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:246
$res
$res
Definition: database.txt:21