65 public function check( $fix =
false, $xml =
'' ) {
68 print
"Checking, will fix errors if possible...\n";
70 print
"Checking...\n";
72 $maxRevId = $dbr->newSelectQueryBuilder()
73 ->select(
'MAX(rev_id)' )
75 ->caller( __METHOD__ )->fetchField();
79 $knownFlags = [
'external',
'gzip',
'object',
'utf-8' ];
82 'restore revision' => [],
88 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
89 $chunkEnd = $chunkStart + $chunkSize - 1;
96 $res = $dbr->newSelectQueryBuilder()
97 ->select( [
'slot_revision_id',
'content_address' ] )
99 ->join(
'content',
null,
'content_id = slot_content_id' )
100 ->where( [
"slot_revision_id BETWEEN $chunkStart AND $chunkEnd" ] )
101 ->caller( __METHOD__ )->fetchResultSet();
104 '@phan-var \MediaWiki\Storage\SqlBlobStore $blobStore';
105 foreach ( $res as $row ) {
106 $textId = $blobStore->getTextIdFromAddress( $row->content_address );
108 if ( !isset( $this->oldIdMap[$textId] ) ) {
109 $this->oldIdMap[ $textId ] = [ $row->slot_revision_id ];
110 } elseif ( !in_array( $row->slot_revision_id, $this->oldIdMap[$textId] ) ) {
111 $this->oldIdMap[ $textId ][] = $row->slot_revision_id;
116 if ( !count( $this->oldIdMap ) ) {
124 $res = $dbr->newSelectQueryBuilder()
125 ->select( [
'old_id',
'old_flags' ] )
127 ->where( [
'old_id' => array_keys( $this->oldIdMap ) ] )
128 ->caller( __METHOD__ )->fetchResultSet();
129 foreach ( $res as $row ) {
133 $flags = $row->old_flags;
137 $flagStats += [ $flags => 0 ];
139 $flagStats[$flags]++;
142 unset( $missingTextRows[$row->old_id] );
145 if ( $flags ==
'' ) {
148 $flagArray = explode(
',', $flags );
150 if ( in_array(
'external', $flagArray ) ) {
151 $externalRevs[] = $id;
152 } elseif ( in_array(
'object', $flagArray ) ) {
157 if ( $flags ==
'0' ) {
161 $this->addError(
'fixed',
"Warning: old_flags set to 0", $id );
164 $dbw->update(
'text', [
'old_flags' =>
'' ],
165 [
'old_id' => $id ], __METHOD__ );
168 $this->addError(
'fixable',
"Warning: old_flags set to 0", $id );
170 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
171 $this->addError(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
176 foreach ( $missingTextRows as $oldId => $revIds ) {
177 $this->addError(
'restore revision',
"Error: missing text row", $oldId );
181 $externalConcatBlobs = [];
182 $externalNormalBlobs = [];
183 if ( count( $externalRevs ) ) {
184 $res = $dbr->newSelectQueryBuilder()
185 ->select( [
'old_id',
'old_flags',
'old_text' ] )
187 ->where( [
'old_id' => $externalRevs ] )
188 ->caller( __METHOD__ )->fetchResultSet();
189 foreach ( $res as $row ) {
190 $urlParts = explode(
'://', $row->old_text, 2 );
191 if ( count( $urlParts ) !== 2 || $urlParts[1] ==
'' ) {
192 $this->addError(
'restore text',
"Error: invalid URL \"{$row->old_text}\"", $row->old_id );
195 [ $proto, ] = $urlParts;
196 if ( $proto !=
'DB' ) {
199 "Error: invalid external protocol \"$proto\"",
203 $path = explode(
'/', $row->old_text );
206 if ( isset(
$path[4] ) ) {
207 $externalConcatBlobs[$cluster][$id][] = $row->old_id;
209 $externalNormalBlobs[$cluster][$id][] = $row->old_id;
215 $this->checkExternalConcatBlobs( $externalConcatBlobs );
218 if ( count( $externalNormalBlobs ) ) {
219 if ( $this->dbStore ===
null ) {
221 $this->dbStore = $esFactory->getStore(
'DB' );
223 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
224 $blobIds = array_keys( $xBlobIds );
225 $extDb = $this->dbStore->getReplica( $cluster );
226 $blobsTable = $this->dbStore->getTable( $extDb );
227 $res = $extDb->newSelectQueryBuilder()
228 ->select( [
'blob_id' ] )
229 ->from( $blobsTable )
230 ->where( [
'blob_id' => $blobIds ] )
231 ->caller( __METHOD__ )->fetchResultSet();
232 foreach ( $res as $row ) {
233 unset( $xBlobIds[$row->blob_id] );
236 foreach ( $xBlobIds as $blobId => $oldId ) {
239 "Error: missing target $blobId for one-part ES URL",
249 if ( count( $objectRevs ) ) {
251 $res = $dbr->newSelectQueryBuilder()
252 ->select( [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ] )
254 ->where( [
'old_id' => $objectRevs ] )
255 ->caller( __METHOD__ )->fetchResultSet();
256 foreach ( $res as $row ) {
257 $oldId = $row->old_id;
259 if ( !preg_match(
'/^O:(\d+):"(\w+)"/', $row->header,
$matches ) ) {
260 $this->addError(
'restore text',
"Error: invalid object header", $oldId );
264 $className = strtolower(
$matches[2] );
265 if ( strlen( $className ) !=
$matches[1] ) {
268 "Error: invalid object header, wrong class name length",
274 $objectStats += [ $className => 0 ];
275 $objectStats[$className]++;
277 switch ( $className ) {
278 case 'concatenatedgziphistoryblob':
281 case 'historyblobstub':
282 case 'historyblobcurstub':
283 if ( strlen( $row->header ) == $headerLength ) {
284 $this->addError(
'unfixable',
"Error: overlong stub header", $oldId );
287 $stubObj = unserialize( $row->header );
288 if ( !is_object( $stubObj ) ) {
289 $this->addError(
'restore text',
"Error: unable to unserialize stub object", $oldId );
292 if ( $className ==
'historyblobstub' ) {
293 $concatBlobs[$stubObj->getLocation()][] = $oldId;
295 $curIds[$stubObj->mCurId][] = $oldId;
299 $this->addError(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
305 $externalConcatBlobs = [];
306 if ( count( $concatBlobs ) ) {
308 $res = $dbr->newSelectQueryBuilder()
309 ->select( [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ] )
311 ->where( [
'old_id' => array_keys( $concatBlobs ) ] )
312 ->caller( __METHOD__ )->fetchResultSet();
313 foreach ( $res as $row ) {
314 $flags = explode(
',', $row->old_flags );
315 if ( in_array(
'external', $flags ) ) {
317 if ( in_array(
'object', $flags ) ) {
318 $urlParts = explode(
'/', $row->header );
319 if ( $urlParts[0] !=
'DB:' ) {
322 "Error: unrecognised external storage type \"{$urlParts[0]}",
326 $cluster = $urlParts[2];
328 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
329 $externalConcatBlobs[$cluster][$id] = [];
331 $externalConcatBlobs[$cluster][$id] = array_merge(
332 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
338 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
339 $concatBlobs[$row->old_id] );
341 } elseif ( strcasecmp(
342 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
347 "Error: Incorrect object header for concat bulk row {$row->old_id}",
348 $concatBlobs[$row->old_id]
352 unset( $concatBlobs[$row->old_id] );
357 $this->checkExternalConcatBlobs( $externalConcatBlobs );
361 print
"\n\nErrors:\n";
362 foreach ( $this->errors as $name =>
$errors ) {
364 $description = $this->errorDescriptions[$name];
365 echo
"$description: " . implode(
',', array_keys(
$errors ) ) .
"\n";
369 if ( count( $this->errors[
'restore text'] ) && $fix ) {
370 if ( (
string)$xml !==
'' ) {
371 $this->restoreText( array_keys( $this->errors[
'restore text'] ), $xml );
373 echo
"Can't fix text, no XML backup specified\n";
377 print
"\nFlag statistics:\n";
378 $total = array_sum( $flagStats );
379 foreach ( $flagStats as $flag => $count ) {
380 printf(
"%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
382 print
"\nLocal object statistics:\n";
383 $total = array_sum( $objectStats );
384 foreach ( $objectStats as $className => $count ) {
385 printf(
"%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );