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)',
false, __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 );
103 $res =
$dbr->select(
'text', [
'old_id',
'old_flags' ],
104 'old_id IN (' . implode(
',', $this->oldIdMap ) .
')', __METHOD__ );
105 foreach (
$res as $row ) {
113 $flagStats = $flagStats + [
$flags => 0 ];
118 unset( $missingTextRows[$row->old_id] );
124 $flagArray = explode(
',',
$flags );
126 if ( in_array(
'external', $flagArray ) ) {
127 $externalRevs[] = $id;
128 } elseif ( in_array(
'object', $flagArray ) ) {
137 $this->
error(
'fixed',
"Warning: old_flags set to 0", $id );
140 $dbw->update(
'text', [
'old_flags' =>
'' ],
141 [
'old_id' => $id ], __METHOD__ );
144 $this->
error(
'fixable',
"Warning: old_flags set to 0", $id );
146 } elseif (
count( array_diff( $flagArray, $knownFlags ) ) ) {
147 $this->
error(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
153 foreach ( $missingTextRows
as $oldId => $revId ) {
154 $this->
error(
'restore revision',
"Error: missing text row", $oldId );
158 $externalConcatBlobs = [];
159 $externalNormalBlobs = [];
160 if (
count( $externalRevs ) ) {
161 $res =
$dbr->select(
'text', [
'old_id',
'old_flags',
'old_text' ],
162 [
'old_id IN (' . implode(
',', $externalRevs ) .
')' ], __METHOD__ );
163 foreach (
$res as $row ) {
164 $urlParts = explode(
'://', $row->old_text, 2 );
165 if (
count( $urlParts ) !== 2 || $urlParts[1] ==
'' ) {
166 $this->
error(
'restore text',
"Error: invalid URL \"{$row->old_text}\"", $row->old_id );
169 list( $proto, ) = $urlParts;
170 if ( $proto !=
'DB' ) {
171 $this->
error(
'restore text',
"Error: invalid external protocol \"$proto\"", $row->old_id );
174 $path = explode(
'/', $row->old_text );
177 if ( isset(
$path[4] ) ) {
178 $externalConcatBlobs[$cluster][$id][] = $row->old_id;
180 $externalNormalBlobs[$cluster][$id][] = $row->old_id;
190 if (
count( $externalNormalBlobs ) ) {
191 if ( is_null( $this->dbStore ) ) {
194 foreach ( $externalConcatBlobs
as $cluster => $xBlobIds ) {
195 $blobIds = array_keys( $xBlobIds );
196 $extDb =& $this->dbStore->getSlave( $cluster );
197 $blobsTable = $this->dbStore->getTable( $extDb );
198 $res = $extDb->select( $blobsTable,
200 [
'blob_id IN( ' . implode(
',', $blobIds ) .
')' ], __METHOD__ );
201 foreach (
$res as $row ) {
202 unset( $xBlobIds[$row->blob_id] );
204 $extDb->freeResult(
$res );
206 foreach ( $xBlobIds
as $blobId => $oldId ) {
207 $this->
error(
'restore text',
"Error: missing target $blobId for one-part ES URL", $oldId );
216 if (
count( $objectRevs ) ) {
220 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
221 [
'old_id IN (' . implode(
',', $objectRevs ) .
')' ],
224 foreach (
$res as $row ) {
225 $oldId = $row->old_id;
227 if ( !preg_match(
'/^O:(\d+):"(\w+)"/', $row->header,
$matches ) ) {
228 $this->
error(
'restore text',
"Error: invalid object header", $oldId );
232 $className = strtolower(
$matches[2] );
233 if ( strlen( $className ) !=
$matches[1] ) {
236 "Error: invalid object header, wrong class name length",
242 $objectStats = $objectStats + [ $className => 0 ];
243 $objectStats[$className]++;
245 switch ( $className ) {
246 case 'concatenatedgziphistoryblob':
249 case 'historyblobstub':
250 case 'historyblobcurstub':
251 if ( strlen( $row->header ) == $headerLength ) {
252 $this->
error(
'unfixable',
"Error: overlong stub header", $oldId );
256 if ( !is_object( $stubObj ) ) {
257 $this->
error(
'restore text',
"Error: unable to unserialize stub object", $oldId );
260 if ( $className ==
'historyblobstub' ) {
261 $concatBlobs[$stubObj->mOldId][] = $oldId;
263 $curIds[$stubObj->mCurId][] = $oldId;
267 $this->
error(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
274 $externalConcatBlobs = [];
275 if (
count( $concatBlobs ) ) {
279 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
280 [
'old_id IN (' . implode(
',', array_keys( $concatBlobs ) ) .
')' ],
283 foreach (
$res as $row ) {
284 $flags = explode(
',', $row->old_flags );
285 if ( in_array(
'external',
$flags ) ) {
287 if ( in_array(
'object',
$flags ) ) {
288 $urlParts = explode(
'/', $row->header );
289 if ( $urlParts[0] !=
'DB:' ) {
292 "Error: unrecognised external storage type \"{$urlParts[0]}",
296 $cluster = $urlParts[2];
298 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
299 $externalConcatBlobs[$cluster][$id] = [];
301 $externalConcatBlobs[$cluster][$id] = array_merge(
302 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
308 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
309 $concatBlobs[$row->old_id] );
311 } elseif ( strcasecmp(
312 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
317 "Error: Incorrect object header for concat bulk row {$row->old_id}",
318 $concatBlobs[$row->old_id]
322 unset( $concatBlobs[$row->old_id] );
332 print
"\n\nErrors:\n";
335 $description = $this->errorDescriptions[
$name];
336 echo
"$description: " . implode(
',', array_keys(
$errors ) ) .
"\n";
340 if (
count( $this->
errors[
'restore text'] ) && $fix ) {
341 if ( (
string)$xml !==
'' ) {
344 echo
"Can't fix text, no XML backup specified\n";
348 print
"\nFlag statistics:\n";
349 $total = array_sum( $flagStats );
350 foreach ( $flagStats
as $flag => $count ) {
351 printf(
"%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
353 print
"\nLocal object statistics:\n";
354 $total = array_sum( $objectStats );
355 foreach ( $objectStats
as $className => $count ) {
356 printf(
"%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );
361 if ( is_array( $ids ) &&
count( $ids ) == 1 ) {
362 $ids = reset( $ids );
364 if ( is_array( $ids ) ) {
366 foreach ( $ids
as $id ) {
367 $revIds = array_merge( $revIds, array_keys( $this->oldIdMap, $id ) );
369 print
"$msg in text rows " . implode(
', ', $ids ) .
370 ", revisions " . implode(
', ', $revIds ) .
"\n";
373 $revIds = array_keys( $this->oldIdMap, $id );
374 if (
count( $revIds ) == 1 ) {
375 print
"$msg in old_id $id, rev_id {$revIds[0]}\n";
377 print
"$msg in old_id $id, revisions " . implode(
', ', $revIds ) .
"\n";
384 if ( !
count( $externalConcatBlobs ) ) {
388 if ( is_null( $this->dbStore ) ) {
392 foreach ( $externalConcatBlobs
as $cluster => $oldIds ) {
393 $blobIds = array_keys( $oldIds );
394 $extDb =& $this->dbStore->getSlave( $cluster );
395 $blobsTable = $this->dbStore->getTable( $extDb );
396 $headerLength = strlen( self::CONCAT_HEADER );
397 $res = $extDb->select( $blobsTable,
398 [
'blob_id',
"LEFT(blob_text, $headerLength) AS header" ],
399 [
'blob_id IN( ' . implode(
',', $blobIds ) .
')' ], __METHOD__ );
400 foreach (
$res as $row ) {
401 if ( strcasecmp( $row->header, self::CONCAT_HEADER ) ) {
404 "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL",
405 $oldIds[$row->blob_id]
408 unset( $oldIds[$row->blob_id] );
410 $extDb->freeResult(
$res );
413 foreach ( $oldIds
as $blobId => $oldIds2 ) {
416 "Error: missing target $cluster/$blobId for two-part ES URL",
427 if ( !
count( $revIds ) ) {
431 print
"Restoring text from XML backup...\n";
433 $revFileName =
"$tmpDir/broken-revlist-$wgDBname";
434 $filteredXmlFileName =
"$tmpDir/filtered-$wgDBname.xml";
437 if ( !file_put_contents( $revFileName, implode(
"\n", $revIds ) ) ) {
438 echo
"Error writing revision list, can't restore text\n";
444 echo
"Filtering XML dump...\n";
446 passthru(
'mwdumper ' .
448 "--output=file:$filteredXmlFileName",
449 "--filter=revlist:$revFileName",
455 echo
"mwdumper died with exit status $exitStatus\n";
460 $file = fopen( $filteredXmlFileName,
'r' );
462 echo
"Unable to open filtered XML file\n";
475 MediaWikiServices::getInstance()->getMainConfig()
477 $importer->setRevisionCallback( [ $this,
'importRevision' ] );
478 $importer->doImport();
482 $id = $revision->getID();
484 $id = $id ? $id :
'';
486 if ( $content ===
null ) {
487 echo
"Revision $id is broken, we have no content available\n";
492 $text = $content->serialize();
493 if ( $text ===
'' ) {
499 echo
"Revision $id is blank in the dump, may have been broken before export\n";
506 echo
"No id tag in revision, can't import\n";
513 $oldId =
$dbr->selectField(
'revision',
'rev_text_id', [
'rev_id' => $id ], __METHOD__ );
515 echo
"Missing revision row for rev_id $id\n";
525 $dbw->update(
'text',
526 [
'old_flags' =>
$flags,
'old_text' => $text ],
527 [
'old_id' => $oldId ],
528 __METHOD__, [
'LIMIT' => 1 ]
532 unset( $this->
errors[
'restore text'][$id] );
533 $this->
errors[
'fixed'][$id] =
true;