24 if ( !defined(
'MEDIAWIKI' ) ) {
25 require_once __DIR__ .
'/../commandLine.inc';
29 if ( isset(
$args[0] ) ) {
34 $cs->check( $fix, $xml );
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',
57 function check( $fix =
false, $xml =
'' ) {
60 print
"Checking, will fix errors if possible...\n";
62 print
"Checking...\n";
64 $maxRevId =
$dbr->selectField(
'revision',
'MAX(rev_id)',
false, __METHOD__ );
67 $objectStats =
array();
68 $knownFlags =
array(
'external',
'gzip',
'object',
'utf-8' );
70 'restore text' =>
array(),
71 'restore revision' =>
array(),
72 'unfixable' =>
array(),
77 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
78 $chunkEnd = $chunkStart + $chunkSize - 1;
82 $this->oldIdMap =
array();
84 $res =
$dbr->select(
'revision',
array(
'rev_id',
'rev_text_id' ),
85 array(
"rev_id BETWEEN $chunkStart AND $chunkEnd" ), __METHOD__ );
87 $this->oldIdMap[$row->rev_id] = $row->rev_text_id;
91 if ( !count( $this->oldIdMap ) ) {
96 $missingTextRows = array_flip( $this->oldIdMap );
97 $externalRevs =
array();
98 $objectRevs =
array();
100 'old_id IN (' . implode(
',', $this->oldIdMap ) .
')', __METHOD__ );
101 foreach (
$res as $row ) {
114 unset( $missingTextRows[$row->old_id] );
118 $flagArray =
array();
120 $flagArray = explode(
',',
$flags );
122 if ( in_array(
'external', $flagArray ) ) {
123 $externalRevs[] = $id;
124 } elseif ( in_array(
'object', $flagArray ) ) {
133 $this->
error(
'fixed',
"Warning: old_flags set to 0", $id );
136 $dbw->update(
'text',
array(
'old_flags' =>
'' ),
137 array(
'old_id' => $id ), __METHOD__ );
140 $this->
error(
'fixable',
"Warning: old_flags set to 0", $id );
142 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
143 $this->
error(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
149 foreach ( $missingTextRows
as $oldId => $revId ) {
150 $this->
error(
'restore revision',
"Error: missing text row", $oldId );
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 );
165 list( $proto, ) = $urlParts;
166 if ( $proto !=
'DB' ) {
167 $this->
error(
'restore text',
"Error: invalid external protocol \"$proto\"", $row->old_id );
170 $path = explode(
'/', $row->old_text );
173 if ( isset(
$path[4] ) ) {
174 $externalConcatBlobs[$cluster][$id][] = $row->old_id;
176 $externalNormalBlobs[$cluster][$id][] = $row->old_id;
186 if ( count( $externalNormalBlobs ) ) {
187 if ( is_null( $this->dbStore ) ) {
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,
196 array(
'blob_id IN( ' . implode(
',', $blobIds ) .
')' ), __METHOD__ );
197 foreach (
$res as $row ) {
198 unset( $xBlobIds[$row->blob_id] );
200 $extDb->freeResult(
$res );
202 foreach ( $xBlobIds
as $blobId => $oldId ) {
203 $this->
error(
'restore text',
"Error: missing target $blobId for one-part ES URL", $oldId );
210 $concatBlobs =
array();
212 if ( count( $objectRevs ) ) {
214 $res =
$dbr->select(
'text',
array(
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ),
215 array(
'old_id IN (' . implode(
',', $objectRevs ) .
')' ), __METHOD__ );
216 foreach (
$res as $row ) {
217 $oldId = $row->old_id;
219 if ( !preg_match(
'/^O:(\d+):"(\w+)"/', $row->header,
$matches ) ) {
220 $this->
error(
'restore text',
"Error: invalid object header", $oldId );
224 $className = strtolower(
$matches[2] );
225 if ( strlen( $className ) !=
$matches[1] ) {
226 $this->
error(
'restore text',
"Error: invalid object header, wrong class name length", $oldId );
230 $objectStats = $objectStats +
array( $className => 0 );
231 $objectStats[$className]++;
233 switch ( $className ) {
234 case 'concatenatedgziphistoryblob':
237 case 'historyblobstub':
238 case 'historyblobcurstub':
239 if ( strlen( $row->header ) == $headerLength ) {
240 $this->
error(
'unfixable',
"Error: overlong stub header", $oldId );
243 $stubObj = unserialize( $row->header );
244 if ( !is_object( $stubObj ) ) {
245 $this->
error(
'restore text',
"Error: unable to unserialize stub object", $oldId );
248 if ( $className ==
'historyblobstub' ) {
249 $concatBlobs[$stubObj->mOldId][] = $oldId;
251 $curIds[$stubObj->mCurId][] = $oldId;
255 $this->
error(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
262 $externalConcatBlobs =
array();
263 if ( count( $concatBlobs ) ) {
265 $res =
$dbr->select(
'text',
array(
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ),
266 array(
'old_id IN (' . implode(
',', array_keys( $concatBlobs ) ) .
')' ), __METHOD__ );
267 foreach (
$res as $row ) {
268 $flags = explode(
',', $row->old_flags );
269 if ( in_array(
'external',
$flags ) ) {
271 if ( in_array(
'object',
$flags ) ) {
272 $urlParts = explode(
'/', $row->header );
273 if ( $urlParts[0] !=
'DB:' ) {
274 $this->
error(
'unfixable',
"Error: unrecognised external storage type \"{$urlParts[0]}", $row->old_id );
276 $cluster = $urlParts[2];
278 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
279 $externalConcatBlobs[$cluster][$id] =
array();
281 $externalConcatBlobs[$cluster][$id] = array_merge(
282 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
286 $this->
error(
'unfixable',
"Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
287 $concatBlobs[$row->old_id] );
289 } elseif ( strcasecmp( substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ), self::CONCAT_HEADER ) ) {
290 $this->
error(
'restore text',
"Error: Incorrect object header for concat bulk row {$row->old_id}",
291 $concatBlobs[$row->old_id] );
294 unset( $concatBlobs[$row->old_id] );
305 print
"\n\nErrors:\n";
308 $description = $this->errorDescriptions[
$name];
309 echo
"$description: " . implode(
',', array_keys(
$errors ) ) .
"\n";
313 if ( count( $this->
errors[
'restore text'] ) && $fix ) {
314 if ( (
string)$xml !==
'' ) {
317 echo
"Can't fix text, no XML backup specified\n";
321 print
"\nFlag statistics:\n";
322 $total = array_sum( $flagStats );
323 foreach ( $flagStats
as $flag =>
$count ) {
326 print
"\nLocal object statistics:\n";
327 $total = array_sum( $objectStats );
328 foreach ( $objectStats
as $className =>
$count ) {
334 if ( is_array( $ids ) && count( $ids ) == 1 ) {
335 $ids = reset( $ids );
337 if ( is_array( $ids ) ) {
339 foreach ( $ids
as $id ) {
340 $revIds = array_merge( $revIds, array_keys( $this->oldIdMap, $id ) );
342 print
"$msg in text rows " . implode(
', ', $ids ) .
343 ", revisions " . implode(
', ', $revIds ) .
"\n";
346 $revIds = array_keys( $this->oldIdMap, $id );
347 if ( count( $revIds ) == 1 ) {
348 print
"$msg in old_id $id, rev_id {$revIds[0]}\n";
350 print
"$msg in old_id $id, revisions " . implode(
', ', $revIds ) .
"\n";
357 if ( !count( $externalConcatBlobs ) ) {
361 if ( is_null( $this->dbStore ) ) {
365 foreach ( $externalConcatBlobs
as $cluster => $oldIds ) {
366 $blobIds = array_keys( $oldIds );
367 $extDb =& $this->dbStore->getSlave( $cluster );
368 $blobsTable = $this->dbStore->getTable( $extDb );
369 $headerLength = strlen( self::CONCAT_HEADER );
370 $res = $extDb->select( $blobsTable,
371 array(
'blob_id',
"LEFT(blob_text, $headerLength) AS header" ),
372 array(
'blob_id IN( ' . implode(
',', $blobIds ) .
')' ), __METHOD__ );
373 foreach (
$res as $row ) {
374 if ( strcasecmp( $row->header, self::CONCAT_HEADER ) ) {
375 $this->
error(
'restore text',
"Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL",
376 $oldIds[$row->blob_id] );
378 unset( $oldIds[$row->blob_id] );
381 $extDb->freeResult(
$res );
384 foreach ( $oldIds
as $blobId => $oldIds2 ) {
385 $this->
error(
'restore text',
"Error: missing target $cluster/$blobId for two-part ES URL", $oldIds2 );
394 if ( !count( $revIds ) ) {
398 print
"Restoring text from XML backup...\n";
400 $revFileName =
"$tmpDir/broken-revlist-$wgDBname";
401 $filteredXmlFileName =
"$tmpDir/filtered-$wgDBname.xml";
404 if ( !file_put_contents( $revFileName, implode(
"\n", $revIds ) ) ) {
405 echo
"Error writing revision list, can't restore text\n";
410 echo
"Filtering XML dump...\n";
412 passthru(
'mwdumper ' .
414 "--output=file:$filteredXmlFileName",
415 "--filter=revlist:$revFileName",
421 echo
"mwdumper died with exit status $exitStatus\n";
425 $file = fopen( $filteredXmlFileName,
'r' );
427 echo
"Unable to open filtered XML file\n";
438 $importer->setRevisionCallback(
array( &$this,
'importRevision' ) );
439 $importer->doImport();
443 $id = $revision->getID();
445 $id = $id ? $id :
'';
447 if ( $content ===
null ) {
448 echo
"Revision $id is broken, we have no content available\n";
452 $text = $content->serialize();
453 if ( $text ===
'' ) {
459 echo
"Revision $id is blank in the dump, may have been broken before export\n";
465 echo
"No id tag in revision, can't import\n";
471 $oldId =
$dbr->selectField(
'revision',
'rev_text_id',
array(
'rev_id' => $id ), __METHOD__ );
473 echo
"Missing revision row for rev_id $id\n";
482 $dbw->update(
'text',
483 array(
'old_flags' =>
$flags,
'old_text' => $text ),
484 array(
'old_id' => $oldId ),
485 __METHOD__,
array(
'LIMIT' => 1 )
489 unset( $this->
errors[
'restore text'][$id] );
490 $this->
errors[
'fixed'][$id] =
true;