66 public function check( $fix =
false, $xml =
'' ) {
69 print
"Checking, will fix errors if possible...\n";
71 print
"Checking...\n";
73 $maxRevId = $dbr->newSelectQueryBuilder()
74 ->select(
'MAX(rev_id)' )
76 ->caller( __METHOD__ )->fetchField();
80 $knownFlags = [
'external',
'gzip',
'object',
'utf-8' ];
83 'restore revision' => [],
89 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
90 $chunkEnd = $chunkStart + $chunkSize - 1;
97 $res = $dbr->newSelectQueryBuilder()
98 ->select( [
'slot_revision_id',
'content_address' ] )
100 ->join(
'content',
null,
'content_id = slot_content_id' )
101 ->where( [
"slot_revision_id BETWEEN $chunkStart AND $chunkEnd" ] )
102 ->caller( __METHOD__ )->fetchResultSet();
105 '@phan-var \MediaWiki\Storage\SqlBlobStore $blobStore';
106 foreach ( $res as $row ) {
107 $textId = $blobStore->getTextIdFromAddress( $row->content_address );
109 if ( !isset( $this->oldIdMap[$textId] ) ) {
110 $this->oldIdMap[ $textId ] = [ $row->slot_revision_id ];
111 } elseif ( !in_array( $row->slot_revision_id, $this->oldIdMap[$textId] ) ) {
112 $this->oldIdMap[ $textId ][] = $row->slot_revision_id;
117 if ( !count( $this->oldIdMap ) ) {
125 $res = $dbr->newSelectQueryBuilder()
126 ->select( [
'old_id',
'old_flags' ] )
128 ->where( [
'old_id' => array_keys( $this->oldIdMap ) ] )
129 ->caller( __METHOD__ )->fetchResultSet();
130 foreach ( $res as $row ) {
134 $flags = $row->old_flags;
138 $flagStats += [ $flags => 0 ];
140 $flagStats[$flags]++;
143 unset( $missingTextRows[$row->old_id] );
146 if ( $flags ==
'' ) {
149 $flagArray = explode(
',', $flags );
151 if ( in_array(
'external', $flagArray ) ) {
152 $externalRevs[] = $id;
153 } elseif ( in_array(
'object', $flagArray ) ) {
158 if ( $flags ==
'0' ) {
162 $this->addError(
'fixed',
"Warning: old_flags set to 0", $id );
165 $dbw->update(
'text', [
'old_flags' =>
'' ],
166 [
'old_id' => $id ], __METHOD__ );
169 $this->addError(
'fixable',
"Warning: old_flags set to 0", $id );
171 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
172 $this->addError(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
177 foreach ( $missingTextRows as $oldId => $revIds ) {
178 $this->addError(
'restore revision',
"Error: missing text row", $oldId );
182 $externalConcatBlobs = [];
183 $externalNormalBlobs = [];
184 if ( count( $externalRevs ) ) {
185 $res = $dbr->newSelectQueryBuilder()
186 ->select( [
'old_id',
'old_flags',
'old_text' ] )
188 ->where( [
'old_id' => $externalRevs ] )
189 ->caller( __METHOD__ )->fetchResultSet();
190 foreach ( $res as $row ) {
191 $urlParts = explode(
'://', $row->old_text, 2 );
192 if ( count( $urlParts ) !== 2 || $urlParts[1] ==
'' ) {
193 $this->addError(
'restore text',
"Error: invalid URL \"{$row->old_text}\"", $row->old_id );
196 [ $proto, ] = $urlParts;
197 if ( $proto !=
'DB' ) {
200 "Error: invalid external protocol \"$proto\"",
204 $path = explode(
'/', $row->old_text );
207 if ( isset(
$path[4] ) ) {
208 $externalConcatBlobs[$cluster][$id][] = $row->old_id;
210 $externalNormalBlobs[$cluster][$id][] = $row->old_id;
216 $this->checkExternalConcatBlobs( $externalConcatBlobs );
219 if ( count( $externalNormalBlobs ) ) {
220 if ( $this->dbStore ===
null ) {
222 $this->dbStore = $esFactory->getStore(
'DB' );
224 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
225 $blobIds = array_keys( $xBlobIds );
226 $extDb = $this->dbStore->getReplica( $cluster );
227 $blobsTable = $this->dbStore->getTable( $extDb );
228 $res = $extDb->newSelectQueryBuilder()
229 ->select( [
'blob_id' ] )
230 ->from( $blobsTable )
231 ->where( [
'blob_id' => $blobIds ] )
232 ->caller( __METHOD__ )->fetchResultSet();
233 foreach ( $res as $row ) {
234 unset( $xBlobIds[$row->blob_id] );
237 foreach ( $xBlobIds as $blobId => $oldId ) {
240 "Error: missing target $blobId for one-part ES URL",
250 if ( count( $objectRevs ) ) {
252 $res = $dbr->newSelectQueryBuilder()
253 ->select( [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ] )
255 ->where( [
'old_id' => $objectRevs ] )
256 ->caller( __METHOD__ )->fetchResultSet();
257 foreach ( $res as $row ) {
258 $oldId = $row->old_id;
260 if ( !preg_match(
'/^O:(\d+):"(\w+)"/', $row->header,
$matches ) ) {
261 $this->addError(
'restore text',
"Error: invalid object header", $oldId );
265 $className = strtolower(
$matches[2] );
266 if ( strlen( $className ) !=
$matches[1] ) {
269 "Error: invalid object header, wrong class name length",
275 $objectStats += [ $className => 0 ];
276 $objectStats[$className]++;
278 switch ( $className ) {
279 case 'concatenatedgziphistoryblob':
282 case 'historyblobstub':
283 case 'historyblobcurstub':
284 if ( strlen( $row->header ) == $headerLength ) {
285 $this->addError(
'unfixable',
"Error: overlong stub header", $oldId );
288 $stubObj = unserialize( $row->header );
289 if ( !is_object( $stubObj ) ) {
290 $this->addError(
'restore text',
"Error: unable to unserialize stub object", $oldId );
293 if ( $className ==
'historyblobstub' ) {
294 $concatBlobs[$stubObj->getLocation()][] = $oldId;
296 $curIds[$stubObj->mCurId][] = $oldId;
300 $this->addError(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
306 $externalConcatBlobs = [];
307 if ( count( $concatBlobs ) ) {
309 $res = $dbr->newSelectQueryBuilder()
310 ->select( [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ] )
312 ->where( [
'old_id' => array_keys( $concatBlobs ) ] )
313 ->caller( __METHOD__ )->fetchResultSet();
314 foreach ( $res as $row ) {
315 $flags = explode(
',', $row->old_flags );
316 if ( in_array(
'external', $flags ) ) {
318 if ( in_array(
'object', $flags ) ) {
319 $urlParts = explode(
'/', $row->header );
320 if ( $urlParts[0] !=
'DB:' ) {
323 "Error: unrecognised external storage type \"{$urlParts[0]}",
327 $cluster = $urlParts[2];
329 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
330 $externalConcatBlobs[$cluster][$id] = [];
332 $externalConcatBlobs[$cluster][$id] = array_merge(
333 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
339 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
340 $concatBlobs[$row->old_id] );
342 } elseif ( strcasecmp(
343 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
348 "Error: Incorrect object header for concat bulk row {$row->old_id}",
349 $concatBlobs[$row->old_id]
353 unset( $concatBlobs[$row->old_id] );
358 $this->checkExternalConcatBlobs( $externalConcatBlobs );
362 print
"\n\nErrors:\n";
363 foreach ( $this->errors as $name =>
$errors ) {
365 $description = $this->errorDescriptions[$name];
366 echo
"$description: " . implode(
',', array_keys(
$errors ) ) .
"\n";
370 if ( count( $this->errors[
'restore text'] ) && $fix ) {
371 if ( (
string)$xml !==
'' ) {
372 $this->restoreText( array_keys( $this->errors[
'restore text'] ), $xml );
374 echo
"Can't fix text, no XML backup specified\n";
378 print
"\nFlag statistics:\n";
379 $total = array_sum( $flagStats );
380 foreach ( $flagStats as $flag => $count ) {
381 printf(
"%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
383 print
"\nLocal object statistics:\n";
384 $total = array_sum( $objectStats );
385 foreach ( $objectStats as $className => $count ) {
386 printf(
"%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );