6 * @ingroup Maintenance ExternalStorage
9 define( 'CONCAT_HEADER', 'O:27:"concatenatedgziphistoryblob"' );
11 if ( !defined( 'MEDIAWIKI' ) ) {
12 require_once( dirname( __FILE__
) . '/../commandLine.inc' );
14 $cs = new CheckStorage
;
15 $fix = isset( $options['fix'] );
16 if ( isset( $args[0] ) ) {
21 $cs->check( $fix, $xml );
25 // ----------------------------------------------------------------------------------
28 * @ingroup Maintenance ExternalStorage
31 var $oldIdMap, $errors;
34 var $errorDescriptions = array(
35 'restore text' => 'Damaged text, need to be restored from a backup',
36 'restore revision' => 'Damaged revision row, need to be restored from a backup',
37 'unfixable' => 'Unexpected errors with no automated fixing method',
38 'fixed' => 'Errors already fixed',
39 'fixable' => 'Errors which would already be fixed if --fix was specified',
42 function check( $fix = false, $xml = '' ) {
43 $fname = 'checkStorage';
44 $dbr = wfGetDB( DB_SLAVE
);
46 $dbw = wfGetDB( DB_MASTER
);
47 print "Checking, will fix errors if possible...\n";
49 print "Checking...\n";
51 $maxRevId = $dbr->selectField( 'revision', 'MAX(rev_id)', false, $fname );
54 $objectStats = array();
55 $knownFlags = array( 'external', 'gzip', 'object', 'utf-8' );
56 $this->errors
= array(
57 'restore text' => array(),
58 'restore revision' => array(),
59 'unfixable' => array(),
64 for ( $chunkStart = 1 ; $chunkStart < $maxRevId; $chunkStart +
= $chunkSize ) {
65 $chunkEnd = $chunkStart +
$chunkSize - 1;
66 // print "$chunkStart of $maxRevId\n";
68 // Fetch revision rows
69 $this->oldIdMap
= array();
71 $res = $dbr->select( 'revision', array( 'rev_id', 'rev_text_id' ),
72 array( "rev_id BETWEEN $chunkStart AND $chunkEnd" ), $fname );
73 foreach ( $res as $row ) {
74 $this->oldIdMap
[$row->rev_id
] = $row->rev_text_id
;
76 $dbr->freeResult( $res );
78 if ( !count( $this->oldIdMap
) ) {
83 $missingTextRows = array_flip( $this->oldIdMap
);
84 $externalRevs = array();
85 $objectRevs = array();
86 $res = $dbr->select( 'text', array( 'old_id', 'old_flags' ),
87 'old_id IN (' . implode( ',', $this->oldIdMap
) . ')', $fname );
88 foreach ( $res as $row ) {
89 $flags = $row->old_flags
;
92 // Create flagStats row if it doesn't exist
93 $flagStats = $flagStats +
array( $flags => 0 );
98 unset( $missingTextRows[$row->old_id
] );
100 // Check for external or object
101 if ( $flags == '' ) {
102 $flagArray = array();
104 $flagArray = explode( ',', $flags );
106 if ( in_array( 'external', $flagArray ) ) {
107 $externalRevs[] = $id;
108 } elseif ( in_array( 'object', $flagArray ) ) {
112 // Check for unrecognised flags
113 if ( $flags == '0' ) {
114 // This is a known bug from 2004
115 // It's safe to just erase the old_flags field
117 $this->error( 'fixed', "Warning: old_flags set to 0", $id );
119 $dbw->update( 'text', array( 'old_flags' => '' ),
120 array( 'old_id' => $id ), $fname );
123 $this->error( 'fixable', "Warning: old_flags set to 0", $id );
125 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
126 $this->error( 'unfixable', "Error: invalid flags field \"$flags\"", $id );
129 $dbr->freeResult( $res );
131 // Output errors for any missing text rows
132 foreach ( $missingTextRows as $oldId => $revId ) {
133 $this->error( 'restore revision', "Error: missing text row", $oldId );
136 // Verify external revisions
137 $externalConcatBlobs = array();
138 $externalNormalBlobs = array();
139 if ( count( $externalRevs ) ) {
140 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ),
141 array( 'old_id IN (' . implode( ',', $externalRevs ) . ')' ), $fname );
142 foreach ( $res as $row ) {
143 $urlParts = explode( '://', $row->old_text
, 2 );
144 if ( count( $urlParts ) !== 2 ||
$urlParts[1] == '' ) {
145 $this->error( 'restore text', "Error: invalid URL \"{$row->old_text}\"", $row->old_id
);
148 list( $proto, ) = $urlParts;
149 if ( $proto != 'DB' ) {
150 $this->error( 'restore text', "Error: invalid external protocol \"$proto\"", $row->old_id
);
153 $path = explode( '/', $row->old_text
);
156 if ( isset( $path[4] ) ) {
157 $externalConcatBlobs[$cluster][$id][] = $row->old_id
;
159 $externalNormalBlobs[$cluster][$id][] = $row->old_id
;
162 $dbr->freeResult( $res );
165 // Check external concat blobs for the right header
166 $this->checkExternalConcatBlobs( $externalConcatBlobs );
168 // Check external normal blobs for existence
169 if ( count( $externalNormalBlobs ) ) {
170 if ( is_null( $this->dbStore
) ) {
171 $this->dbStore
= new ExternalStoreDB
;
173 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
174 $blobIds = array_keys( $xBlobIds );
175 $extDb =& $this->dbStore
->getSlave( $cluster );
176 $blobsTable = $this->dbStore
->getTable( $extDb );
177 $res = $extDb->select( $blobsTable,
179 array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), $fname );
180 foreach ( $res as $row ) {
181 unset( $xBlobIds[$row->blob_id
] );
183 $extDb->freeResult( $res );
184 // Print errors for missing blobs rows
185 foreach ( $xBlobIds as $blobId => $oldId ) {
186 $this->error( 'restore text', "Error: missing target $blobId for one-part ES URL", $oldId );
191 // Check local objects
193 $concatBlobs = array();
195 if ( count( $objectRevs ) ) {
197 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ),
198 array( 'old_id IN (' . implode( ',', $objectRevs ) . ')' ), $fname );
199 foreach ( $res as $row ) {
200 $oldId = $row->old_id
;
202 if ( !preg_match( '/^O:(\d+):"(\w+)"/', $row->header
, $matches ) ) {
203 $this->error( 'restore text', "Error: invalid object header", $oldId );
207 $className = strtolower( $matches[2] );
208 if ( strlen( $className ) != $matches[1] ) {
209 $this->error( 'restore text', "Error: invalid object header, wrong class name length", $oldId );
213 $objectStats = $objectStats +
array( $className => 0 );
214 $objectStats[$className]++
;
216 switch ( $className ) {
217 case 'concatenatedgziphistoryblob':
220 case 'historyblobstub':
221 case 'historyblobcurstub':
222 if ( strlen( $row->header
) == $headerLength ) {
223 $this->error( 'unfixable', "Error: overlong stub header", $oldId );
226 $stubObj = unserialize( $row->header
);
227 if ( !is_object( $stubObj ) ) {
228 $this->error( 'restore text', "Error: unable to unserialize stub object", $oldId );
231 if ( $className == 'historyblobstub' ) {
232 $concatBlobs[$stubObj->mOldId
][] = $oldId;
234 $curIds[$stubObj->mCurId
][] = $oldId;
238 $this->error( 'unfixable', "Error: unrecognised object class \"$className\"", $oldId );
241 $dbr->freeResult( $res );
244 // Check local concat blob validity
245 $externalConcatBlobs = array();
246 if ( count( $concatBlobs ) ) {
248 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ),
249 array( 'old_id IN (' . implode( ',', array_keys( $concatBlobs ) ) . ')' ), $fname );
250 foreach ( $res as $row ) {
251 $flags = explode( ',', $row->old_flags
);
252 if ( in_array( 'external', $flags ) ) {
253 // Concat blob is in external storage?
254 if ( in_array( 'object', $flags ) ) {
255 $urlParts = explode( '/', $row->header
);
256 if ( $urlParts[0] != 'DB:' ) {
257 $this->error( 'unfixable', "Error: unrecognised external storage type \"{$urlParts[0]}", $row->old_id
);
259 $cluster = $urlParts[2];
261 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
262 $externalConcatBlobs[$cluster][$id] = array();
264 $externalConcatBlobs[$cluster][$id] = array_merge(
265 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id
]
269 $this->error( 'unfixable', "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
270 $concatBlobs[$row->old_id
] );
272 } elseif ( strcasecmp( substr( $row->header
, 0, strlen( CONCAT_HEADER
) ), CONCAT_HEADER
) ) {
273 $this->error( 'restore text', "Error: Incorrect object header for concat bulk row {$row->old_id}",
274 $concatBlobs[$row->old_id
] );
277 unset( $concatBlobs[$row->old_id
] );
279 $dbr->freeResult( $res );
282 // Check targets of unresolved stubs
283 $this->checkExternalConcatBlobs( $externalConcatBlobs );
288 print "\n\nErrors:\n";
289 foreach ( $this->errors
as $name => $errors ) {
290 if ( count( $errors ) ) {
291 $description = $this->errorDescriptions
[$name];
292 echo "$description: " . implode( ',', array_keys( $errors ) ) . "\n";
296 if ( count( $this->errors
['restore text'] ) && $fix ) {
297 if ( (string)$xml !== '' ) {
298 $this->restoreText( array_keys( $this->errors
['restore text'] ), $xml );
300 echo "Can't fix text, no XML backup specified\n";
304 print "\nFlag statistics:\n";
305 $total = array_sum( $flagStats );
306 foreach ( $flagStats as $flag => $count ) {
307 printf( "%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
309 print "\nLocal object statistics:\n";
310 $total = array_sum( $objectStats );
311 foreach ( $objectStats as $className => $count ) {
312 printf( "%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );
317 function error( $type, $msg, $ids ) {
318 if ( is_array( $ids ) && count( $ids ) == 1 ) {
319 $ids = reset( $ids );
321 if ( is_array( $ids ) ) {
323 foreach ( $ids as $id ) {
324 $revIds = array_merge( $revIds, array_keys( $this->oldIdMap
, $id ) );
326 print "$msg in text rows " . implode( ', ', $ids ) .
327 ", revisions " . implode( ', ', $revIds ) . "\n";
330 $revIds = array_keys( $this->oldIdMap
, $id );
331 if ( count( $revIds ) == 1 ) {
332 print "$msg in old_id $id, rev_id {$revIds[0]}\n";
334 print "$msg in old_id $id, revisions " . implode( ', ', $revIds ) . "\n";
337 $this->errors
[$type] = $this->errors
[$type] +
array_flip( $revIds );
340 function checkExternalConcatBlobs( $externalConcatBlobs ) {
341 $fname = 'CheckStorage::checkExternalConcatBlobs';
342 if ( !count( $externalConcatBlobs ) ) {
346 if ( is_null( $this->dbStore
) ) {
347 $this->dbStore
= new ExternalStoreDB
;
350 foreach ( $externalConcatBlobs as $cluster => $oldIds ) {
351 $blobIds = array_keys( $oldIds );
352 $extDb =& $this->dbStore
->getSlave( $cluster );
353 $blobsTable = $this->dbStore
->getTable( $extDb );
354 $headerLength = strlen( CONCAT_HEADER
);
355 $res = $extDb->select( $blobsTable,
356 array( 'blob_id', "LEFT(blob_text, $headerLength) AS header" ),
357 array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), $fname );
358 foreach ( $res as $row ) {
359 if ( strcasecmp( $row->header
, CONCAT_HEADER
) ) {
360 $this->error( 'restore text', "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL",
361 $oldIds[$row->blob_id
] );
363 unset( $oldIds[$row->blob_id
] );
366 $extDb->freeResult( $res );
368 // Print errors for missing blobs rows
369 foreach ( $oldIds as $blobId => $oldIds ) {
370 $this->error( 'restore text', "Error: missing target $cluster/$blobId for two-part ES URL", $oldIds );
375 function restoreText( $revIds, $xml ) {
376 global $wgTmpDirectory, $wgDBname;
378 if ( !count( $revIds ) ) {
382 print "Restoring text from XML backup...\n";
384 $revFileName = "$wgTmpDirectory/broken-revlist-$wgDBname";
385 $filteredXmlFileName = "$wgTmpDirectory/filtered-$wgDBname.xml";
387 // Write revision list
388 if ( !file_put_contents( $revFileName, implode( "\n", $revIds ) ) ) {
389 echo "Error writing revision list, can't restore text\n";
394 echo "Filtering XML dump...\n";
396 passthru( 'mwdumper ' .
398 "--output=file:$filteredXmlFileName",
399 "--filter=revlist:$revFileName",
405 echo "mwdumper died with exit status $exitStatus\n";
409 $file = fopen( $filteredXmlFileName, 'r' );
411 echo "Unable to open filtered XML file\n";
415 $dbr = wfGetDB( DB_SLAVE
);
416 $dbw = wfGetDB( DB_MASTER
);
420 $source = new ImportStreamSource( $file );
421 $importer = new WikiImporter( $source );
422 $importer->setRevisionCallback( array( &$this, 'importRevision' ) );
423 $importer->doImport();
426 function importRevision( &$revision, &$importer ) {
427 $fname = 'CheckStorage::importRevision';
429 $id = $revision->getID();
430 $text = $revision->getText();
431 if ( $text === '' ) {
432 // This is what happens if the revision was broken at the time the
433 // dump was made. Unfortunately, it also happens if the revision was
434 // legitimately blank, so there's no way to tell the difference. To
435 // be safe, we'll skip it and leave it broken
436 $id = $id ?
$id : '';
437 echo "Revision $id is blank in the dump, may have been broken before export\n";
442 // No ID, can't import
443 echo "No id tag in revision, can't import\n";
447 // Find text row again
448 $dbr = wfGetDB( DB_SLAVE
);
449 $oldId = $dbr->selectField( 'revision', 'rev_text_id', array( 'rev_id' => $id ), $fname );
451 echo "Missing revision row for rev_id $id\n";
456 $flags = Revision
::compressRevisionText( $text );
458 // Update the text row
459 $dbw = wfGetDB( DB_MASTER
);
460 $dbw->update( 'text',
461 array( 'old_flags' => $flags, 'old_text' => $text ),
462 array( 'old_id' => $oldId ),
463 $fname, array( 'LIMIT' => 1 )
466 // Remove it from the unfixed list and add it to the fixed list
467 unset( $this->errors
['restore text'][$id] );
468 $this->errors
['fixed'][$id] = true;