7 define( 'CONCAT_HEADER', 'O:27:"concatenatedgziphistoryblob"' );
9 if ( !defined( 'MEDIAWIKI' ) ) {
10 require_once( dirname(__FILE__
) . '/../commandLine.inc' );
11 require_once( 'ExternalStore.php' );
12 require_once( 'ExternalStoreDB.php' );
13 require_once( 'SpecialImport.php' );
15 $cs = new CheckStorage
;
16 $fix = isset( $options['fix'] );
17 if ( isset( $args[0] ) ) {
22 $cs->check( $fix, $xml );
26 //----------------------------------------------------------------------------------
30 var $oldIdMap, $errors;
33 var $errorDescriptions = array(
34 'restore text' => 'Damaged text, need to be restored from a backup',
35 'restore revision' => 'Damaged revision row, need to be restored from a backup',
36 'unfixable' => 'Unexpected errors with no automated fixing method',
37 'fixed' => 'Errors already fixed',
38 'fixable' => 'Errors which would already be fixed if --fix was specified',
41 function check( $fix = false, $xml = '' ) {
42 $fname = 'checkStorage';
43 $dbr =& wfGetDB( DB_SLAVE
);
45 $dbw =& wfGetDB( DB_MASTER
);
46 print "Checking, will fix errors if possible...\n";
48 print "Checking...\n";
50 $maxRevId = $dbr->selectField( 'revision', 'MAX(rev_id)', false, $fname );
53 $objectStats = array();
54 $knownFlags = array( 'external', 'gzip', 'object', 'utf-8' );
55 $this->errors
= array(
56 'restore text' => array(),
57 'restore revision' => array(),
58 'unfixable' => array(),
63 for ( $chunkStart = 1 ; $chunkStart < $maxRevId; $chunkStart +
= $chunkSize ) {
64 $chunkEnd = $chunkStart +
$chunkSize - 1;
65 //print "$chunkStart of $maxRevId\n";
67 // Fetch revision rows
68 $this->oldIdMap
= array();
70 $res = $dbr->select( 'revision', array( 'rev_id', 'rev_text_id' ),
71 array( "rev_id BETWEEN $chunkStart AND $chunkEnd" ), $fname );
72 while ( $row = $dbr->fetchObject( $res ) ) {
73 $this->oldIdMap
[$row->rev_id
] = $row->rev_text_id
;
75 $dbr->freeResult( $res );
77 if ( !count( $this->oldIdMap
) ) {
82 $missingTextRows = array_flip( $this->oldIdMap
);
83 $externalRevs = array();
84 $objectRevs = array();
85 $res = $dbr->select( 'text', array( 'old_id', 'old_flags' ),
86 'old_id IN (' . implode( ',', $this->oldIdMap
) . ')', $fname );
87 while ( $row = $dbr->fetchObject( $res ) ) {
88 $flags = $row->old_flags
;
91 // Create flagStats row if it doesn't exist
92 $flagStats = $flagStats +
array( $flags => 0 );
97 unset( $missingTextRows[$row->old_id
] );
99 // Check for external or object
100 if ( $flags == '' ) {
101 $flagArray = array();
103 $flagArray = explode( ',', $flags );
105 if ( in_array( 'external', $flagArray ) ) {
106 $externalRevs[] = $id;
107 } elseif ( in_array( 'object', $flagArray ) ) {
111 // Check for unrecognised flags
112 if ( $flags == '0' ) {
113 // This is a known bug from 2004
114 // It's safe to just erase the old_flags field
116 $this->error( 'fixed', "Warning: old_flags set to 0", $id );
118 $dbw->update( 'text', array( 'old_flags' => '' ),
119 array( 'old_id' => $id ), $fname );
122 $this->error( 'fixable', "Warning: old_flags set to 0", $id );
124 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
125 $this->error( 'unfixable', "Error: invalid flags field \"$flags\"", $id );
128 $dbr->freeResult( $res );
130 // Output errors for any missing text rows
131 foreach ( $missingTextRows as $oldId => $revId ) {
132 $this->error( 'restore revision', "Error: missing text row", $oldId );
135 // Verify external revisions
136 $externalConcatBlobs = array();
137 $externalNormalBlobs = array();
138 if ( count( $externalRevs ) ) {
139 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ),
140 array( 'old_id IN (' . implode( ',', $externalRevs ) . ')' ), $fname );
141 while ( $row = $dbr->fetchObject( $res ) ) {
142 $urlParts = explode( '://', $row->old_text
, 2 );
143 if ( count( $urlParts ) !== 2 ||
$urlParts[1] == '' ) {
144 $this->error( 'restore text', "Error: invalid URL \"{$row->old_text}\"", $row->old_id
);
147 list( $proto, $path ) = $urlParts;
148 if ( $proto != 'DB' ) {
149 $this->error( 'restore text', "Error: invalid external protocol \"$proto\"", $row->old_id
);
152 $path = explode( '/', $row->old_text
);
155 if ( isset( $path[4] ) ) {
156 $externalConcatBlobs[$cluster][$id][] = $row->old_id
;
158 $externalNormalBlobs[$cluster][$id][] = $row->old_id
;
161 $dbr->freeResult( $res );
164 // Check external concat blobs for the right header
165 $this->checkExternalConcatBlobs( $externalConcatBlobs );
167 // Check external normal blobs for existence
168 if ( count( $externalNormalBlobs ) ) {
169 if ( is_null( $this->dbStore
) ) {
170 $this->dbStore
= new ExternalStoreDB
;
172 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
173 $blobIds = array_keys( $xBlobIds );
174 $extDb =& $this->dbStore
->getSlave( $cluster );
175 $blobsTable = $this->dbStore
->getTable( $extDb );
176 $res = $extDb->select( $blobsTable,
178 array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), $fname );
179 while ( $row = $extDb->fetchObject( $res ) ) {
180 unset( $xBlobIds[$row->blob_id
] );
182 $extDb->freeResult( $res );
183 // Print errors for missing blobs rows
184 foreach ( $xBlobIds as $blobId => $oldId ) {
185 $this->error( 'restore text', "Error: missing target $blobId for one-part ES URL", $oldId );
190 // Check local objects
192 $concatBlobs = array();
194 if ( count( $objectRevs ) ) {
196 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ),
197 array( 'old_id IN (' . implode( ',', $objectRevs ) . ')' ), $fname );
198 while ( $row = $dbr->fetchObject( $res ) ) {
199 $oldId = $row->old_id
;
200 if ( !preg_match( '/^O:(\d+):"(\w+)"/', $row->header
, $matches ) ) {
201 $this->error( 'restore text', "Error: invalid object header", $oldId );
205 $className = strtolower( $matches[2] );
206 if ( strlen( $className ) != $matches[1] ) {
207 $this->error( 'restore text', "Error: invalid object header, wrong class name length", $oldId );
211 $objectStats = $objectStats +
array( $className => 0 );
212 $objectStats[$className]++
;
214 switch ( $className ) {
215 case 'concatenatedgziphistoryblob':
218 case 'historyblobstub':
219 case 'historyblobcurstub':
220 if ( strlen( $row->header
) == $headerLength ) {
221 $this->error( 'unfixable', "Error: overlong stub header", $oldId );
224 $stubObj = unserialize( $row->header
);
225 if ( !is_object( $stubObj ) ) {
226 $this->error( 'restore text', "Error: unable to unserialize stub object", $oldId );
229 if ( $className == 'historyblobstub' ) {
230 $concatBlobs[$stubObj->mOldId
][] = $oldId;
232 $curIds[$stubObj->mCurId
][] = $oldId;
236 $this->error( 'unfixable', "Error: unrecognised object class \"$className\"", $oldId );
239 $dbr->freeResult( $res );
242 // Check local concat blob validity
243 $externalConcatBlobs = array();
244 if ( count( $concatBlobs ) ) {
246 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ),
247 array( 'old_id IN (' . implode( ',', array_keys( $concatBlobs ) ) . ')' ), $fname );
248 while ( $row = $dbr->fetchObject( $res ) ) {
249 $flags = explode( ',', $row->old_flags
);
250 if ( in_array( 'external', $flags ) ) {
251 // Concat blob is in external storage?
252 if ( in_array( 'object', $flags ) ) {
253 $urlParts = explode( '/', $row->header
);
254 if ( $urlParts[0] != 'DB:' ) {
255 $this->error( 'unfixable', "Error: unrecognised external storage type \"{$urlParts[0]}", $row->old_id
);
257 $cluster = $urlParts[2];
259 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
260 $externalConcatBlobs[$cluster][$id] = array();
262 $externalConcatBlobs[$cluster][$id] = array_merge(
263 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id
]
267 $this->error( 'unfixable', "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
268 $concatBlobs[$row->old_id
] );
270 } elseif ( strcasecmp( substr( $row->header
, 0, strlen( CONCAT_HEADER
) ), CONCAT_HEADER
) ) {
271 $this->error( 'restore text', "Error: Incorrect object header for concat bulk row {$row->old_id}",
272 $concatBlobs[$row->old_id
] );
275 unset( $concatBlobs[$row->old_id
] );
277 $dbr->freeResult( $res );
280 // Check targets of unresolved stubs
281 $this->checkExternalConcatBlobs( $externalConcatBlobs );
286 print "\n\nErrors:\n";
287 foreach( $this->errors
as $name => $errors ) {
288 if ( count( $errors ) ) {
289 $description = $this->errorDescriptions
[$name];
290 echo "$description: " . implode( ',', array_keys( $errors ) ) . "\n";
294 if ( count( $this->errors
['restore text'] ) && $fix ) {
295 if ( (string)$xml !== '' ) {
296 $this->restoreText( array_keys( $this->errors
['restore text'] ), $xml );
298 echo "Can't fix text, no XML backup specified\n";
302 print "\nFlag statistics:\n";
303 $total = array_sum( $flagStats );
304 foreach ( $flagStats as $flag => $count ) {
305 printf( "%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
307 print "\nLocal object statistics:\n";
308 $total = array_sum( $objectStats );
309 foreach ( $objectStats as $className => $count ) {
310 printf( "%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );
315 function error( $type, $msg, $ids ) {
316 if ( is_array( $ids ) && count( $ids ) == 1 ) {
317 $ids = reset( $ids );
319 if ( is_array( $ids ) ) {
321 foreach ( $ids as $id ) {
322 $revIds = array_merge( $revIds, array_keys( $this->oldIdMap
, $id ) );
324 print "$msg in text rows " . implode( ', ', $ids ) .
325 ", revisions " . implode( ', ', $revIds ) . "\n";
328 $revIds = array_keys( $this->oldIdMap
, $id );
329 if ( count( $revIds ) == 1 ) {
330 print "$msg in old_id $id, rev_id {$revIds[0]}\n";
332 print "$msg in old_id $id, revisions " . implode( ', ', $revIds ) . "\n";
335 $this->errors
[$type] = $this->errors
[$type] +
array_flip( $revIds );
338 function checkExternalConcatBlobs( $externalConcatBlobs ) {
339 $fname = 'CheckStorage::checkExternalConcatBlobs';
340 if ( !count( $externalConcatBlobs ) ) {
344 if ( is_null( $this->dbStore
) ) {
345 $this->dbStore
= new ExternalStoreDB
;
348 foreach ( $externalConcatBlobs as $cluster => $oldIds ) {
349 $blobIds = array_keys( $oldIds );
350 $extDb =& $this->dbStore
->getSlave( $cluster );
351 $blobsTable = $this->dbStore
->getTable( $extDb );
352 $headerLength = strlen( CONCAT_HEADER
);
353 $res = $extDb->select( $blobsTable,
354 array( 'blob_id', "LEFT(blob_text, $headerLength) AS header" ),
355 array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), $fname );
356 while ( $row = $extDb->fetchObject( $res ) ) {
357 if ( strcasecmp( $row->header
, CONCAT_HEADER
) ) {
358 $this->error( 'restore text', "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL",
359 $oldIds[$row->blob_id
] );
361 unset( $oldIds[$row->blob_id
] );
364 $extDb->freeResult( $res );
366 // Print errors for missing blobs rows
367 foreach ( $oldIds as $blobId => $oldIds ) {
368 $this->error( 'restore text', "Error: missing target $cluster/$blobId for two-part ES URL", $oldIds );
373 function restoreText( $revIds, $xml ) {
374 global $wgTmpDirectory, $wgDBname;
376 if ( !count( $revIds ) ) {
380 print "Restoring text from XML backup...\n";
382 $revFileName = "$wgTmpDirectory/broken-revlist-$wgDBname";
383 $filteredXmlFileName = "$wgTmpDirectory/filtered-$wgDBname.xml";
385 // Write revision list
386 if ( !file_put_contents( $revFileName, implode( "\n", $revIds ) ) ) {
387 echo "Error writing revision list, can't restore text\n";
392 echo "Filtering XML dump...\n";
394 passthru( 'mwdumper ' .
396 "--output=file:$filteredXmlFileName",
397 "--filter=revlist:$revFileName",
403 echo "mwdumper died with exit status $exitStatus\n";
407 $file = fopen( $filteredXmlFileName, 'r' );
409 echo "Unable to open filtered XML file\n";
413 $dbr =& wfGetDB( DB_SLAVE
);
414 $dbw =& wfGetDB( DB_MASTER
);
418 $source = new ImportStreamSource( $file );
419 $importer = new WikiImporter( $source );
420 $importer->setRevisionCallback( array( &$this, 'importRevision' ) );
421 $importer->doImport();
424 function importRevision( &$revision, &$importer ) {
425 $fname = 'CheckStorage::importRevision';
427 $id = $revision->getID();
428 $text = $revision->getText();
429 if ( $text === '' ) {
430 // This is what happens if the revision was broken at the time the
431 // dump was made. Unfortunately, it also happens if the revision was
432 // legitimately blank, so there's no way to tell the difference. To
433 // be safe, we'll skip it and leave it broken
434 $id = $id ?
$id : '';
435 echo "Revision $id is blank in the dump, may have been broken before export\n";
440 // No ID, can't import
441 echo "No id tag in revision, can't import\n";
445 // Find text row again
446 $dbr =& wfGetDB( DB_SLAVE
);
447 $oldId = $dbr->selectField( 'revision', 'rev_text_id', array( 'rev_id' => $id ), $fname );
449 echo "Missing revision row for rev_id $id\n";
454 $flags = Revision
::compressRevisionText( $text );
456 // Update the text row
457 $dbw->update( 'text',
458 array( 'old_flags' => $flags, 'old_text' => $text ),
459 array( 'old_id' => $oldId ),
460 $fname, array( 'LIMIT' => 1 )
463 // Remove it from the unfixed list and add it to the fixed list
464 unset( $this->errors
['restore text'][$id] );
465 $this->errors
['fixed'][$id] = true;