28 if ( !defined(
'MEDIAWIKI' ) ) {
30 require_once __DIR__ .
'/../CommandLineInc.php';
33 $fix = isset( $options[
'fix'] );
34 $xml =
$args[0] ??
false;
35 $cs->check( $fix, $xml );
53 'restore text' =>
'Damaged text, need to be restored from a backup',
54 'restore revision' =>
'Damaged revision row, need to be restored from a backup',
55 'unfixable' =>
'Unexpected errors with no automated fixing method',
56 'fixed' =>
'Errors already fixed',
57 'fixable' =>
'Errors which would already be fixed if --fix was specified',
60 public function check( $fix =
false, $xml =
'' ) {
63 print
"Checking, will fix errors if possible...\n";
65 print
"Checking...\n";
67 $maxRevId =
$dbr->selectField(
'revision',
'MAX(rev_id)',
'', __METHOD__ );
71 $knownFlags = [
'external',
'gzip',
'object',
'utf-8' ];
74 'restore revision' => [],
80 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
81 $chunkEnd = $chunkStart + $chunkSize - 1;
89 [
'slots',
'content' ],
90 [
'slot_revision_id',
'content_address' ],
91 [
"slot_revision_id BETWEEN $chunkStart AND $chunkEnd" ],
94 [
'content' => [
'INNER JOIN', [
'content_id = slot_content_id' ] ] ]
97 $blobStore = MediaWikiServices::getInstance()->getBlobStore();
98 '@phan-var \MediaWiki\Storage\SqlBlobStore $blobStore';
99 foreach (
$res as $row ) {
100 $textId = $blobStore->getTextIdFromAddress( $row->content_address );
102 if ( !isset( $this->oldIdMap[$textId] ) ) {
103 $this->oldIdMap[ $textId ] = [ $row->slot_revision_id ];
104 } elseif ( !in_array( $row->slot_revision_id, $this->oldIdMap[$textId] ) ) {
105 $this->oldIdMap[ $textId ][] = $row->slot_revision_id;
110 if ( !count( $this->oldIdMap ) ) {
120 [
'old_id',
'old_flags' ],
121 [
'old_id' => array_keys( $this->oldIdMap ) ],
124 foreach (
$res as $row ) {
128 $flags = $row->old_flags;
132 $flagStats += [ $flags => 0 ];
134 $flagStats[$flags]++;
137 unset( $missingTextRows[$row->old_id] );
140 if ( $flags ==
'' ) {
143 $flagArray = explode(
',', $flags );
145 if ( in_array(
'external', $flagArray ) ) {
146 $externalRevs[] = $id;
147 } elseif ( in_array(
'object', $flagArray ) ) {
152 if ( $flags ==
'0' ) {
156 $this->
addError(
'fixed',
"Warning: old_flags set to 0", $id );
159 $dbw->update(
'text', [
'old_flags' =>
'' ],
160 [
'old_id' => $id ], __METHOD__ );
163 $this->
addError(
'fixable',
"Warning: old_flags set to 0", $id );
165 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
166 $this->
addError(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
171 foreach ( $missingTextRows as $oldId => $revIds ) {
172 $this->
addError(
'restore revision',
"Error: missing text row", $oldId );
176 $externalConcatBlobs = [];
177 $externalNormalBlobs = [];
178 if ( count( $externalRevs ) ) {
181 [
'old_id',
'old_flags',
'old_text' ],
182 [
'old_id' => $externalRevs ],
185 foreach (
$res as $row ) {
186 $urlParts = explode(
'://', $row->old_text, 2 );
187 if ( count( $urlParts ) !== 2 || $urlParts[1] ==
'' ) {
188 $this->
addError(
'restore text',
"Error: invalid URL \"{$row->old_text}\"", $row->old_id );
191 list( $proto, ) = $urlParts;
192 if ( $proto !=
'DB' ) {
195 "Error: invalid external protocol \"$proto\"",
199 $path = explode(
'/', $row->old_text );
202 if ( isset(
$path[4] ) ) {
203 $externalConcatBlobs[$cluster][$id][] = $row->old_id;
205 $externalNormalBlobs[$cluster][$id][] = $row->old_id;
214 if ( count( $externalNormalBlobs ) ) {
215 if ( $this->dbStore ===
null ) {
216 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
217 $this->dbStore = $esFactory->getStore(
'DB' );
219 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
220 $blobIds = array_keys( $xBlobIds );
221 $extDb =& $this->dbStore->getReplica( $cluster );
222 $blobsTable = $this->dbStore->getTable( $extDb );
223 $res = $extDb->select( $blobsTable,
225 [
'blob_id' => $blobIds ],
228 foreach (
$res as $row ) {
229 unset( $xBlobIds[$row->blob_id] );
232 foreach ( $xBlobIds as $blobId => $oldId ) {
235 "Error: missing target $blobId for one-part ES URL",
245 if ( count( $objectRevs ) ) {
249 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
250 [
'old_id' => $objectRevs ],
253 foreach (
$res as $row ) {
254 $oldId = $row->old_id;
256 if ( !preg_match(
'/^O:(\d+):"(\w+)"/', $row->header,
$matches ) ) {
257 $this->
addError(
'restore text',
"Error: invalid object header", $oldId );
261 $className = strtolower(
$matches[2] );
262 if ( strlen( $className ) !=
$matches[1] ) {
265 "Error: invalid object header, wrong class name length",
271 $objectStats += [ $className => 0 ];
272 $objectStats[$className]++;
274 switch ( $className ) {
275 case 'concatenatedgziphistoryblob':
278 case 'historyblobstub':
279 case 'historyblobcurstub':
280 if ( strlen( $row->header ) == $headerLength ) {
281 $this->
addError(
'unfixable',
"Error: overlong stub header", $oldId );
285 if ( !is_object( $stubObj ) ) {
286 $this->
addError(
'restore text',
"Error: unable to unserialize stub object", $oldId );
289 if ( $className ==
'historyblobstub' ) {
290 $concatBlobs[$stubObj->mOldId][] = $oldId;
292 $curIds[$stubObj->mCurId][] = $oldId;
296 $this->
addError(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
302 $externalConcatBlobs = [];
303 if ( count( $concatBlobs ) ) {
307 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
308 [
'old_id' => array_keys( $concatBlobs ) ],
311 foreach (
$res as $row ) {
312 $flags = explode(
',', $row->old_flags );
313 if ( in_array(
'external', $flags ) ) {
315 if ( in_array(
'object', $flags ) ) {
316 $urlParts = explode(
'/', $row->header );
317 if ( $urlParts[0] !=
'DB:' ) {
320 "Error: unrecognised external storage type \"{$urlParts[0]}",
324 $cluster = $urlParts[2];
326 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
327 $externalConcatBlobs[$cluster][$id] = [];
329 $externalConcatBlobs[$cluster][$id] = array_merge(
330 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
336 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
337 $concatBlobs[$row->old_id] );
339 } elseif ( strcasecmp(
340 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
345 "Error: Incorrect object header for concat bulk row {$row->old_id}",
346 $concatBlobs[$row->old_id]
350 unset( $concatBlobs[$row->old_id] );
359 print
"\n\nErrors:\n";
360 foreach ( $this->errors as $name =>
$errors ) {
363 $description = $this->errorDescriptions[$name];
364 echo
"$description: " . implode(
',', array_keys(
$errors ) ) .
"\n";
369 if ( count( $this->errors[
'restore text'] ) && $fix ) {
370 if ( (
string)$xml !==
'' ) {
371 $this->
restoreText( array_keys( $this->errors[
'restore text'] ), $xml );
373 echo
"Can't fix text, no XML backup specified\n";
377 print
"\nFlag statistics:\n";
378 $total = array_sum( $flagStats );
379 foreach ( $flagStats as $flag => $count ) {
380 printf(
"%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
382 print
"\nLocal object statistics:\n";
383 $total = array_sum( $objectStats );
384 foreach ( $objectStats as $className => $count ) {
385 printf(
"%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );
390 if ( is_array( $ids ) && count( $ids ) == 1 ) {
391 $ids = reset( $ids );
393 if ( is_array( $ids ) ) {
395 foreach ( $ids as $id ) {
396 $revIds = array_unique( array_merge( $revIds, $this->oldIdMap[$id] ) );
398 print
"$msg in text rows " . implode(
', ', $ids ) .
399 ", revisions " . implode(
', ', $revIds ) .
"\n";
402 $revIds = $this->oldIdMap[$id];
403 if ( count( $revIds ) == 1 ) {
404 print
"$msg in old_id $id, rev_id {$revIds[0]}\n";
406 print
"$msg in old_id $id, revisions " . implode(
', ', $revIds ) .
"\n";
409 $this->errors[
$type] += array_flip( $revIds );
413 if ( !count( $externalConcatBlobs ) ) {
417 if ( $this->dbStore ===
null ) {
418 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
419 $this->dbStore = $esFactory->getStore(
'DB' );
422 foreach ( $externalConcatBlobs as $cluster => $oldIds ) {
423 $blobIds = array_keys( $oldIds );
424 $extDb =& $this->dbStore->getReplica( $cluster );
425 $blobsTable = $this->dbStore->getTable( $extDb );
426 $headerLength = strlen( self::CONCAT_HEADER );
427 $res = $extDb->select( $blobsTable,
428 [
'blob_id',
"LEFT(blob_text, $headerLength) AS header" ],
429 [
'blob_id' => $blobIds ],
432 foreach (
$res as $row ) {
433 if ( strcasecmp( $row->header, self::CONCAT_HEADER ) ) {
436 "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL",
437 $oldIds[$row->blob_id]
440 unset( $oldIds[$row->blob_id] );
444 foreach ( $oldIds as $blobId => $oldIds2 ) {
447 "Error: missing target $cluster/$blobId for two-part ES URL",
458 if ( !count( $revIds ) ) {
462 print
"Restoring text from XML backup...\n";
464 $revFileName =
"$tmpDir/broken-revlist-$wgDBname";
465 $filteredXmlFileName =
"$tmpDir/filtered-$wgDBname.xml";
468 if ( !file_put_contents( $revFileName, implode(
"\n", $revIds ) ) ) {
469 echo
"Error writing revision list, can't restore text\n";
475 echo
"Filtering XML dump...\n";
477 passthru(
'mwdumper ' .
479 "--output=file:$filteredXmlFileName",
480 "--filter=revlist:$revFileName",
486 echo
"mwdumper died with exit status $exitStatus\n";
491 $file = fopen( $filteredXmlFileName,
'r' );
493 echo
"Unable to open filtered XML file\n";
506 MediaWikiServices::getInstance()->getMainConfig()
508 $importer->setRevisionCallback( [ $this,
'importRevision' ] );
509 $importer->setNoticeCallback(
function ( $msg, $params ) {
510 echo
wfMessage( $msg, $params )->text() .
"\n";
512 $importer->doImport();
516 $id = $revision->getID();
517 $content = $revision->getContent( RevisionRecord::RAW );
521 echo
"Revision $id is broken, we have no content available\n";
527 if ( $text ===
'' ) {
533 echo
"Revision $id is blank in the dump, may have been broken before export\n";
540 echo
"No id tag in revision, can't import\n";
548 [
'slots',
'content' ],
549 [
'content_address' ],
550 [
'slot_revision_id' => $id ],
553 [
'content' => [
'INNER JOIN', [
'content_id = slot_content_id' ] ] ]
556 $blobStore = MediaWikiServices::getInstance()
557 ->getBlobStoreFactory()
559 $oldId = $blobStore->getTextIdFromAddress(
$res->content_address );
562 echo
"Missing revision row for rev_id $id\n";
567 $flags = $blobStore->compressData( $text );
571 $dbw->update(
'text',
572 [
'old_flags' => $flags,
'old_text' => $text ],
573 [
'old_id' => $oldId ],
574 __METHOD__, [
'LIMIT' => 1 ]
578 unset( $this->errors[
'restore text'][$id] );
579 $this->errors[
'fixed'][$id] =
true;