26 if ( !defined(
'MEDIAWIKI' ) ) {
28 require_once __DIR__ .
'/../commandLine.inc';
32 if ( isset(
$args[0] ) ) {
37 $cs->check( $fix, $xml );
54 'restore text' =>
'Damaged text, need to be restored from a backup',
55 'restore revision' =>
'Damaged revision row, need to be restored from a backup',
56 'unfixable' =>
'Unexpected errors with no automated fixing method',
57 'fixed' =>
'Errors already fixed',
58 'fixable' =>
'Errors which would already be fixed if --fix was specified',
61 function check( $fix =
false, $xml =
'' ) {
64 print
"Checking, will fix errors if possible...\n";
66 print
"Checking...\n";
68 $maxRevId =
$dbr->selectField(
'revision',
'MAX(rev_id)',
'', __METHOD__ );
72 $knownFlags = [
'external',
'gzip',
'object',
'utf-8' ];
75 'restore revision' => [],
81 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
82 $chunkEnd = $chunkStart + $chunkSize - 1;
88 $res =
$dbr->select(
'revision', [
'rev_id',
'rev_text_id' ],
89 [
"rev_id BETWEEN $chunkStart AND $chunkEnd" ], __METHOD__ );
91 $this->oldIdMap[$row->rev_id] = $row->rev_text_id;
95 if ( !
count( $this->oldIdMap ) ) {
100 $missingTextRows = array_flip( $this->oldIdMap );
105 [
'old_id',
'old_flags' ],
106 [
'old_id' => $this->oldIdMap ],
109 foreach (
$res as $row ) {
113 $flags = $row->old_flags;
117 $flagStats = $flagStats + [ $flags => 0 ];
119 $flagStats[$flags]++;
122 unset( $missingTextRows[$row->old_id] );
125 if ( $flags ==
'' ) {
128 $flagArray = explode(
',', $flags );
130 if ( in_array(
'external', $flagArray ) ) {
131 $externalRevs[] = $id;
132 } elseif ( in_array(
'object', $flagArray ) ) {
137 if ( $flags ==
'0' ) {
141 $this->
addError(
'fixed',
"Warning: old_flags set to 0", $id );
144 $dbw->update(
'text', [
'old_flags' =>
'' ],
145 [
'old_id' => $id ], __METHOD__ );
148 $this->
addError(
'fixable',
"Warning: old_flags set to 0", $id );
150 } elseif (
count( array_diff( $flagArray, $knownFlags ) ) ) {
151 $this->
addError(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
157 foreach ( $missingTextRows
as $oldId => $revId ) {
158 $this->
addError(
'restore revision',
"Error: missing text row", $oldId );
162 $externalConcatBlobs = [];
163 $externalNormalBlobs = [];
164 if (
count( $externalRevs ) ) {
167 [
'old_id',
'old_flags',
'old_text' ],
168 [
'old_id' => $externalRevs ],
171 foreach (
$res as $row ) {
172 $urlParts = explode(
'://', $row->old_text, 2 );
173 if (
count( $urlParts ) !== 2 || $urlParts[1] ==
'' ) {
174 $this->
addError(
'restore text',
"Error: invalid URL \"{$row->old_text}\"", $row->old_id );
177 list( $proto, ) = $urlParts;
178 if ( $proto !=
'DB' ) {
181 "Error: invalid external protocol \"$proto\"",
185 $path = explode(
'/', $row->old_text );
188 if ( isset(
$path[4] ) ) {
189 $externalConcatBlobs[$cluster][$id][] = $row->old_id;
191 $externalNormalBlobs[$cluster][$id][] = $row->old_id;
201 if (
count( $externalNormalBlobs ) ) {
202 if ( is_null( $this->dbStore ) ) {
205 foreach ( $externalConcatBlobs
as $cluster => $xBlobIds ) {
206 $blobIds = array_keys( $xBlobIds );
207 $extDb =& $this->dbStore->getSlave( $cluster );
208 $blobsTable = $this->dbStore->getTable( $extDb );
209 $res = $extDb->select( $blobsTable,
211 [
'blob_id' => $blobIds ],
214 foreach (
$res as $row ) {
215 unset( $xBlobIds[$row->blob_id] );
217 $extDb->freeResult(
$res );
219 foreach ( $xBlobIds
as $blobId => $oldId ) {
222 "Error: missing target $blobId for one-part ES URL",
232 if (
count( $objectRevs ) ) {
236 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
237 [
'old_id' => $objectRevs ],
240 foreach (
$res as $row ) {
241 $oldId = $row->old_id;
243 if ( !preg_match(
'/^O:(\d+):"(\w+)"/', $row->header,
$matches ) ) {
244 $this->
addError(
'restore text',
"Error: invalid object header", $oldId );
248 $className = strtolower(
$matches[2] );
249 if ( strlen( $className ) !=
$matches[1] ) {
252 "Error: invalid object header, wrong class name length",
258 $objectStats = $objectStats + [ $className => 0 ];
259 $objectStats[$className]++;
261 switch ( $className ) {
262 case 'concatenatedgziphistoryblob':
265 case 'historyblobstub':
266 case 'historyblobcurstub':
267 if ( strlen( $row->header ) == $headerLength ) {
268 $this->
addError(
'unfixable',
"Error: overlong stub header", $oldId );
272 if ( !is_object( $stubObj ) ) {
273 $this->
addError(
'restore text',
"Error: unable to unserialize stub object", $oldId );
276 if ( $className ==
'historyblobstub' ) {
277 $concatBlobs[$stubObj->mOldId][] = $oldId;
279 $curIds[$stubObj->mCurId][] = $oldId;
283 $this->
addError(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
290 $externalConcatBlobs = [];
291 if (
count( $concatBlobs ) ) {
295 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
296 [
'old_id' => array_keys( $concatBlobs ) ],
299 foreach (
$res as $row ) {
300 $flags = explode(
',', $row->old_flags );
301 if ( in_array(
'external', $flags ) ) {
303 if ( in_array(
'object', $flags ) ) {
304 $urlParts = explode(
'/', $row->header );
305 if ( $urlParts[0] !=
'DB:' ) {
308 "Error: unrecognised external storage type \"{$urlParts[0]}",
312 $cluster = $urlParts[2];
314 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
315 $externalConcatBlobs[$cluster][$id] = [];
317 $externalConcatBlobs[$cluster][$id] = array_merge(
318 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
324 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
325 $concatBlobs[$row->old_id] );
327 } elseif ( strcasecmp(
328 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
333 "Error: Incorrect object header for concat bulk row {$row->old_id}",
334 $concatBlobs[$row->old_id]
338 unset( $concatBlobs[$row->old_id] );
348 print
"\n\nErrors:\n";
351 $description = $this->errorDescriptions[
$name];
352 echo
"$description: " . implode(
',', array_keys(
$errors ) ) .
"\n";
356 if (
count( $this->
errors[
'restore text'] ) && $fix ) {
357 if ( (
string)$xml !==
'' ) {
360 echo
"Can't fix text, no XML backup specified\n";
364 print
"\nFlag statistics:\n";
365 $total = array_sum( $flagStats );
366 foreach ( $flagStats
as $flag => $count ) {
367 printf(
"%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
369 print
"\nLocal object statistics:\n";
370 $total = array_sum( $objectStats );
371 foreach ( $objectStats
as $className => $count ) {
372 printf(
"%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );
377 if ( is_array( $ids ) &&
count( $ids ) == 1 ) {
378 $ids = reset( $ids );
380 if ( is_array( $ids ) ) {
382 foreach ( $ids
as $id ) {
383 $revIds = array_merge( $revIds, array_keys( $this->oldIdMap, $id ) );
385 print
"$msg in text rows " . implode(
', ', $ids ) .
386 ", revisions " . implode(
', ', $revIds ) .
"\n";
389 $revIds = array_keys( $this->oldIdMap, $id );
390 if (
count( $revIds ) == 1 ) {
391 print
"$msg in old_id $id, rev_id {$revIds[0]}\n";
393 print
"$msg in old_id $id, revisions " . implode(
', ', $revIds ) .
"\n";
400 if ( !
count( $externalConcatBlobs ) ) {
404 if ( is_null( $this->dbStore ) ) {
408 foreach ( $externalConcatBlobs
as $cluster => $oldIds ) {
409 $blobIds = array_keys( $oldIds );
410 $extDb =& $this->dbStore->getSlave( $cluster );
411 $blobsTable = $this->dbStore->getTable( $extDb );
412 $headerLength = strlen( self::CONCAT_HEADER );
413 $res = $extDb->select( $blobsTable,
414 [
'blob_id',
"LEFT(blob_text, $headerLength) AS header" ],
415 [
'blob_id' => $blobIds ],
418 foreach (
$res as $row ) {
419 if ( strcasecmp( $row->header, self::CONCAT_HEADER ) ) {
422 "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL",
423 $oldIds[$row->blob_id]
426 unset( $oldIds[$row->blob_id] );
428 $extDb->freeResult(
$res );
431 foreach ( $oldIds
as $blobId => $oldIds2 ) {
434 "Error: missing target $cluster/$blobId for two-part ES URL",
445 if ( !
count( $revIds ) ) {
449 print
"Restoring text from XML backup...\n";
451 $revFileName =
"$tmpDir/broken-revlist-$wgDBname";
452 $filteredXmlFileName =
"$tmpDir/filtered-$wgDBname.xml";
455 if ( !file_put_contents( $revFileName, implode(
"\n", $revIds ) ) ) {
456 echo
"Error writing revision list, can't restore text\n";
462 echo
"Filtering XML dump...\n";
464 passthru(
'mwdumper ' .
466 "--output=file:$filteredXmlFileName",
467 "--filter=revlist:$revFileName",
473 echo
"mwdumper died with exit status $exitStatus\n";
478 $file = fopen( $filteredXmlFileName,
'r' );
480 echo
"Unable to open filtered XML file\n";
493 MediaWikiServices::getInstance()->getMainConfig()
495 $importer->setRevisionCallback( [ $this,
'importRevision' ] );
496 $importer->setNoticeCallback(
function ( $msg,
$params ) {
499 $importer->doImport();
503 $id = $revision->getID();
508 echo
"Revision $id is broken, we have no content available\n";
514 if ( $text ===
'' ) {
520 echo
"Revision $id is blank in the dump, may have been broken before export\n";
527 echo
"No id tag in revision, can't import\n";
534 $oldId =
$dbr->selectField(
'revision',
'rev_text_id', [
'rev_id' => $id ], __METHOD__ );
536 echo
"Missing revision row for rev_id $id\n";
546 $dbw->update(
'text',
547 [
'old_flags' => $flags,
'old_text' => $text ],
548 [
'old_id' => $oldId ],
549 __METHOD__, [
'LIMIT' => 1 ]
553 unset( $this->
errors[
'restore text'][$id] );
554 $this->
errors[
'fixed'][$id] =
true;