MediaWiki
REL1_24
|
00001 <?php 00024 if ( !defined( 'MEDIAWIKI' ) ) { 00025 require_once __DIR__ . '/../commandLine.inc'; 00026 00027 $cs = new CheckStorage; 00028 $fix = isset( $options['fix'] ); 00029 if ( isset( $args[0] ) ) { 00030 $xml = $args[0]; 00031 } else { 00032 $xml = false; 00033 } 00034 $cs->check( $fix, $xml ); 00035 } 00036 00037 // ---------------------------------------------------------------------------------- 00038 00044 class CheckStorage { 00045 const CONCAT_HEADER = 'O:27:"concatenatedgziphistoryblob"'; 00046 public $oldIdMap, $errors; 00047 public $dbStore = null; 00048 00049 public $errorDescriptions = array( 00050 'restore text' => 'Damaged text, need to be restored from a backup', 00051 'restore revision' => 'Damaged revision row, need to be restored from a backup', 00052 'unfixable' => 'Unexpected errors with no automated fixing method', 00053 'fixed' => 'Errors already fixed', 00054 'fixable' => 'Errors which would already be fixed if --fix was specified', 00055 ); 00056 00057 function check( $fix = false, $xml = '' ) { 00058 $dbr = wfGetDB( DB_SLAVE ); 00059 if ( $fix ) { 00060 print "Checking, will fix errors if possible...\n"; 00061 } else { 00062 print "Checking...\n"; 00063 } 00064 $maxRevId = $dbr->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__ ); 00065 $chunkSize = 1000; 00066 $flagStats = array(); 00067 $objectStats = array(); 00068 $knownFlags = array( 'external', 'gzip', 'object', 'utf-8' ); 00069 $this->errors = array( 00070 'restore text' => array(), 00071 'restore revision' => array(), 00072 'unfixable' => array(), 00073 'fixed' => array(), 00074 'fixable' => array(), 00075 ); 00076 00077 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) { 00078 $chunkEnd = $chunkStart + $chunkSize - 1; 00079 // print "$chunkStart of $maxRevId\n"; 00080 00081 // Fetch revision rows 00082 $this->oldIdMap = array(); 00083 $dbr->ping(); 00084 $res = $dbr->select( 'revision', array( 'rev_id', 'rev_text_id' ), 00085 array( "rev_id BETWEEN $chunkStart AND $chunkEnd" ), __METHOD__ ); 00086 foreach ( $res as $row ) { 00087 $this->oldIdMap[$row->rev_id] = $row->rev_text_id; 00088 } 00089 $dbr->freeResult( $res ); 00090 00091 if ( !count( $this->oldIdMap ) ) { 00092 continue; 00093 } 00094 00095 // Fetch old_flags 00096 $missingTextRows = array_flip( $this->oldIdMap ); 00097 $externalRevs = array(); 00098 $objectRevs = array(); 00099 $res = $dbr->select( 'text', array( 'old_id', 'old_flags' ), 00100 'old_id IN (' . implode( ',', $this->oldIdMap ) . ')', __METHOD__ ); 00101 foreach ( $res as $row ) { 00105 $flags = $row->old_flags; 00106 $id = $row->old_id; 00107 00108 // Create flagStats row if it doesn't exist 00109 $flagStats = $flagStats + array( $flags => 0 ); 00110 // Increment counter 00111 $flagStats[$flags]++; 00112 00113 // Not missing 00114 unset( $missingTextRows[$row->old_id] ); 00115 00116 // Check for external or object 00117 if ( $flags == '' ) { 00118 $flagArray = array(); 00119 } else { 00120 $flagArray = explode( ',', $flags ); 00121 } 00122 if ( in_array( 'external', $flagArray ) ) { 00123 $externalRevs[] = $id; 00124 } elseif ( in_array( 'object', $flagArray ) ) { 00125 $objectRevs[] = $id; 00126 } 00127 00128 // Check for unrecognised flags 00129 if ( $flags == '0' ) { 00130 // This is a known bug from 2004 00131 // It's safe to just erase the old_flags field 00132 if ( $fix ) { 00133 $this->error( 'fixed', "Warning: old_flags set to 0", $id ); 00134 $dbw = wfGetDB( DB_MASTER ); 00135 $dbw->ping(); 00136 $dbw->update( 'text', array( 'old_flags' => '' ), 00137 array( 'old_id' => $id ), __METHOD__ ); 00138 echo "Fixed\n"; 00139 } else { 00140 $this->error( 'fixable', "Warning: old_flags set to 0", $id ); 00141 } 00142 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) { 00143 $this->error( 'unfixable', "Error: invalid flags field \"$flags\"", $id ); 00144 } 00145 } 00146 $dbr->freeResult( $res ); 00147 00148 // Output errors for any missing text rows 00149 foreach ( $missingTextRows as $oldId => $revId ) { 00150 $this->error( 'restore revision', "Error: missing text row", $oldId ); 00151 } 00152 00153 // Verify external revisions 00154 $externalConcatBlobs = array(); 00155 $externalNormalBlobs = array(); 00156 if ( count( $externalRevs ) ) { 00157 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ), 00158 array( 'old_id IN (' . implode( ',', $externalRevs ) . ')' ), __METHOD__ ); 00159 foreach ( $res as $row ) { 00160 $urlParts = explode( '://', $row->old_text, 2 ); 00161 if ( count( $urlParts ) !== 2 || $urlParts[1] == '' ) { 00162 $this->error( 'restore text', "Error: invalid URL \"{$row->old_text}\"", $row->old_id ); 00163 continue; 00164 } 00165 list( $proto, ) = $urlParts; 00166 if ( $proto != 'DB' ) { 00167 $this->error( 'restore text', "Error: invalid external protocol \"$proto\"", $row->old_id ); 00168 continue; 00169 } 00170 $path = explode( '/', $row->old_text ); 00171 $cluster = $path[2]; 00172 $id = $path[3]; 00173 if ( isset( $path[4] ) ) { 00174 $externalConcatBlobs[$cluster][$id][] = $row->old_id; 00175 } else { 00176 $externalNormalBlobs[$cluster][$id][] = $row->old_id; 00177 } 00178 } 00179 $dbr->freeResult( $res ); 00180 } 00181 00182 // Check external concat blobs for the right header 00183 $this->checkExternalConcatBlobs( $externalConcatBlobs ); 00184 00185 // Check external normal blobs for existence 00186 if ( count( $externalNormalBlobs ) ) { 00187 if ( is_null( $this->dbStore ) ) { 00188 $this->dbStore = new ExternalStoreDB; 00189 } 00190 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) { 00191 $blobIds = array_keys( $xBlobIds ); 00192 $extDb =& $this->dbStore->getSlave( $cluster ); 00193 $blobsTable = $this->dbStore->getTable( $extDb ); 00194 $res = $extDb->select( $blobsTable, 00195 array( 'blob_id' ), 00196 array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), __METHOD__ ); 00197 foreach ( $res as $row ) { 00198 unset( $xBlobIds[$row->blob_id] ); 00199 } 00200 $extDb->freeResult( $res ); 00201 // Print errors for missing blobs rows 00202 foreach ( $xBlobIds as $blobId => $oldId ) { 00203 $this->error( 'restore text', "Error: missing target $blobId for one-part ES URL", $oldId ); 00204 } 00205 } 00206 } 00207 00208 // Check local objects 00209 $dbr->ping(); 00210 $concatBlobs = array(); 00211 $curIds = array(); 00212 if ( count( $objectRevs ) ) { 00213 $headerLength = 300; 00214 $res = $dbr->select( 00215 'text', 00216 array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ), 00217 array( 'old_id IN (' . implode( ',', $objectRevs ) . ')' ), 00218 __METHOD__ 00219 ); 00220 foreach ( $res as $row ) { 00221 $oldId = $row->old_id; 00222 $matches = array(); 00223 if ( !preg_match( '/^O:(\d+):"(\w+)"/', $row->header, $matches ) ) { 00224 $this->error( 'restore text', "Error: invalid object header", $oldId ); 00225 continue; 00226 } 00227 00228 $className = strtolower( $matches[2] ); 00229 if ( strlen( $className ) != $matches[1] ) { 00230 $this->error( 00231 'restore text', 00232 "Error: invalid object header, wrong class name length", 00233 $oldId 00234 ); 00235 continue; 00236 } 00237 00238 $objectStats = $objectStats + array( $className => 0 ); 00239 $objectStats[$className]++; 00240 00241 switch ( $className ) { 00242 case 'concatenatedgziphistoryblob': 00243 // Good 00244 break; 00245 case 'historyblobstub': 00246 case 'historyblobcurstub': 00247 if ( strlen( $row->header ) == $headerLength ) { 00248 $this->error( 'unfixable', "Error: overlong stub header", $oldId ); 00249 continue; 00250 } 00251 $stubObj = unserialize( $row->header ); 00252 if ( !is_object( $stubObj ) ) { 00253 $this->error( 'restore text', "Error: unable to unserialize stub object", $oldId ); 00254 continue; 00255 } 00256 if ( $className == 'historyblobstub' ) { 00257 $concatBlobs[$stubObj->mOldId][] = $oldId; 00258 } else { 00259 $curIds[$stubObj->mCurId][] = $oldId; 00260 } 00261 break; 00262 default: 00263 $this->error( 'unfixable', "Error: unrecognised object class \"$className\"", $oldId ); 00264 } 00265 } 00266 $dbr->freeResult( $res ); 00267 } 00268 00269 // Check local concat blob validity 00270 $externalConcatBlobs = array(); 00271 if ( count( $concatBlobs ) ) { 00272 $headerLength = 300; 00273 $res = $dbr->select( 00274 'text', 00275 array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ), 00276 array( 'old_id IN (' . implode( ',', array_keys( $concatBlobs ) ) . ')' ), 00277 __METHOD__ 00278 ); 00279 foreach ( $res as $row ) { 00280 $flags = explode( ',', $row->old_flags ); 00281 if ( in_array( 'external', $flags ) ) { 00282 // Concat blob is in external storage? 00283 if ( in_array( 'object', $flags ) ) { 00284 $urlParts = explode( '/', $row->header ); 00285 if ( $urlParts[0] != 'DB:' ) { 00286 $this->error( 00287 'unfixable', 00288 "Error: unrecognised external storage type \"{$urlParts[0]}", 00289 $row->old_id 00290 ); 00291 } else { 00292 $cluster = $urlParts[2]; 00293 $id = $urlParts[3]; 00294 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) { 00295 $externalConcatBlobs[$cluster][$id] = array(); 00296 } 00297 $externalConcatBlobs[$cluster][$id] = array_merge( 00298 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id] 00299 ); 00300 } 00301 } else { 00302 $this->error( 00303 'unfixable', 00304 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}", 00305 $concatBlobs[$row->old_id] ); 00306 } 00307 } elseif ( strcasecmp( 00308 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ), 00309 self::CONCAT_HEADER 00310 ) ) { 00311 $this->error( 00312 'restore text', 00313 "Error: Incorrect object header for concat bulk row {$row->old_id}", 00314 $concatBlobs[$row->old_id] 00315 ); 00316 } # else good 00317 00318 unset( $concatBlobs[$row->old_id] ); 00319 } 00320 $dbr->freeResult( $res ); 00321 } 00322 00323 // Check targets of unresolved stubs 00324 $this->checkExternalConcatBlobs( $externalConcatBlobs ); 00325 // next chunk 00326 } 00327 00328 print "\n\nErrors:\n"; 00329 foreach ( $this->errors as $name => $errors ) { 00330 if ( count( $errors ) ) { 00331 $description = $this->errorDescriptions[$name]; 00332 echo "$description: " . implode( ',', array_keys( $errors ) ) . "\n"; 00333 } 00334 } 00335 00336 if ( count( $this->errors['restore text'] ) && $fix ) { 00337 if ( (string)$xml !== '' ) { 00338 $this->restoreText( array_keys( $this->errors['restore text'] ), $xml ); 00339 } else { 00340 echo "Can't fix text, no XML backup specified\n"; 00341 } 00342 } 00343 00344 print "\nFlag statistics:\n"; 00345 $total = array_sum( $flagStats ); 00346 foreach ( $flagStats as $flag => $count ) { 00347 printf( "%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 ); 00348 } 00349 print "\nLocal object statistics:\n"; 00350 $total = array_sum( $objectStats ); 00351 foreach ( $objectStats as $className => $count ) { 00352 printf( "%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 ); 00353 } 00354 } 00355 00356 function error( $type, $msg, $ids ) { 00357 if ( is_array( $ids ) && count( $ids ) == 1 ) { 00358 $ids = reset( $ids ); 00359 } 00360 if ( is_array( $ids ) ) { 00361 $revIds = array(); 00362 foreach ( $ids as $id ) { 00363 $revIds = array_merge( $revIds, array_keys( $this->oldIdMap, $id ) ); 00364 } 00365 print "$msg in text rows " . implode( ', ', $ids ) . 00366 ", revisions " . implode( ', ', $revIds ) . "\n"; 00367 } else { 00368 $id = $ids; 00369 $revIds = array_keys( $this->oldIdMap, $id ); 00370 if ( count( $revIds ) == 1 ) { 00371 print "$msg in old_id $id, rev_id {$revIds[0]}\n"; 00372 } else { 00373 print "$msg in old_id $id, revisions " . implode( ', ', $revIds ) . "\n"; 00374 } 00375 } 00376 $this->errors[$type] = $this->errors[$type] + array_flip( $revIds ); 00377 } 00378 00379 function checkExternalConcatBlobs( $externalConcatBlobs ) { 00380 if ( !count( $externalConcatBlobs ) ) { 00381 return; 00382 } 00383 00384 if ( is_null( $this->dbStore ) ) { 00385 $this->dbStore = new ExternalStoreDB; 00386 } 00387 00388 foreach ( $externalConcatBlobs as $cluster => $oldIds ) { 00389 $blobIds = array_keys( $oldIds ); 00390 $extDb =& $this->dbStore->getSlave( $cluster ); 00391 $blobsTable = $this->dbStore->getTable( $extDb ); 00392 $headerLength = strlen( self::CONCAT_HEADER ); 00393 $res = $extDb->select( $blobsTable, 00394 array( 'blob_id', "LEFT(blob_text, $headerLength) AS header" ), 00395 array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), __METHOD__ ); 00396 foreach ( $res as $row ) { 00397 if ( strcasecmp( $row->header, self::CONCAT_HEADER ) ) { 00398 $this->error( 00399 'restore text', 00400 "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL", 00401 $oldIds[$row->blob_id] 00402 ); 00403 } 00404 unset( $oldIds[$row->blob_id] ); 00405 } 00406 $extDb->freeResult( $res ); 00407 00408 // Print errors for missing blobs rows 00409 foreach ( $oldIds as $blobId => $oldIds2 ) { 00410 $this->error( 00411 'restore text', 00412 "Error: missing target $cluster/$blobId for two-part ES URL", 00413 $oldIds2 00414 ); 00415 } 00416 } 00417 } 00418 00419 function restoreText( $revIds, $xml ) { 00420 global $wgDBname; 00421 $tmpDir = wfTempDir(); 00422 00423 if ( !count( $revIds ) ) { 00424 return; 00425 } 00426 00427 print "Restoring text from XML backup...\n"; 00428 00429 $revFileName = "$tmpDir/broken-revlist-$wgDBname"; 00430 $filteredXmlFileName = "$tmpDir/filtered-$wgDBname.xml"; 00431 00432 // Write revision list 00433 if ( !file_put_contents( $revFileName, implode( "\n", $revIds ) ) ) { 00434 echo "Error writing revision list, can't restore text\n"; 00435 00436 return; 00437 } 00438 00439 // Run mwdumper 00440 echo "Filtering XML dump...\n"; 00441 $exitStatus = 0; 00442 passthru( 'mwdumper ' . 00443 wfEscapeShellArg( 00444 "--output=file:$filteredXmlFileName", 00445 "--filter=revlist:$revFileName", 00446 $xml 00447 ), $exitStatus 00448 ); 00449 00450 if ( $exitStatus ) { 00451 echo "mwdumper died with exit status $exitStatus\n"; 00452 00453 return; 00454 } 00455 00456 $file = fopen( $filteredXmlFileName, 'r' ); 00457 if ( !$file ) { 00458 echo "Unable to open filtered XML file\n"; 00459 00460 return; 00461 } 00462 00463 $dbr = wfGetDB( DB_SLAVE ); 00464 $dbw = wfGetDB( DB_MASTER ); 00465 $dbr->ping(); 00466 $dbw->ping(); 00467 00468 $source = new ImportStreamSource( $file ); 00469 $importer = new WikiImporter( $source ); 00470 $importer->setRevisionCallback( array( &$this, 'importRevision' ) ); 00471 $importer->doImport(); 00472 } 00473 00474 function importRevision( &$revision, &$importer ) { 00475 $id = $revision->getID(); 00476 $content = $revision->getContent( Revision::RAW ); 00477 $id = $id ? $id : ''; 00478 00479 if ( $content === null ) { 00480 echo "Revision $id is broken, we have no content available\n"; 00481 00482 return; 00483 } 00484 00485 $text = $content->serialize(); 00486 if ( $text === '' ) { 00487 // This is what happens if the revision was broken at the time the 00488 // dump was made. Unfortunately, it also happens if the revision was 00489 // legitimately blank, so there's no way to tell the difference. To 00490 // be safe, we'll skip it and leave it broken 00491 00492 echo "Revision $id is blank in the dump, may have been broken before export\n"; 00493 00494 return; 00495 } 00496 00497 if ( !$id ) { 00498 // No ID, can't import 00499 echo "No id tag in revision, can't import\n"; 00500 00501 return; 00502 } 00503 00504 // Find text row again 00505 $dbr = wfGetDB( DB_SLAVE ); 00506 $oldId = $dbr->selectField( 'revision', 'rev_text_id', array( 'rev_id' => $id ), __METHOD__ ); 00507 if ( !$oldId ) { 00508 echo "Missing revision row for rev_id $id\n"; 00509 00510 return; 00511 } 00512 00513 // Compress the text 00514 $flags = Revision::compressRevisionText( $text ); 00515 00516 // Update the text row 00517 $dbw = wfGetDB( DB_MASTER ); 00518 $dbw->update( 'text', 00519 array( 'old_flags' => $flags, 'old_text' => $text ), 00520 array( 'old_id' => $oldId ), 00521 __METHOD__, array( 'LIMIT' => 1 ) 00522 ); 00523 00524 // Remove it from the unfixed list and add it to the fixed list 00525 unset( $this->errors['restore text'][$id] ); 00526 $this->errors['fixed'][$id] = true; 00527 } 00528 }