Putting .config-page-list (sidebar nav) floating to the right as part of the page...
[mediawiki.git] / maintenance / fixSlaveDesync.php
blob9605e161ffaebf9981c8a308b79f4bb7ace7cb65
1 <?php
2 /**
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
18 * @ingroup Maintenance
21 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
23 class FixSlaveDesync extends Maintenance {
24 public function __construct() {
25 parent::__construct();
26 $this->mDescription = "";
29 public function getDbType() {
30 return self::DB_ADMIN;
33 public function execute() {
34 $this->slaveIndexes = array();
35 for ( $i = 1; $i < wfGetLB()->getServerCount(); $i++ ) {
36 if ( wfGetLB()->isNonZeroLoad( $i ) ) {
37 $this->slaveIndexes[] = $i;
41 if ( $this->hasArg() ) {
42 $this->desyncFixPage( $this->getArg() );
43 } else {
44 $dbw = wfGetDB( DB_MASTER );
45 $maxPage = $dbw->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
46 $corrupt = $this->findPageLatestCorruption();
47 foreach ( $corrupt as $id => $dummy ) {
48 $this->desyncFixPage( $id );
53 /**
54 * Find all pages that have a corrupted page_latest
55 * @return array
57 private function findPageLatestCorruption() {
58 $desync = array();
59 $n = 0;
60 $dbw = wfGetDB( DB_MASTER );
61 $masterIDs = array();
62 $res = $dbw->select( 'page', array( 'page_id', 'page_latest' ), array( 'page_id<6054123' ), __METHOD__ );
63 $this->output( "Number of pages: " . $dbw->numRows( $res ) . "\n" );
64 foreach ( $res as $row ) {
65 $masterIDs[$row->page_id] = $row->page_latest;
66 if ( !( ++$n % 10000 ) ) {
67 $this->output( "$n\r" );
70 $this->output( "\n" );
72 foreach ( $this->slaveIndexes as $i ) {
73 $db = wfGetDB( $i );
74 $res = $db->select( 'page', array( 'page_id', 'page_latest' ), array( 'page_id<6054123' ), __METHOD__ );
75 foreach ( $res as $row ) {
76 if ( isset( $masterIDs[$row->page_id] ) && $masterIDs[$row->page_id] != $row->page_latest ) {
77 $desync[$row->page_id] = true;
78 $this->output( $row->page_id . "\t" );
82 $this->output( "\n" );
83 return $desync;
86 /**
87 * Fix a broken page entry
88 * @param $pageID int The page_id to fix
90 private function desyncFixPage( $pageID ) {
91 # Check for a corrupted page_latest
92 $dbw = wfGetDB( DB_MASTER );
93 $dbw->begin();
94 $realLatest = $dbw->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ),
95 __METHOD__, 'FOR UPDATE' );
96 # list( $masterFile, $masterPos ) = $dbw->getMasterPos();
97 $found = false;
98 foreach ( $this->slaveIndexes as $i ) {
99 $db = wfGetDB( $i );
101 if ( !$db->masterPosWait( $masterFile, $masterPos, 10 ) ) {
102 $this->output( "Slave is too lagged, aborting\n" );
103 $dbw->commit();
104 sleep(10);
105 return;
107 $latest = $db->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ), __METHOD__ );
108 $max = $db->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__ );
109 if ( $latest != $realLatest && $realLatest < $max ) {
110 $this->output( "page_latest corrupted in page $pageID, server $i\n" );
111 $found = true;
112 break;
115 if ( !$found ) {
116 $this->output( "page_id $pageID seems fine\n" );
117 $dbw->commit();
118 return;
121 # Find the missing revisions
122 $res = $dbw->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ),
123 __METHOD__, 'FOR UPDATE' );
124 $masterIDs = array();
125 foreach ( $res as $row ) {
126 $masterIDs[] = $row->rev_id;
129 $res = $db->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ), __METHOD__ );
130 $slaveIDs = array();
131 foreach ( $res as $row ) {
132 $slaveIDs[] = $row->rev_id;
134 if ( count( $masterIDs ) < count( $slaveIDs ) ) {
135 $missingIDs = array_diff( $slaveIDs, $masterIDs );
136 if ( count( $missingIDs ) ) {
137 $this->output( "Found " . count( $missingIDs ) . " lost in master, copying from slave... " );
138 $dbFrom = $db;
139 $found = true;
140 $toMaster = true;
141 } else {
142 $found = false;
144 } else {
145 $missingIDs = array_diff( $masterIDs, $slaveIDs );
146 if ( count( $missingIDs ) ) {
147 $this->output( "Found " . count( $missingIDs ) . " missing revision(s), copying from master... " );
148 $dbFrom = $dbw;
149 $found = true;
150 $toMaster = false;
151 } else {
152 $found = false;
156 if ( $found ) {
157 foreach ( $missingIDs as $rid ) {
158 $this->output( "$rid " );
159 # Revision
160 $row = $dbFrom->selectRow( 'revision', '*', array( 'rev_id' => $rid ), __METHOD__ );
161 if ( $toMaster ) {
162 $id = $dbw->selectField( 'revision', 'rev_id', array( 'rev_id' => $rid ),
163 __METHOD__, 'FOR UPDATE' );
164 if ( $id ) {
165 $this->output( "Revision already exists\n" );
166 $found = false;
167 break;
168 } else {
169 $dbw->insert( 'revision', get_object_vars( $row ), __METHOD__, 'IGNORE' );
171 } else {
172 foreach ( $this->slaveIndexes as $i ) {
173 $db = wfGetDB( $i );
174 $db->insert( 'revision', get_object_vars( $row ), __METHOD__, 'IGNORE' );
178 # Text
179 $row = $dbFrom->selectRow( 'text', '*', array( 'old_id' => $row->rev_text_id ), __METHOD__ );
180 if ( $toMaster ) {
181 $dbw->insert( 'text', get_object_vars( $row ), __METHOD__, 'IGNORE' );
182 } else {
183 foreach ( $this->slaveIndexes as $i ) {
184 $db = wfGetDB( $i );
185 $db->insert( 'text', get_object_vars( $row ), __METHOD__, 'IGNORE' );
189 $this->output( "done\n" );
192 if ( $found ) {
193 $this->output( "Fixing page_latest... " );
194 if ( $toMaster ) {
195 # $dbw->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), __METHOD__ );
196 } else {
197 foreach ( $this->slaveIndexes as $i ) {
198 $db = wfGetDB( $i );
199 $db->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), __METHOD__ );
202 $this->output( "done\n" );
204 $dbw->commit();
208 $maintClass = "FixSlaveDesync";
209 require_once( DO_MAINTENANCE );