8 require_once( 'commandLine.inc' );
10 //$wgDebugLogFile = '/dev/stdout';
12 $slaveIndexes = array();
13 for ( $i = 1; $i < count( $wgDBservers ); $i++
) {
14 if ( wfGetLB()->isNonZeroLoad( $i ) ) {
19 foreach ( wfGetLB()->mServers as $i => $server ) {
20 wfGetLB()->mServers[$i]['flags'] |= DBO_DEBUG;
22 $reportingInterval = 1000;
24 if ( isset( $args[0] ) ) {
25 desyncFixPage( $args[0] );
27 $dbw = wfGetDB( DB_MASTER
);
28 $maxPage = $dbw->selectField( 'page', 'MAX(page_id)', false, 'fixDesync.php' );
29 $corrupt = findPageLatestCorruption();
30 foreach ( $corrupt as $id => $dummy ) {
34 for ( $i=1; $i <= $maxPage; $i++ ) {
36 if ( !($i % $reportingInterval) ) {
42 function findPageLatestCorruption() {
45 $dbw = wfGetDB( DB_MASTER
);
47 $res = $dbw->select( 'page', array( 'page_id', 'page_latest' ), array( 'page_id<6054123' ), __METHOD__
);
48 print "Number of pages: " . $dbw->numRows( $res ) . "\n";
49 while ( $row = $dbw->fetchObject( $res ) ) {
50 $masterIDs[$row->page_id
] = $row->page_latest
;
51 if ( !( ++
$n %
10000 ) ) {
56 $dbw->freeResult( $res );
59 foreach ( $slaveIndexes as $i ) {
61 $res = $db->select( 'page', array( 'page_id', 'page_latest' ), array( 'page_id<6054123' ), __METHOD__
);
62 while ( $row = $db->fetchObject( $res ) ) {
63 if ( isset( $masterIDs[$row->page_id
] ) && $masterIDs[$row->page_id
] != $row->page_latest
) {
64 $desync[$row->page_id
] = true;
65 print $row->page_id
. "\t";
68 $db->freeResult( $res );
74 function desyncFixPage( $pageID ) {
76 $fname = 'desyncFixPage';
78 # Check for a corrupted page_latest
79 $dbw = wfGetDB( DB_MASTER
);
81 $realLatest = $dbw->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ),
82 $fname, 'FOR UPDATE' );
83 #list( $masterFile, $masterPos ) = $dbw->getMasterPos();
85 foreach ( $slaveIndexes as $i ) {
88 if ( !$db->masterPosWait( $masterFile, $masterPos, 10 ) ) {
89 echo "Slave is too lagged, aborting\n";
94 $latest = $db->selectField( 'page', 'page_latest', array( 'page_id' => $pageID ), $fname );
95 $max = $db->selectField( 'revision', 'MAX(rev_id)', false, $fname );
96 if ( $latest != $realLatest && $realLatest < $max ) {
97 print "page_latest corrupted in page $pageID, server $i\n";
103 print "page_id $pageID seems fine\n";
108 # Find the missing revisions
109 $res = $dbw->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ),
110 $fname, 'FOR UPDATE' );
111 $masterIDs = array();
112 while ( $row = $dbw->fetchObject( $res ) ) {
113 $masterIDs[] = $row->rev_id
;
115 $dbw->freeResult( $res );
117 $res = $db->select( 'revision', array( 'rev_id' ), array( 'rev_page' => $pageID ), $fname );
119 while ( $row = $db->fetchObject( $res ) ) {
120 $slaveIDs[] = $row->rev_id
;
122 $db->freeResult( $res );
123 if ( count( $masterIDs ) < count( $slaveIDs ) ) {
124 $missingIDs = array_diff( $slaveIDs, $masterIDs );
125 if ( count( $missingIDs ) ) {
126 print "Found " . count( $missingIDs ) . " lost in master, copying from slave... ";
134 $missingIDs = array_diff( $masterIDs, $slaveIDs );
135 if ( count( $missingIDs ) ) {
136 print "Found " . count( $missingIDs ) . " missing revision(s), copying from master... ";
146 foreach ( $missingIDs as $rid ) {
149 $row = $dbFrom->selectRow( 'revision', '*', array( 'rev_id' => $rid ), $fname );
151 $id = $dbw->selectField( 'revision', 'rev_id', array( 'rev_id' => $rid ),
152 $fname, 'FOR UPDATE' );
154 echo "Revision already exists\n";
158 $dbw->insert( 'revision', get_object_vars( $row ), $fname, 'IGNORE' );
161 foreach ( $slaveIndexes as $i ) {
163 $db->insert( 'revision', get_object_vars( $row ), $fname, 'IGNORE' );
168 $row = $dbFrom->selectRow( 'text', '*', array( 'old_id' => $row->rev_text_id
), $fname );
170 $dbw->insert( 'text', get_object_vars( $row ), $fname, 'IGNORE' );
172 foreach ( $slaveIndexes as $i ) {
174 $db->insert( 'text', get_object_vars( $row ), $fname, 'IGNORE' );
182 print "Fixing page_latest... ";
184 #$dbw->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), $fname );
186 foreach ( $slaveIndexes as $i ) {
188 $db->update( 'page', array( 'page_latest' => $realLatest ), array( 'page_id' => $pageID ), $fname );