[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Fsck for MediaWiki 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License along 16 * with this program; if not, write to the Free Software Foundation, Inc., 17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 * http://www.gnu.org/copyleft/gpl.html 19 * 20 * @file 21 * @ingroup Maintenance ExternalStorage 22 */ 23 24 if ( !defined( 'MEDIAWIKI' ) ) { 25 require_once __DIR__ . '/../commandLine.inc'; 26 27 $cs = new CheckStorage; 28 $fix = isset( $options['fix'] ); 29 if ( isset( $args[0] ) ) { 30 $xml = $args[0]; 31 } else { 32 $xml = false; 33 } 34 $cs->check( $fix, $xml ); 35 } 36 37 // ---------------------------------------------------------------------------------- 38 39 /** 40 * Maintenance script to do various checks on external storage. 41 * 42 * @ingroup Maintenance ExternalStorage 43 */ 44 class CheckStorage { 45 const CONCAT_HEADER = 'O:27:"concatenatedgziphistoryblob"'; 46 public $oldIdMap, $errors; 47 public $dbStore = null; 48 49 public $errorDescriptions = array( 50 'restore text' => 'Damaged text, need to be restored from a backup', 51 'restore revision' => 'Damaged revision row, need to be restored from a backup', 52 'unfixable' => 'Unexpected errors with no automated fixing method', 53 'fixed' => 'Errors already fixed', 54 'fixable' => 'Errors which would already be fixed if --fix was specified', 55 ); 56 57 function check( $fix = false, $xml = '' ) { 58 $dbr = wfGetDB( DB_SLAVE ); 59 if ( $fix ) { 60 print "Checking, will fix errors if possible...\n"; 61 } else { 62 print "Checking...\n"; 63 } 64 $maxRevId = $dbr->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__ ); 65 $chunkSize = 1000; 66 $flagStats = array(); 67 $objectStats = array(); 68 $knownFlags = array( 'external', 'gzip', 'object', 'utf-8' ); 69 $this->errors = array( 70 'restore text' => array(), 71 'restore revision' => array(), 72 'unfixable' => array(), 73 'fixed' => array(), 74 'fixable' => array(), 75 ); 76 77 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) { 78 $chunkEnd = $chunkStart + $chunkSize - 1; 79 // print "$chunkStart of $maxRevId\n"; 80 81 // Fetch revision rows 82 $this->oldIdMap = array(); 83 $dbr->ping(); 84 $res = $dbr->select( 'revision', array( 'rev_id', 'rev_text_id' ), 85 array( "rev_id BETWEEN $chunkStart AND $chunkEnd" ), __METHOD__ ); 86 foreach ( $res as $row ) { 87 $this->oldIdMap[$row->rev_id] = $row->rev_text_id; 88 } 89 $dbr->freeResult( $res ); 90 91 if ( !count( $this->oldIdMap ) ) { 92 continue; 93 } 94 95 // Fetch old_flags 96 $missingTextRows = array_flip( $this->oldIdMap ); 97 $externalRevs = array(); 98 $objectRevs = array(); 99 $res = $dbr->select( 'text', array( 'old_id', 'old_flags' ), 100 'old_id IN (' . implode( ',', $this->oldIdMap ) . ')', __METHOD__ ); 101 foreach ( $res as $row ) { 102 /** 103 * @var $flags int 104 */ 105 $flags = $row->old_flags; 106 $id = $row->old_id; 107 108 // Create flagStats row if it doesn't exist 109 $flagStats = $flagStats + array( $flags => 0 ); 110 // Increment counter 111 $flagStats[$flags]++; 112 113 // Not missing 114 unset( $missingTextRows[$row->old_id] ); 115 116 // Check for external or object 117 if ( $flags == '' ) { 118 $flagArray = array(); 119 } else { 120 $flagArray = explode( ',', $flags ); 121 } 122 if ( in_array( 'external', $flagArray ) ) { 123 $externalRevs[] = $id; 124 } elseif ( in_array( 'object', $flagArray ) ) { 125 $objectRevs[] = $id; 126 } 127 128 // Check for unrecognised flags 129 if ( $flags == '0' ) { 130 // This is a known bug from 2004 131 // It's safe to just erase the old_flags field 132 if ( $fix ) { 133 $this->error( 'fixed', "Warning: old_flags set to 0", $id ); 134 $dbw = wfGetDB( DB_MASTER ); 135 $dbw->ping(); 136 $dbw->update( 'text', array( 'old_flags' => '' ), 137 array( 'old_id' => $id ), __METHOD__ ); 138 echo "Fixed\n"; 139 } else { 140 $this->error( 'fixable', "Warning: old_flags set to 0", $id ); 141 } 142 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) { 143 $this->error( 'unfixable', "Error: invalid flags field \"$flags\"", $id ); 144 } 145 } 146 $dbr->freeResult( $res ); 147 148 // Output errors for any missing text rows 149 foreach ( $missingTextRows as $oldId => $revId ) { 150 $this->error( 'restore revision', "Error: missing text row", $oldId ); 151 } 152 153 // Verify external revisions 154 $externalConcatBlobs = array(); 155 $externalNormalBlobs = array(); 156 if ( count( $externalRevs ) ) { 157 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ), 158 array( 'old_id IN (' . implode( ',', $externalRevs ) . ')' ), __METHOD__ ); 159 foreach ( $res as $row ) { 160 $urlParts = explode( '://', $row->old_text, 2 ); 161 if ( count( $urlParts ) !== 2 || $urlParts[1] == '' ) { 162 $this->error( 'restore text', "Error: invalid URL \"{$row->old_text}\"", $row->old_id ); 163 continue; 164 } 165 list( $proto, ) = $urlParts; 166 if ( $proto != 'DB' ) { 167 $this->error( 'restore text', "Error: invalid external protocol \"$proto\"", $row->old_id ); 168 continue; 169 } 170 $path = explode( '/', $row->old_text ); 171 $cluster = $path[2]; 172 $id = $path[3]; 173 if ( isset( $path[4] ) ) { 174 $externalConcatBlobs[$cluster][$id][] = $row->old_id; 175 } else { 176 $externalNormalBlobs[$cluster][$id][] = $row->old_id; 177 } 178 } 179 $dbr->freeResult( $res ); 180 } 181 182 // Check external concat blobs for the right header 183 $this->checkExternalConcatBlobs( $externalConcatBlobs ); 184 185 // Check external normal blobs for existence 186 if ( count( $externalNormalBlobs ) ) { 187 if ( is_null( $this->dbStore ) ) { 188 $this->dbStore = new ExternalStoreDB; 189 } 190 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) { 191 $blobIds = array_keys( $xBlobIds ); 192 $extDb =& $this->dbStore->getSlave( $cluster ); 193 $blobsTable = $this->dbStore->getTable( $extDb ); 194 $res = $extDb->select( $blobsTable, 195 array( 'blob_id' ), 196 array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), __METHOD__ ); 197 foreach ( $res as $row ) { 198 unset( $xBlobIds[$row->blob_id] ); 199 } 200 $extDb->freeResult( $res ); 201 // Print errors for missing blobs rows 202 foreach ( $xBlobIds as $blobId => $oldId ) { 203 $this->error( 'restore text', "Error: missing target $blobId for one-part ES URL", $oldId ); 204 } 205 } 206 } 207 208 // Check local objects 209 $dbr->ping(); 210 $concatBlobs = array(); 211 $curIds = array(); 212 if ( count( $objectRevs ) ) { 213 $headerLength = 300; 214 $res = $dbr->select( 215 'text', 216 array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ), 217 array( 'old_id IN (' . implode( ',', $objectRevs ) . ')' ), 218 __METHOD__ 219 ); 220 foreach ( $res as $row ) { 221 $oldId = $row->old_id; 222 $matches = array(); 223 if ( !preg_match( '/^O:(\d+):"(\w+)"/', $row->header, $matches ) ) { 224 $this->error( 'restore text', "Error: invalid object header", $oldId ); 225 continue; 226 } 227 228 $className = strtolower( $matches[2] ); 229 if ( strlen( $className ) != $matches[1] ) { 230 $this->error( 231 'restore text', 232 "Error: invalid object header, wrong class name length", 233 $oldId 234 ); 235 continue; 236 } 237 238 $objectStats = $objectStats + array( $className => 0 ); 239 $objectStats[$className]++; 240 241 switch ( $className ) { 242 case 'concatenatedgziphistoryblob': 243 // Good 244 break; 245 case 'historyblobstub': 246 case 'historyblobcurstub': 247 if ( strlen( $row->header ) == $headerLength ) { 248 $this->error( 'unfixable', "Error: overlong stub header", $oldId ); 249 continue; 250 } 251 $stubObj = unserialize( $row->header ); 252 if ( !is_object( $stubObj ) ) { 253 $this->error( 'restore text', "Error: unable to unserialize stub object", $oldId ); 254 continue; 255 } 256 if ( $className == 'historyblobstub' ) { 257 $concatBlobs[$stubObj->mOldId][] = $oldId; 258 } else { 259 $curIds[$stubObj->mCurId][] = $oldId; 260 } 261 break; 262 default: 263 $this->error( 'unfixable', "Error: unrecognised object class \"$className\"", $oldId ); 264 } 265 } 266 $dbr->freeResult( $res ); 267 } 268 269 // Check local concat blob validity 270 $externalConcatBlobs = array(); 271 if ( count( $concatBlobs ) ) { 272 $headerLength = 300; 273 $res = $dbr->select( 274 'text', 275 array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ), 276 array( 'old_id IN (' . implode( ',', array_keys( $concatBlobs ) ) . ')' ), 277 __METHOD__ 278 ); 279 foreach ( $res as $row ) { 280 $flags = explode( ',', $row->old_flags ); 281 if ( in_array( 'external', $flags ) ) { 282 // Concat blob is in external storage? 283 if ( in_array( 'object', $flags ) ) { 284 $urlParts = explode( '/', $row->header ); 285 if ( $urlParts[0] != 'DB:' ) { 286 $this->error( 287 'unfixable', 288 "Error: unrecognised external storage type \"{$urlParts[0]}", 289 $row->old_id 290 ); 291 } else { 292 $cluster = $urlParts[2]; 293 $id = $urlParts[3]; 294 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) { 295 $externalConcatBlobs[$cluster][$id] = array(); 296 } 297 $externalConcatBlobs[$cluster][$id] = array_merge( 298 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id] 299 ); 300 } 301 } else { 302 $this->error( 303 'unfixable', 304 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}", 305 $concatBlobs[$row->old_id] ); 306 } 307 } elseif ( strcasecmp( 308 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ), 309 self::CONCAT_HEADER 310 ) ) { 311 $this->error( 312 'restore text', 313 "Error: Incorrect object header for concat bulk row {$row->old_id}", 314 $concatBlobs[$row->old_id] 315 ); 316 } # else good 317 318 unset( $concatBlobs[$row->old_id] ); 319 } 320 $dbr->freeResult( $res ); 321 } 322 323 // Check targets of unresolved stubs 324 $this->checkExternalConcatBlobs( $externalConcatBlobs ); 325 // next chunk 326 } 327 328 print "\n\nErrors:\n"; 329 foreach ( $this->errors as $name => $errors ) { 330 if ( count( $errors ) ) { 331 $description = $this->errorDescriptions[$name]; 332 echo "$description: " . implode( ',', array_keys( $errors ) ) . "\n"; 333 } 334 } 335 336 if ( count( $this->errors['restore text'] ) && $fix ) { 337 if ( (string)$xml !== '' ) { 338 $this->restoreText( array_keys( $this->errors['restore text'] ), $xml ); 339 } else { 340 echo "Can't fix text, no XML backup specified\n"; 341 } 342 } 343 344 print "\nFlag statistics:\n"; 345 $total = array_sum( $flagStats ); 346 foreach ( $flagStats as $flag => $count ) { 347 printf( "%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 ); 348 } 349 print "\nLocal object statistics:\n"; 350 $total = array_sum( $objectStats ); 351 foreach ( $objectStats as $className => $count ) { 352 printf( "%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 ); 353 } 354 } 355 356 function error( $type, $msg, $ids ) { 357 if ( is_array( $ids ) && count( $ids ) == 1 ) { 358 $ids = reset( $ids ); 359 } 360 if ( is_array( $ids ) ) { 361 $revIds = array(); 362 foreach ( $ids as $id ) { 363 $revIds = array_merge( $revIds, array_keys( $this->oldIdMap, $id ) ); 364 } 365 print "$msg in text rows " . implode( ', ', $ids ) . 366 ", revisions " . implode( ', ', $revIds ) . "\n"; 367 } else { 368 $id = $ids; 369 $revIds = array_keys( $this->oldIdMap, $id ); 370 if ( count( $revIds ) == 1 ) { 371 print "$msg in old_id $id, rev_id {$revIds[0]}\n"; 372 } else { 373 print "$msg in old_id $id, revisions " . implode( ', ', $revIds ) . "\n"; 374 } 375 } 376 $this->errors[$type] = $this->errors[$type] + array_flip( $revIds ); 377 } 378 379 function checkExternalConcatBlobs( $externalConcatBlobs ) { 380 if ( !count( $externalConcatBlobs ) ) { 381 return; 382 } 383 384 if ( is_null( $this->dbStore ) ) { 385 $this->dbStore = new ExternalStoreDB; 386 } 387 388 foreach ( $externalConcatBlobs as $cluster => $oldIds ) { 389 $blobIds = array_keys( $oldIds ); 390 $extDb =& $this->dbStore->getSlave( $cluster ); 391 $blobsTable = $this->dbStore->getTable( $extDb ); 392 $headerLength = strlen( self::CONCAT_HEADER ); 393 $res = $extDb->select( $blobsTable, 394 array( 'blob_id', "LEFT(blob_text, $headerLength) AS header" ), 395 array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), __METHOD__ ); 396 foreach ( $res as $row ) { 397 if ( strcasecmp( $row->header, self::CONCAT_HEADER ) ) { 398 $this->error( 399 'restore text', 400 "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL", 401 $oldIds[$row->blob_id] 402 ); 403 } 404 unset( $oldIds[$row->blob_id] ); 405 } 406 $extDb->freeResult( $res ); 407 408 // Print errors for missing blobs rows 409 foreach ( $oldIds as $blobId => $oldIds2 ) { 410 $this->error( 411 'restore text', 412 "Error: missing target $cluster/$blobId for two-part ES URL", 413 $oldIds2 414 ); 415 } 416 } 417 } 418 419 function restoreText( $revIds, $xml ) { 420 global $wgDBname; 421 $tmpDir = wfTempDir(); 422 423 if ( !count( $revIds ) ) { 424 return; 425 } 426 427 print "Restoring text from XML backup...\n"; 428 429 $revFileName = "$tmpDir/broken-revlist-$wgDBname"; 430 $filteredXmlFileName = "$tmpDir/filtered-$wgDBname.xml"; 431 432 // Write revision list 433 if ( !file_put_contents( $revFileName, implode( "\n", $revIds ) ) ) { 434 echo "Error writing revision list, can't restore text\n"; 435 436 return; 437 } 438 439 // Run mwdumper 440 echo "Filtering XML dump...\n"; 441 $exitStatus = 0; 442 passthru( 'mwdumper ' . 443 wfEscapeShellArg( 444 "--output=file:$filteredXmlFileName", 445 "--filter=revlist:$revFileName", 446 $xml 447 ), $exitStatus 448 ); 449 450 if ( $exitStatus ) { 451 echo "mwdumper died with exit status $exitStatus\n"; 452 453 return; 454 } 455 456 $file = fopen( $filteredXmlFileName, 'r' ); 457 if ( !$file ) { 458 echo "Unable to open filtered XML file\n"; 459 460 return; 461 } 462 463 $dbr = wfGetDB( DB_SLAVE ); 464 $dbw = wfGetDB( DB_MASTER ); 465 $dbr->ping(); 466 $dbw->ping(); 467 468 $source = new ImportStreamSource( $file ); 469 $importer = new WikiImporter( $source ); 470 $importer->setRevisionCallback( array( &$this, 'importRevision' ) ); 471 $importer->doImport(); 472 } 473 474 function importRevision( &$revision, &$importer ) { 475 $id = $revision->getID(); 476 $content = $revision->getContent( Revision::RAW ); 477 $id = $id ? $id : ''; 478 479 if ( $content === null ) { 480 echo "Revision $id is broken, we have no content available\n"; 481 482 return; 483 } 484 485 $text = $content->serialize(); 486 if ( $text === '' ) { 487 // This is what happens if the revision was broken at the time the 488 // dump was made. Unfortunately, it also happens if the revision was 489 // legitimately blank, so there's no way to tell the difference. To 490 // be safe, we'll skip it and leave it broken 491 492 echo "Revision $id is blank in the dump, may have been broken before export\n"; 493 494 return; 495 } 496 497 if ( !$id ) { 498 // No ID, can't import 499 echo "No id tag in revision, can't import\n"; 500 501 return; 502 } 503 504 // Find text row again 505 $dbr = wfGetDB( DB_SLAVE ); 506 $oldId = $dbr->selectField( 'revision', 'rev_text_id', array( 'rev_id' => $id ), __METHOD__ ); 507 if ( !$oldId ) { 508 echo "Missing revision row for rev_id $id\n"; 509 510 return; 511 } 512 513 // Compress the text 514 $flags = Revision::compressRevisionText( $text ); 515 516 // Update the text row 517 $dbw = wfGetDB( DB_MASTER ); 518 $dbw->update( 'text', 519 array( 'old_flags' => $flags, 'old_text' => $text ), 520 array( 'old_id' => $oldId ), 521 __METHOD__, array( 'LIMIT' => 1 ) 522 ); 523 524 // Remove it from the unfixed list and add it to the fixed list 525 unset( $this->errors['restore text'][$id] ); 526 $this->errors['fixed'][$id] = true; 527 } 528 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |