59 public function check( $fix =
false, $xml =
'' ) {
62 print "Checking, will fix errors if possible...\n";
64 print "Checking...\n";
66 $maxRevId =
$dbr->selectField(
'revision',
'MAX(rev_id)',
'', __METHOD__ );
70 $knownFlags = [
'external',
'gzip',
'object',
'utf-8' ];
73 'restore revision' => [],
79 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
80 $chunkEnd = $chunkStart + $chunkSize - 1;
88 [
'slots',
'content' ],
89 [
'slot_revision_id',
'content_address' ],
90 [
"slot_revision_id BETWEEN $chunkStart AND $chunkEnd" ],
93 [
'content' => [
'INNER JOIN', [
'content_id = slot_content_id' ] ] ]
96 $blobStore = MediaWikiServices::getInstance()->getBlobStore();
97 '@phan-var \MediaWiki\Storage\SqlBlobStore $blobStore';
98 foreach (
$res as $row ) {
99 $textId = $blobStore->getTextIdFromAddress( $row->content_address );
101 if ( !isset( $this->oldIdMap[$textId] ) ) {
102 $this->oldIdMap[ $textId ] = [ $row->slot_revision_id ];
103 } elseif ( !in_array( $row->slot_revision_id, $this->oldIdMap[$textId] ) ) {
104 $this->oldIdMap[ $textId ][] = $row->slot_revision_id;
109 if ( !count( $this->oldIdMap ) ) {
119 [
'old_id',
'old_flags' ],
120 [
'old_id' => array_keys( $this->oldIdMap ) ],
123 foreach (
$res as $row ) {
127 $flags = $row->old_flags;
131 $flagStats += [ $flags => 0 ];
133 $flagStats[$flags]++;
136 unset( $missingTextRows[$row->old_id] );
139 if ( $flags ==
'' ) {
142 $flagArray = explode(
',', $flags );
144 if ( in_array(
'external', $flagArray ) ) {
145 $externalRevs[] = $id;
146 } elseif ( in_array(
'object', $flagArray ) ) {
151 if ( $flags ==
'0' ) {
155 $this->
addError(
'fixed',
"Warning: old_flags set to 0", $id );
158 $dbw->update(
'text', [
'old_flags' =>
'' ],
159 [
'old_id' => $id ], __METHOD__ );
162 $this->
addError(
'fixable',
"Warning: old_flags set to 0", $id );
164 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
165 $this->
addError(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
170 foreach ( $missingTextRows as $oldId => $revIds ) {
171 $this->
addError(
'restore revision',
"Error: missing text row", $oldId );
175 $externalConcatBlobs = [];
176 $externalNormalBlobs = [];
177 if ( count( $externalRevs ) ) {
180 [
'old_id',
'old_flags',
'old_text' ],
181 [
'old_id' => $externalRevs ],
184 foreach (
$res as $row ) {
185 $urlParts = explode(
'://', $row->old_text, 2 );
186 if ( count( $urlParts ) !== 2 || $urlParts[1] ==
'' ) {
187 $this->
addError(
'restore text',
"Error: invalid URL \"{$row->old_text}\"", $row->old_id );
190 list( $proto, ) = $urlParts;
191 if ( $proto !=
'DB' ) {
194 "Error: invalid external protocol \"$proto\"",
198 $path = explode(
'/', $row->old_text );
201 if ( isset(
$path[4] ) ) {
202 $externalConcatBlobs[$cluster][$id][] = $row->old_id;
204 $externalNormalBlobs[$cluster][$id][] = $row->old_id;
213 if ( count( $externalNormalBlobs ) ) {
214 if ( $this->dbStore ===
null ) {
215 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
216 $this->dbStore = $esFactory->getStore(
'DB' );
218 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
219 $blobIds = array_keys( $xBlobIds );
220 $extDb =& $this->dbStore->getReplica( $cluster );
221 $blobsTable = $this->dbStore->getTable( $extDb );
222 $res = $extDb->select( $blobsTable,
224 [
'blob_id' => $blobIds ],
227 foreach (
$res as $row ) {
228 unset( $xBlobIds[$row->blob_id] );
231 foreach ( $xBlobIds as $blobId => $oldId ) {
234 "Error: missing target $blobId for one-part ES URL",
244 if ( count( $objectRevs ) ) {
248 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
249 [
'old_id' => $objectRevs ],
252 foreach (
$res as $row ) {
253 $oldId = $row->old_id;
255 if ( !preg_match(
'/^O:(\d+):"(\w+)"/', $row->header,
$matches ) ) {
256 $this->
addError(
'restore text',
"Error: invalid object header", $oldId );
260 $className = strtolower(
$matches[2] );
261 if ( strlen( $className ) !=
$matches[1] ) {
264 "Error: invalid object header, wrong class name length",
270 $objectStats += [ $className => 0 ];
271 $objectStats[$className]++;
273 switch ( $className ) {
274 case 'concatenatedgziphistoryblob':
277 case 'historyblobstub':
278 case 'historyblobcurstub':
279 if ( strlen( $row->header ) == $headerLength ) {
280 $this->
addError(
'unfixable',
"Error: overlong stub header", $oldId );
284 if ( !is_object( $stubObj ) ) {
285 $this->
addError(
'restore text',
"Error: unable to unserialize stub object", $oldId );
288 if ( $className ==
'historyblobstub' ) {
289 $concatBlobs[$stubObj->getLocation()][] = $oldId;
291 $curIds[$stubObj->mCurId][] = $oldId;
295 $this->
addError(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
301 $externalConcatBlobs = [];
302 if ( count( $concatBlobs ) ) {
306 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
307 [
'old_id' => array_keys( $concatBlobs ) ],
310 foreach (
$res as $row ) {
311 $flags = explode(
',', $row->old_flags );
312 if ( in_array(
'external', $flags ) ) {
314 if ( in_array(
'object', $flags ) ) {
315 $urlParts = explode(
'/', $row->header );
316 if ( $urlParts[0] !=
'DB:' ) {
319 "Error: unrecognised external storage type \"{$urlParts[0]}",
323 $cluster = $urlParts[2];
325 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
326 $externalConcatBlobs[$cluster][$id] = [];
328 $externalConcatBlobs[$cluster][$id] = array_merge(
329 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
335 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
336 $concatBlobs[$row->old_id] );
338 } elseif ( strcasecmp(
339 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
344 "Error: Incorrect object header for concat bulk row {$row->old_id}",
345 $concatBlobs[$row->old_id]
349 unset( $concatBlobs[$row->old_id] );
358 print "\n\nErrors:\n";
359 foreach ( $this->errors as $name =>
$errors ) {
362 $description = $this->errorDescriptions[$name];
363 echo
"$description: " . implode(
',', array_keys(
$errors ) ) .
"\n";
368 if ( count( $this->errors[
'restore text'] ) && $fix ) {
369 if ( (
string)$xml !==
'' ) {
370 $this->
restoreText( array_keys( $this->errors[
'restore text'] ), $xml );
372 echo
"Can't fix text, no XML backup specified\n";
376 print "\nFlag statistics:\n";
377 $total = array_sum( $flagStats );
378 foreach ( $flagStats as $flag => $count ) {
379 printf(
"%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
381 print "\nLocal object statistics:\n";
382 $total = array_sum( $objectStats );
383 foreach ( $objectStats as $className => $count ) {
384 printf(
"%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );
412 if ( !count( $externalConcatBlobs ) ) {
416 if ( $this->dbStore ===
null ) {
417 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
418 $this->dbStore = $esFactory->getStore(
'DB' );
421 foreach ( $externalConcatBlobs as $cluster => $oldIds ) {
422 $blobIds = array_keys( $oldIds );
423 $extDb =& $this->dbStore->getReplica( $cluster );
424 $blobsTable = $this->dbStore->getTable( $extDb );
425 $headerLength = strlen( self::CONCAT_HEADER );
426 $res = $extDb->select( $blobsTable,
427 [
'blob_id',
"LEFT(blob_text, $headerLength) AS header" ],
428 [
'blob_id' => $blobIds ],
431 foreach (
$res as $row ) {
432 if ( strcasecmp( $row->header, self::CONCAT_HEADER ) ) {
435 "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL",
436 $oldIds[$row->blob_id]
439 unset( $oldIds[$row->blob_id] );
443 foreach ( $oldIds as $blobId => $oldIds2 ) {
446 "Error: missing target $cluster/$blobId for two-part ES URL",