60 function check( $fix =
false, $xml =
'' ) {
65 print "Checking, will fix errors if possible...\n";
67 print "Checking...\n";
69 $maxRevId =
$dbr->selectField(
'revision',
'MAX(rev_id)',
'', __METHOD__ );
73 $knownFlags = [
'external',
'gzip',
'object',
'utf-8' ];
76 'restore revision' => [],
82 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
83 $chunkEnd = $chunkStart + $chunkSize - 1;
91 $res =
$dbr->select(
'revision', [
'rev_id',
'rev_text_id' ],
92 [
"rev_id BETWEEN $chunkStart AND $chunkEnd" ], __METHOD__ );
93 foreach (
$res as $row ) {
94 if ( !isset( $this->oldIdMap[ $row->rev_text_id ] ) ) {
95 $this->oldIdMap[ $row->rev_text_id ] = [ $row->rev_id ];
96 } elseif ( !in_array( $row->rev_id, $this->oldIdMap[ $row->rev_text_id ] ) ) {
97 $this->oldIdMap[ $row->rev_text_id ][] = $row->rev_id;
102 [
'slots',
'content' ],
103 [
'slot_revision_id',
'content_address' ],
104 [
"slot_revision_id BETWEEN $chunkStart AND $chunkEnd" ],
107 [
'content' => [
'INNER JOIN', [
'content_id = slot_content_id' ] ] ]
110 $blobStore = MediaWikiServices::getInstance()->getBlobStore();
111 '@phan-var \MediaWiki\Storage\SqlBlobStore $blobStore';
112 foreach (
$res as $row ) {
113 $textId = $blobStore->getTextIdFromAddress( $row->content_address );
115 if ( !isset( $this->oldIdMap[$textId] ) ) {
116 $this->oldIdMap[ $textId ] = [ $row->slot_revision_id ];
117 } elseif ( !in_array( $row->slot_revision_id, $this->oldIdMap[$textId] ) ) {
118 $this->oldIdMap[ $textId ][] = $row->slot_revision_id;
124 if ( !count( $this->oldIdMap ) ) {
134 [
'old_id',
'old_flags' ],
135 [
'old_id' => array_keys( $this->oldIdMap ) ],
138 foreach (
$res as $row ) {
142 $flags = $row->old_flags;
146 $flagStats = $flagStats + [ $flags => 0 ];
148 $flagStats[$flags]++;
151 unset( $missingTextRows[$row->old_id] );
154 if ( $flags ==
'' ) {
157 $flagArray = explode(
',', $flags );
159 if ( in_array(
'external', $flagArray ) ) {
160 $externalRevs[] = $id;
161 } elseif ( in_array(
'object', $flagArray ) ) {
166 if ( $flags ==
'0' ) {
170 $this->
addError(
'fixed',
"Warning: old_flags set to 0", $id );
173 $dbw->update(
'text', [
'old_flags' =>
'' ],
174 [
'old_id' => $id ], __METHOD__ );
177 $this->
addError(
'fixable',
"Warning: old_flags set to 0", $id );
179 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
180 $this->
addError(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
185 foreach ( $missingTextRows as $oldId => $revIds ) {
186 $this->
addError(
'restore revision',
"Error: missing text row", $oldId );
190 $externalConcatBlobs = [];
191 $externalNormalBlobs = [];
192 if ( count( $externalRevs ) ) {
195 [
'old_id',
'old_flags',
'old_text' ],
196 [
'old_id' => $externalRevs ],
199 foreach (
$res as $row ) {
200 $urlParts = explode(
'://', $row->old_text, 2 );
201 if ( count( $urlParts ) !== 2 || $urlParts[1] ==
'' ) {
202 $this->
addError(
'restore text',
"Error: invalid URL \"{$row->old_text}\"", $row->old_id );
205 list( $proto, ) = $urlParts;
206 if ( $proto !=
'DB' ) {
209 "Error: invalid external protocol \"$proto\"",
213 $path = explode(
'/', $row->old_text );
216 if ( isset(
$path[4] ) ) {
217 $externalConcatBlobs[$cluster][$id][] = $row->old_id;
219 $externalNormalBlobs[$cluster][$id][] = $row->old_id;
228 if ( count( $externalNormalBlobs ) ) {
229 if ( is_null( $this->dbStore ) ) {
230 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
231 $this->dbStore = $esFactory->getStore(
'DB' );
233 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
234 $blobIds = array_keys( $xBlobIds );
235 $extDb =& $this->dbStore->getReplica( $cluster );
236 $blobsTable = $this->dbStore->getTable( $extDb );
237 $res = $extDb->select( $blobsTable,
239 [
'blob_id' => $blobIds ],
242 foreach (
$res as $row ) {
243 unset( $xBlobIds[$row->blob_id] );
246 foreach ( $xBlobIds as $blobId => $oldId ) {
249 "Error: missing target $blobId for one-part ES URL",
259 if ( count( $objectRevs ) ) {
263 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
264 [
'old_id' => $objectRevs ],
267 foreach (
$res as $row ) {
268 $oldId = $row->old_id;
270 if ( !preg_match(
'/^O:(\d+):"(\w+)"/', $row->header,
$matches ) ) {
271 $this->
addError(
'restore text',
"Error: invalid object header", $oldId );
275 $className = strtolower(
$matches[2] );
276 if ( strlen( $className ) !=
$matches[1] ) {
279 "Error: invalid object header, wrong class name length",
285 $objectStats = $objectStats + [ $className => 0 ];
286 $objectStats[$className]++;
288 switch ( $className ) {
289 case 'concatenatedgziphistoryblob':
292 case 'historyblobstub':
293 case 'historyblobcurstub':
294 if ( strlen( $row->header ) == $headerLength ) {
295 $this->
addError(
'unfixable',
"Error: overlong stub header", $oldId );
299 if ( !is_object( $stubObj ) ) {
300 $this->
addError(
'restore text',
"Error: unable to unserialize stub object", $oldId );
303 if ( $className ==
'historyblobstub' ) {
304 $concatBlobs[$stubObj->mOldId][] = $oldId;
306 $curIds[$stubObj->mCurId][] = $oldId;
310 $this->
addError(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
316 $externalConcatBlobs = [];
317 if ( count( $concatBlobs ) ) {
321 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
322 [
'old_id' => array_keys( $concatBlobs ) ],
325 foreach (
$res as $row ) {
326 $flags = explode(
',', $row->old_flags );
327 if ( in_array(
'external', $flags ) ) {
329 if ( in_array(
'object', $flags ) ) {
330 $urlParts = explode(
'/', $row->header );
331 if ( $urlParts[0] !=
'DB:' ) {
334 "Error: unrecognised external storage type \"{$urlParts[0]}",
338 $cluster = $urlParts[2];
340 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
341 $externalConcatBlobs[$cluster][$id] = [];
343 $externalConcatBlobs[$cluster][$id] = array_merge(
344 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
350 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
351 $concatBlobs[$row->old_id] );
353 } elseif ( strcasecmp(
354 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
359 "Error: Incorrect object header for concat bulk row {$row->old_id}",
360 $concatBlobs[$row->old_id]
364 unset( $concatBlobs[$row->old_id] );
373 print "\n\nErrors:\n";
374 foreach ( $this->errors as $name =>
$errors ) {
376 $description = $this->errorDescriptions[$name];
377 echo
"$description: " . implode(
',', array_keys(
$errors ) ) .
"\n";
381 if ( count( $this->errors[
'restore text'] ) && $fix ) {
382 if ( (
string)$xml !==
'' ) {
383 $this->
restoreText( array_keys( $this->errors[
'restore text'] ), $xml );
385 echo
"Can't fix text, no XML backup specified\n";
389 print "\nFlag statistics:\n";
390 $total = array_sum( $flagStats );
391 foreach ( $flagStats as $flag => $count ) {
392 printf(
"%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
394 print "\nLocal object statistics:\n";
395 $total = array_sum( $objectStats );
396 foreach ( $objectStats as $className => $count ) {
397 printf(
"%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );
425 if ( !count( $externalConcatBlobs ) ) {
429 if ( is_null( $this->dbStore ) ) {
430 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
431 $this->dbStore = $esFactory->getStore(
'DB' );
434 foreach ( $externalConcatBlobs as $cluster => $oldIds ) {
435 $blobIds = array_keys( $oldIds );
436 $extDb =& $this->dbStore->getReplica( $cluster );
437 $blobsTable = $this->dbStore->getTable( $extDb );
438 $headerLength = strlen( self::CONCAT_HEADER );
439 $res = $extDb->select( $blobsTable,
440 [
'blob_id',
"LEFT(blob_text, $headerLength) AS header" ],
441 [
'blob_id' => $blobIds ],
444 foreach (
$res as $row ) {
445 if ( strcasecmp( $row->header, self::CONCAT_HEADER ) ) {
448 "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL",
449 $oldIds[$row->blob_id]
452 unset( $oldIds[$row->blob_id] );
456 foreach ( $oldIds as $blobId => $oldIds2 ) {
459 "Error: missing target $cluster/$blobId for two-part ES URL",