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