5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance ExternalStorage
24 if ( !defined( 'MEDIAWIKI' ) ) {
25 require_once __DIR__
. '/../commandLine.inc';
27 $cs = new CheckStorage
;
28 $fix = isset( $options['fix'] );
29 if ( isset( $args[0] ) ) {
34 $cs->check( $fix, $xml );
37 // ----------------------------------------------------------------------------------
40 * Maintenance script to do various checks on external storage.
42 * @ingroup Maintenance ExternalStorage
45 const CONCAT_HEADER
= 'O:27:"concatenatedgziphistoryblob"';
46 public $oldIdMap, $errors;
47 public $dbStore = null;
49 public $errorDescriptions = array(
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 = '' ) {
58 $dbr = wfGetDB( DB_SLAVE
);
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' );
69 $this->errors
= array(
70 'restore text' => array(),
71 'restore revision' => array(),
72 'unfixable' => array(),
77 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart +
= $chunkSize ) {
78 $chunkEnd = $chunkStart +
$chunkSize - 1;
79 // print "$chunkStart of $maxRevId\n";
81 // Fetch revision rows
82 $this->oldIdMap
= array();
84 $res = $dbr->select( 'revision', array( 'rev_id', 'rev_text_id' ),
85 array( "rev_id BETWEEN $chunkStart AND $chunkEnd" ), __METHOD__
);
86 foreach ( $res as $row ) {
87 $this->oldIdMap
[$row->rev_id
] = $row->rev_text_id
;
89 $dbr->freeResult( $res );
91 if ( !count( $this->oldIdMap
) ) {
96 $missingTextRows = array_flip( $this->oldIdMap
);
97 $externalRevs = array();
98 $objectRevs = array();
99 $res = $dbr->select( 'text', array( 'old_id', 'old_flags' ),
100 'old_id IN (' . implode( ',', $this->oldIdMap
) . ')', __METHOD__
);
101 foreach ( $res as $row ) {
105 $flags = $row->old_flags
;
108 // Create flagStats row if it doesn't exist
109 $flagStats = $flagStats +
array( $flags => 0 );
111 $flagStats[$flags]++
;
114 unset( $missingTextRows[$row->old_id
] );
116 // Check for external or object
117 if ( $flags == '' ) {
118 $flagArray = array();
120 $flagArray = explode( ',', $flags );
122 if ( in_array( 'external', $flagArray ) ) {
123 $externalRevs[] = $id;
124 } elseif ( in_array( 'object', $flagArray ) ) {
128 // Check for unrecognised flags
129 if ( $flags == '0' ) {
130 // This is a known bug from 2004
131 // It's safe to just erase the old_flags field
133 $this->error( 'fixed', "Warning: old_flags set to 0", $id );
134 $dbw = wfGetDB( DB_MASTER
);
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 );
146 $dbr->freeResult( $res );
148 // Output errors for any missing text rows
149 foreach ( $missingTextRows as $oldId => $revId ) {
150 $this->error( 'restore revision', "Error: missing text row", $oldId );
153 // Verify external revisions
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
;
179 $dbr->freeResult( $res );
182 // Check external concat blobs for the right header
183 $this->checkExternalConcatBlobs( $externalConcatBlobs );
185 // Check external normal blobs for existence
186 if ( count( $externalNormalBlobs ) ) {
187 if ( is_null( $this->dbStore
) ) {
188 $this->dbStore
= new ExternalStoreDB
;
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 );
201 // Print errors for missing blobs rows
202 foreach ( $xBlobIds as $blobId => $oldId ) {
203 $this->error( 'restore text', "Error: missing target $blobId for one-part ES URL", $oldId );
208 // Check local objects
210 $concatBlobs = array();
212 if ( count( $objectRevs ) ) {
216 array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ),
217 array( 'old_id IN (' . implode( ',', $objectRevs ) . ')' ),
220 foreach ( $res as $row ) {
221 $oldId = $row->old_id
;
223 if ( !preg_match( '/^O:(\d+):"(\w+)"/', $row->header
, $matches ) ) {
224 $this->error( 'restore text', "Error: invalid object header", $oldId );
228 $className = strtolower( $matches[2] );
229 if ( strlen( $className ) != $matches[1] ) {
232 "Error: invalid object header, wrong class name length",
238 $objectStats = $objectStats +
array( $className => 0 );
239 $objectStats[$className]++
;
241 switch ( $className ) {
242 case 'concatenatedgziphistoryblob':
245 case 'historyblobstub':
246 case 'historyblobcurstub':
247 if ( strlen( $row->header
) == $headerLength ) {
248 $this->error( 'unfixable', "Error: overlong stub header", $oldId );
251 $stubObj = unserialize( $row->header
);
252 if ( !is_object( $stubObj ) ) {
253 $this->error( 'restore text', "Error: unable to unserialize stub object", $oldId );
256 if ( $className == 'historyblobstub' ) {
257 $concatBlobs[$stubObj->mOldId
][] = $oldId;
259 $curIds[$stubObj->mCurId
][] = $oldId;
263 $this->error( 'unfixable', "Error: unrecognised object class \"$className\"", $oldId );
266 $dbr->freeResult( $res );
269 // Check local concat blob validity
270 $externalConcatBlobs = array();
271 if ( count( $concatBlobs ) ) {
275 array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ),
276 array( 'old_id IN (' . implode( ',', array_keys( $concatBlobs ) ) . ')' ),
279 foreach ( $res as $row ) {
280 $flags = explode( ',', $row->old_flags
);
281 if ( in_array( 'external', $flags ) ) {
282 // Concat blob is in external storage?
283 if ( in_array( 'object', $flags ) ) {
284 $urlParts = explode( '/', $row->header
);
285 if ( $urlParts[0] != 'DB:' ) {
288 "Error: unrecognised external storage type \"{$urlParts[0]}",
292 $cluster = $urlParts[2];
294 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
295 $externalConcatBlobs[$cluster][$id] = array();
297 $externalConcatBlobs[$cluster][$id] = array_merge(
298 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id
]
304 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
305 $concatBlobs[$row->old_id
] );
307 } elseif ( strcasecmp(
308 substr( $row->header
, 0, strlen( self
::CONCAT_HEADER
) ),
313 "Error: Incorrect object header for concat bulk row {$row->old_id}",
314 $concatBlobs[$row->old_id
]
318 unset( $concatBlobs[$row->old_id
] );
320 $dbr->freeResult( $res );
323 // Check targets of unresolved stubs
324 $this->checkExternalConcatBlobs( $externalConcatBlobs );
328 print "\n\nErrors:\n";
329 foreach ( $this->errors
as $name => $errors ) {
330 if ( count( $errors ) ) {
331 $description = $this->errorDescriptions
[$name];
332 echo "$description: " . implode( ',', array_keys( $errors ) ) . "\n";
336 if ( count( $this->errors
['restore text'] ) && $fix ) {
337 if ( (string)$xml !== '' ) {
338 $this->restoreText( array_keys( $this->errors
['restore text'] ), $xml );
340 echo "Can't fix text, no XML backup specified\n";
344 print "\nFlag statistics:\n";
345 $total = array_sum( $flagStats );
346 foreach ( $flagStats as $flag => $count ) {
347 printf( "%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
349 print "\nLocal object statistics:\n";
350 $total = array_sum( $objectStats );
351 foreach ( $objectStats as $className => $count ) {
352 printf( "%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );
356 function error( $type, $msg, $ids ) {
357 if ( is_array( $ids ) && count( $ids ) == 1 ) {
358 $ids = reset( $ids );
360 if ( is_array( $ids ) ) {
362 foreach ( $ids as $id ) {
363 $revIds = array_merge( $revIds, array_keys( $this->oldIdMap
, $id ) );
365 print "$msg in text rows " . implode( ', ', $ids ) .
366 ", revisions " . implode( ', ', $revIds ) . "\n";
369 $revIds = array_keys( $this->oldIdMap
, $id );
370 if ( count( $revIds ) == 1 ) {
371 print "$msg in old_id $id, rev_id {$revIds[0]}\n";
373 print "$msg in old_id $id, revisions " . implode( ', ', $revIds ) . "\n";
376 $this->errors
[$type] = $this->errors
[$type] +
array_flip( $revIds );
379 function checkExternalConcatBlobs( $externalConcatBlobs ) {
380 if ( !count( $externalConcatBlobs ) ) {
384 if ( is_null( $this->dbStore
) ) {
385 $this->dbStore
= new ExternalStoreDB
;
388 foreach ( $externalConcatBlobs as $cluster => $oldIds ) {
389 $blobIds = array_keys( $oldIds );
390 $extDb =& $this->dbStore
->getSlave( $cluster );
391 $blobsTable = $this->dbStore
->getTable( $extDb );
392 $headerLength = strlen( self
::CONCAT_HEADER
);
393 $res = $extDb->select( $blobsTable,
394 array( 'blob_id', "LEFT(blob_text, $headerLength) AS header" ),
395 array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), __METHOD__
);
396 foreach ( $res as $row ) {
397 if ( strcasecmp( $row->header
, self
::CONCAT_HEADER
) ) {
400 "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL",
401 $oldIds[$row->blob_id
]
404 unset( $oldIds[$row->blob_id
] );
406 $extDb->freeResult( $res );
408 // Print errors for missing blobs rows
409 foreach ( $oldIds as $blobId => $oldIds2 ) {
412 "Error: missing target $cluster/$blobId for two-part ES URL",
419 function restoreText( $revIds, $xml ) {
421 $tmpDir = wfTempDir();
423 if ( !count( $revIds ) ) {
427 print "Restoring text from XML backup...\n";
429 $revFileName = "$tmpDir/broken-revlist-$wgDBname";
430 $filteredXmlFileName = "$tmpDir/filtered-$wgDBname.xml";
432 // Write revision list
433 if ( !file_put_contents( $revFileName, implode( "\n", $revIds ) ) ) {
434 echo "Error writing revision list, can't restore text\n";
440 echo "Filtering XML dump...\n";
442 passthru( 'mwdumper ' .
444 "--output=file:$filteredXmlFileName",
445 "--filter=revlist:$revFileName",
451 echo "mwdumper died with exit status $exitStatus\n";
456 $file = fopen( $filteredXmlFileName, 'r' );
458 echo "Unable to open filtered XML file\n";
463 $dbr = wfGetDB( DB_SLAVE
);
464 $dbw = wfGetDB( DB_MASTER
);
468 $source = new ImportStreamSource( $file );
469 $importer = new WikiImporter( $source );
470 $importer->setRevisionCallback( array( &$this, 'importRevision' ) );
471 $importer->doImport();
474 function importRevision( &$revision, &$importer ) {
475 $id = $revision->getID();
476 $content = $revision->getContent( Revision
::RAW
);
477 $id = $id ?
$id : '';
479 if ( $content === null ) {
480 echo "Revision $id is broken, we have no content available\n";
485 $text = $content->serialize();
486 if ( $text === '' ) {
487 // This is what happens if the revision was broken at the time the
488 // dump was made. Unfortunately, it also happens if the revision was
489 // legitimately blank, so there's no way to tell the difference. To
490 // be safe, we'll skip it and leave it broken
492 echo "Revision $id is blank in the dump, may have been broken before export\n";
498 // No ID, can't import
499 echo "No id tag in revision, can't import\n";
504 // Find text row again
505 $dbr = wfGetDB( DB_SLAVE
);
506 $oldId = $dbr->selectField( 'revision', 'rev_text_id', array( 'rev_id' => $id ), __METHOD__
);
508 echo "Missing revision row for rev_id $id\n";
514 $flags = Revision
::compressRevisionText( $text );
516 // Update the text row
517 $dbw = wfGetDB( DB_MASTER
);
518 $dbw->update( 'text',
519 array( 'old_flags' => $flags, 'old_text' => $text ),
520 array( 'old_id' => $oldId ),
521 __METHOD__
, array( 'LIMIT' => 1 )
524 // Remove it from the unfixed list and add it to the fixed list
525 unset( $this->errors
['restore text'][$id] );
526 $this->errors
['fixed'][$id] = true;