58 function check( $fix =
false, $xml =
'' ) {
61 print "Checking, will fix errors if possible...\n";
63 print "Checking...\n";
65 $maxRevId =
$dbr->selectField(
'revision',
'MAX(rev_id)',
'', __METHOD__ );
69 $knownFlags = [
'external',
'gzip',
'object',
'utf-8' ];
72 'restore revision' => [],
79 $chunkEnd = $chunkStart + $chunkSize - 1;
85 $res =
$dbr->select(
'revision', [
'rev_id',
'rev_text_id' ],
86 [
"rev_id BETWEEN $chunkStart AND $chunkEnd" ], __METHOD__ );
87 foreach (
$res as $row ) {
88 $this->oldIdMap[$row->rev_id] = $row->rev_text_id;
91 if ( !count( $this->oldIdMap ) ) {
96 $missingTextRows =
array_flip( $this->oldIdMap );
101 [
'old_id',
'old_flags' ],
102 [
'old_id' => $this->oldIdMap ],
105 foreach (
$res as $row ) {
109 $flags = $row->old_flags;
113 $flagStats = $flagStats + [ $flags => 0 ];
115 $flagStats[$flags]++;
118 unset( $missingTextRows[$row->old_id] );
121 if ( $flags ==
'' ) {
124 $flagArray = explode(
',', $flags );
126 if (
in_array(
'external', $flagArray ) ) {
127 $externalRevs[] = $id;
133 if ( $flags ==
'0' ) {
137 $this->
addError(
'fixed',
"Warning: old_flags set to 0", $id );
140 $dbw->update(
'text', [
'old_flags' =>
'' ],
141 [
'old_id' => $id ], __METHOD__ );
144 $this->
addError(
'fixable',
"Warning: old_flags set to 0", $id );
147 $this->
addError(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
152 foreach ( $missingTextRows as $oldId => $revId ) {
153 $this->
addError(
'restore revision',
"Error: missing text row", $oldId );
157 $externalConcatBlobs = [];
158 $externalNormalBlobs = [];
159 if ( count( $externalRevs ) ) {
162 [
'old_id',
'old_flags',
'old_text' ],
163 [
'old_id' => $externalRevs ],
166 foreach (
$res as $row ) {
167 $urlParts = explode(
'://', $row->old_text, 2 );
168 if ( count( $urlParts ) !== 2 || $urlParts[1] ==
'' ) {
169 $this->
addError(
'restore text',
"Error: invalid URL \"{$row->old_text}\"", $row->old_id );
173 if ( $proto !=
'DB' ) {
176 "Error: invalid external protocol \"$proto\"",
180 $path = explode(
'/', $row->old_text );
183 if (
isset( $path[4] ) ) {
184 $externalConcatBlobs[$cluster][$id][] = $row->old_id;
186 $externalNormalBlobs[$cluster][$id][] = $row->old_id;
195 if ( count( $externalNormalBlobs ) ) {
196 if (
is_null( $this->dbStore ) ) {
199 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
201 $extDb =& $this->dbStore->getSlave( $cluster );
202 $blobsTable = $this->dbStore->getTable( $extDb );
203 $res = $extDb->select( $blobsTable,
205 [
'blob_id' => $blobIds ],
208 foreach (
$res as $row ) {
209 unset( $xBlobIds[$row->blob_id] );
212 foreach ( $xBlobIds as $blobId => $oldId ) {
215 "Error: missing target $blobId for one-part ES URL",
225 if ( count( $objectRevs ) ) {
229 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
230 [
'old_id' => $objectRevs ],
233 foreach (
$res as $row ) {
234 $oldId = $row->old_id;
237 $this->
addError(
'restore text',
"Error: invalid object header", $oldId );
245 "Error: invalid object header, wrong class name length",
251 $objectStats = $objectStats + [ $className => 0 ];
254 switch ( $className ) {
255 case 'concatenatedgziphistoryblob':
258 case 'historyblobstub':
259 case 'historyblobcurstub':
260 if (
strlen( $row->header ) == $headerLength ) {
261 $this->
addError(
'unfixable',
"Error: overlong stub header", $oldId );
266 $this->
addError(
'restore text',
"Error: unable to unserialize stub object", $oldId );
269 if ( $className ==
'historyblobstub' ) {
270 $concatBlobs[$stubObj->mOldId][] =
$oldId;
272 $curIds[$stubObj->mCurId][] =
$oldId;
276 $this->
addError(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
282 $externalConcatBlobs = [];
283 if ( count( $concatBlobs ) ) {
287 [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ],
291 foreach (
$res as $row ) {
292 $flags = explode(
',', $row->old_flags );
293 if (
in_array(
'external', $flags ) ) {
295 if (
in_array(
'object', $flags ) ) {
296 $urlParts = explode(
'/', $row->header );
297 if ( $urlParts[0] !=
'DB:' ) {
300 "Error: unrecognised external storage type \"{$urlParts[0]}",
304 $cluster = $urlParts[2];
306 if ( !
isset( $externalConcatBlobs[$cluster][$id] ) ) {
307 $externalConcatBlobs[$cluster][$id] = [];
310 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
316 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
317 $concatBlobs[$row->old_id] );
320 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
325 "Error: Incorrect object header for concat bulk row {$row->old_id}",
326 $concatBlobs[$row->old_id]
330 unset( $concatBlobs[$row->old_id] );
339 print "\n\nErrors:\n";
340 foreach ( $this->errors as $name =>
$errors ) {
342 $description = $this->errorDescriptions[
$name];
347 if ( count( $this->errors[
'restore text'] ) && $fix ) {
348 if ( (
string)$xml !==
'' ) {
351 echo
"Can't fix text, no XML backup specified\n";
355 print "\nFlag statistics:\n";
357 foreach ( $flagStats as $flag => $count ) {
358 printf(
"%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
360 print "\nLocal object statistics:\n";
362 foreach ( $objectStats as $className => $count ) {
363 printf(
"%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );
391 if ( !count( $externalConcatBlobs ) ) {
395 if (
is_null( $this->dbStore ) ) {
399 foreach ( $externalConcatBlobs as $cluster => $oldIds ) {
401 $extDb =& $this->dbStore->getSlave( $cluster );
402 $blobsTable = $this->dbStore->getTable( $extDb );
403 $headerLength =
strlen( self::CONCAT_HEADER );
404 $res = $extDb->select( $blobsTable,
405 [
'blob_id',
"LEFT(blob_text, $headerLength) AS header" ],
406 [
'blob_id' => $blobIds ],
409 foreach (
$res as $row ) {
410 if (
strcasecmp( $row->header, self::CONCAT_HEADER ) ) {
413 "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL",
414 $oldIds[$row->blob_id]
417 unset( $oldIds[$row->blob_id] );
421 foreach ( $oldIds as $blobId => $oldIds2 ) {
424 "Error: missing target $cluster/$blobId for two-part ES URL",