27 if ( !defined(
'MEDIAWIKI' ) ) {
29 require_once __DIR__ .
'/../CommandLineInc.php';
32 $fix = isset( $options[
'fix'] );
33 $xml = $args[0] ??
false;
34 $cs->check( $fix, $xml );
46 private const CONCAT_HEADER =
'O:27:"concatenatedgziphistoryblob"';
52 'restore text' =>
'Damaged text, need to be restored from a backup',
53 'restore revision' =>
'Damaged revision row, need to be restored from a backup',
54 'unfixable' =>
'Unexpected errors with no automated fixing method',
55 'fixed' =>
'Errors already fixed',
56 'fixable' =>
'Errors which would already be fixed if --fix was specified',
59 public function check( $fix =
false, $xml =
'' ) {
62 print
"Checking, will fix errors if possible...\n";
64 print
"Checking...\n";
66 $maxRevId =
$dbr->selectField(
'revision',
'MAX(rev_id)',
'', __METHOD__ );
70 $knownFlags = [
'external',
'gzip',
'object',
'utf-8' ];
73 'restore revision' => [],
79 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
80 $chunkEnd = $chunkStart + $chunkSize - 1;
88 [
'slots',
'content' ],
89 [
'slot_revision_id',
'content_address' ],
90 [
"slot_revision_id BETWEEN $chunkStart AND $chunkEnd" ],
93 [
'content' => [
'INNER JOIN', [
'content_id = slot_content_id' ] ] ]
96 $blobStore = MediaWikiServices::getInstance()->getBlobStore();
97 '@phan-var \MediaWiki\Storage\SqlBlobStore $blobStore';
98 foreach (
$res as $row ) {
99 $textId = $blobStore->getTextIdFromAddress( $row->content_address );
101 if ( !isset( $this->oldIdMap[$textId] ) ) {
102 $this->oldIdMap[ $textId ] = [ $row->slot_revision_id ];
103 } elseif ( !in_array( $row->slot_revision_id, $this->oldIdMap[$textId] ) ) {
104 $this->oldIdMap[ $textId ][] = $row->slot_revision_id;
109 if ( !count( $this->oldIdMap ) ) {
119 [
'old_id',
'old_flags' ],
120 [
'old_id' => array_keys( $this->oldIdMap ) ],
123 foreach (
$res as $row ) {
127 $flags = $row->old_flags;
131 $flagStats += [ $flags => 0 ];
133 $flagStats[$flags]++;
136 unset( $missingTextRows[$row->old_id] );
139 if ( $flags ==
'' ) {
142 $flagArray = explode(
',', $flags );
144 if ( in_array(
'external', $flagArray ) ) {
145 $externalRevs[] = $id;
146 } elseif ( in_array(
'object', $flagArray ) ) {
151 if ( $flags ==
'0' ) {
155 $this->addError(
'fixed',
"Warning: old_flags set to 0", $id );
158 $dbw->update(
'text', [
'old_flags' =>
'' ],
159 [
'old_id' => $id ], __METHOD__ );
162 $this->addError(
'fixable',
"Warning: old_flags set to 0", $id );
164 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
165 $this->addError(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
170 foreach ( $missingTextRows as $oldId => $revIds ) {
171 $this->addError(
'restore revision',
"Error: missing text row", $oldId );
175 $externalConcatBlobs = [];
176 $externalNormalBlobs = [];
177 if ( count( $externalRevs ) ) {
180 [
'old_id',
'old_flags',
'old_text' ],
181 [
'old_id' => $externalRevs ],
184 foreach (
$res as $row ) {
185 $urlParts = explode(
'://', $row->old_text, 2 );
186 if ( count( $urlParts ) !== 2 || $urlParts[1] ==
'' ) {
187 $this->addError(
'restore text',
"Error: invalid URL \"{$row->old_text}\"", $row->old_id );
190 [ $proto, ] = $urlParts;
191 if ( $proto !=
'DB' ) {
194 "Error: invalid external protocol \"$proto\"",
198 $path = explode(
'/', $row->old_text );
201 if ( isset(
$path[4] ) ) {
202 $externalConcatBlobs[$cluster][$id][] = $row->old_id;
204 $externalNormalBlobs[$cluster][$id][] = $row->old_id;
210 $this->checkExternalConcatBlobs( $externalConcatBlobs );
213 if ( count( $externalNormalBlobs ) ) {
214 if ( $this->dbStore ===
null ) {
215 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
216 $this->dbStore = $esFactory->getStore(
'DB' );
218 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
219 $blobIds = array_keys( $xBlobIds );
220 $extDb =& $this->dbStore->getReplica( $cluster );
221 $blobsTable = $this->dbStore->getTable( $extDb );
222 $res = $extDb->select( $blobsTable,
224 [
'blob_id' => $blobIds ],
227 foreach (
$res as $row ) {
228 unset( $xBlobIds[$row->blob_id] );
231 foreach ( $xBlobIds as $blobId => $oldId ) {
234 "Error: missing target $blobId for one-part ES URL",
244 if ( count( $objectRevs ) ) {
248 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
249 [
'old_id' => $objectRevs ],
252 foreach (
$res as $row ) {
253 $oldId = $row->old_id;
255 if ( !preg_match(
'/^O:(\d+):"(\w+)"/', $row->header,
$matches ) ) {
256 $this->addError(
'restore text',
"Error: invalid object header", $oldId );
260 $className = strtolower(
$matches[2] );
261 if ( strlen( $className ) !=
$matches[1] ) {
264 "Error: invalid object header, wrong class name length",
270 $objectStats += [ $className => 0 ];
271 $objectStats[$className]++;
273 switch ( $className ) {
274 case 'concatenatedgziphistoryblob':
277 case 'historyblobstub':
278 case 'historyblobcurstub':
279 if ( strlen( $row->header ) == $headerLength ) {
280 $this->addError(
'unfixable',
"Error: overlong stub header", $oldId );
283 $stubObj = unserialize( $row->header );
284 if ( !is_object( $stubObj ) ) {
285 $this->addError(
'restore text',
"Error: unable to unserialize stub object", $oldId );
288 if ( $className ==
'historyblobstub' ) {
289 $concatBlobs[$stubObj->getLocation()][] = $oldId;
291 $curIds[$stubObj->mCurId][] = $oldId;
295 $this->addError(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
301 $externalConcatBlobs = [];
302 if ( count( $concatBlobs ) ) {
306 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
307 [
'old_id' => array_keys( $concatBlobs ) ],
310 foreach (
$res as $row ) {
311 $flags = explode(
',', $row->old_flags );
312 if ( in_array(
'external', $flags ) ) {
314 if ( in_array(
'object', $flags ) ) {
315 $urlParts = explode(
'/', $row->header );
316 if ( $urlParts[0] !=
'DB:' ) {
319 "Error: unrecognised external storage type \"{$urlParts[0]}",
323 $cluster = $urlParts[2];
325 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
326 $externalConcatBlobs[$cluster][$id] = [];
328 $externalConcatBlobs[$cluster][$id] = array_merge(
329 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
335 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
336 $concatBlobs[$row->old_id] );
338 } elseif ( strcasecmp(
339 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
344 "Error: Incorrect object header for concat bulk row {$row->old_id}",
345 $concatBlobs[$row->old_id]
349 unset( $concatBlobs[$row->old_id] );
354 $this->checkExternalConcatBlobs( $externalConcatBlobs );
358 print
"\n\nErrors:\n";
359 foreach ( $this->errors as $name =>
$errors ) {
361 $description = $this->errorDescriptions[$name];
362 echo
"$description: " . implode(
',', array_keys(
$errors ) ) .
"\n";
366 if ( count( $this->errors[
'restore text'] ) && $fix ) {
367 if ( (
string)$xml !==
'' ) {
368 $this->restoreText( array_keys( $this->errors[
'restore text'] ), $xml );
370 echo
"Can't fix text, no XML backup specified\n";
374 print
"\nFlag statistics:\n";
375 $total = array_sum( $flagStats );
376 foreach ( $flagStats as $flag => $count ) {
377 printf(
"%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
379 print
"\nLocal object statistics:\n";
380 $total = array_sum( $objectStats );
381 foreach ( $objectStats as $className => $count ) {
382 printf(
"%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );
386 private function addError(
$type, $msg, $ids ) {
387 if ( is_array( $ids ) && count( $ids ) == 1 ) {
388 $ids = reset( $ids );
390 if ( is_array( $ids ) ) {
392 foreach ( $ids as $id ) {
393 $revIds = array_unique( array_merge( $revIds, $this->oldIdMap[$id] ) );
395 print
"$msg in text rows " . implode(
', ', $ids ) .
396 ", revisions " . implode(
', ', $revIds ) .
"\n";
399 $revIds = $this->oldIdMap[$id];
400 if ( count( $revIds ) == 1 ) {
401 print
"$msg in old_id $id, rev_id {$revIds[0]}\n";
403 print
"$msg in old_id $id, revisions " . implode(
', ', $revIds ) .
"\n";
406 $this->errors[
$type] += array_fill_keys( $revIds,
true );
409 private function checkExternalConcatBlobs( $externalConcatBlobs ) {
410 if ( !count( $externalConcatBlobs ) ) {
414 if ( $this->dbStore ===
null ) {
415 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
416 $this->dbStore = $esFactory->getStore(
'DB' );
419 foreach ( $externalConcatBlobs as $cluster => $oldIds ) {
420 $blobIds = array_keys( $oldIds );
421 $extDb =& $this->dbStore->getReplica( $cluster );
422 $blobsTable = $this->dbStore->getTable( $extDb );
423 $headerLength = strlen( self::CONCAT_HEADER );
424 $res = $extDb->select( $blobsTable,
425 [
'blob_id',
"LEFT(blob_text, $headerLength) AS header" ],
426 [
'blob_id' => $blobIds ],
429 foreach (
$res as $row ) {
430 if ( strcasecmp( $row->header, self::CONCAT_HEADER ) ) {
433 "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL",
434 $oldIds[$row->blob_id]
437 unset( $oldIds[$row->blob_id] );
441 foreach ( $oldIds as $blobId => $oldIds2 ) {
444 "Error: missing target $cluster/$blobId for two-part ES URL",
451 private function restoreText( $revIds, $xml ) {
455 if ( !count( $revIds ) ) {
459 print
"Restoring text from XML backup...\n";
461 $revFileName =
"$tmpDir/broken-revlist-$wgDBname";
462 $filteredXmlFileName =
"$tmpDir/filtered-$wgDBname.xml";
465 if ( !file_put_contents( $revFileName, implode(
"\n", $revIds ) ) ) {
466 echo
"Error writing revision list, can't restore text\n";
472 echo
"Filtering XML dump...\n";
474 passthru(
'mwdumper ' .
476 "--output=file:$filteredXmlFileName",
477 "--filter=revlist:$revFileName",
483 echo
"mwdumper died with exit status $exitStatus\n";
488 $file = fopen( $filteredXmlFileName,
'r' );
490 echo
"Unable to open filtered XML file\n";
501 $importer = MediaWikiServices::getInstance()
502 ->getWikiImporterFactory()
504 $importer->setRevisionCallback( [ $this,
'importRevision' ] );
505 $importer->setNoticeCallback(
static function ( $msg, $params ) {
506 echo
wfMessage( $msg, $params )->text() .
"\n";
508 $importer->doImport();
515 $id = $revision->getID();
520 echo
"Revision $id is broken, we have no content available\n";
526 if ( $text ===
'' ) {
532 echo
"Revision $id is blank in the dump, may have been broken before export\n";
539 echo
"No id tag in revision, can't import\n";
547 [
'slots',
'content' ],
548 [
'content_address' ],
549 [
'slot_revision_id' => $id ],
552 [
'content' => [
'INNER JOIN', [
'content_id = slot_content_id' ] ] ]
555 $blobStore = MediaWikiServices::getInstance()
556 ->getBlobStoreFactory()
558 $oldId = $blobStore->getTextIdFromAddress(
$res->content_address );
561 echo
"Missing revision row for rev_id $id\n";
566 $flags = $blobStore->compressData( $text );
570 $dbw->update(
'text',
571 [
'old_flags' => $flags,
'old_text' => $text ],
572 [
'old_id' => $oldId ],
573 __METHOD__, [
'LIMIT' => 1 ]
577 unset( $this->errors[
'restore text'][$id] );
578 $this->errors[
'fixed'][$id] =
true;
global $optionsWithoutArgs
wfTempDir()
Tries to get the system directory for temporary files.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Maintenance script to do various checks on external storage.
importRevision( $revision)
check( $fix=false, $xml='')
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
$wgDBname
Config variable stub for the DBname setting, for use by phpdoc and IDEs.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.