72 public function check( $fix =
false, $xml =
'' ) {
75 print
"Checking, will fix errors if possible...\n";
77 print
"Checking...\n";
79 $maxRevId = $dbr->newSelectQueryBuilder()
80 ->select(
'MAX(rev_id)' )
82 ->caller( __METHOD__ )->fetchField();
86 $knownFlags = [
'external',
'gzip',
'object',
'utf-8' ];
89 'restore revision' => [],
95 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
96 $chunkEnd = $chunkStart + $chunkSize - 1;
103 $res = $dbr->newSelectQueryBuilder()
104 ->select( [
'slot_revision_id',
'content_address' ] )
106 ->join(
'content',
null,
'content_id = slot_content_id' )
108 $dbr->expr(
'slot_revision_id',
'>=', $chunkStart ),
109 $dbr->expr(
'slot_revision_id',
'<=', $chunkEnd ),
111 ->caller( __METHOD__ )->fetchResultSet();
114 '@phan-var \MediaWiki\Storage\SqlBlobStore $blobStore';
115 foreach ( $res as $row ) {
116 $textId = $blobStore->getTextIdFromAddress( $row->content_address );
118 if ( !isset( $this->oldIdMap[$textId] ) ) {
119 $this->oldIdMap[ $textId ] = [ $row->slot_revision_id ];
120 } elseif ( !in_array( $row->slot_revision_id, $this->oldIdMap[$textId] ) ) {
121 $this->oldIdMap[ $textId ][] = $row->slot_revision_id;
126 if ( !count( $this->oldIdMap ) ) {
134 $res = $dbr->newSelectQueryBuilder()
135 ->select( [
'old_id',
'old_flags' ] )
137 ->where( [
'old_id' => array_keys( $this->oldIdMap ) ] )
138 ->caller( __METHOD__ )->fetchResultSet();
139 foreach ( $res as $row ) {
143 $flags = $row->old_flags;
147 $flagStats += [ $flags => 0 ];
149 $flagStats[$flags]++;
152 unset( $missingTextRows[$row->old_id] );
155 if ( $flags ==
'' ) {
158 $flagArray = explode(
',', $flags );
160 if ( in_array(
'external', $flagArray ) ) {
161 $externalRevs[] = $id;
162 } elseif ( in_array(
'object', $flagArray ) ) {
167 if ( $flags ==
'0' ) {
171 $this->addError(
'fixed',
"Warning: old_flags set to 0", $id );
174 $dbw->newUpdateQueryBuilder()
176 ->set( [
'old_flags' =>
'' ] )
177 ->where( [
'old_id' => $id ] )
178 ->caller( __METHOD__ )
182 $this->addError(
'fixable',
"Warning: old_flags set to 0", $id );
184 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
185 $this->addError(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
190 foreach ( $missingTextRows as $oldId => $revIds ) {
191 $this->addError(
'restore revision',
"Error: missing text row", $oldId );
195 $externalConcatBlobs = [];
196 $externalNormalBlobs = [];
197 if ( count( $externalRevs ) ) {
198 $res = $dbr->newSelectQueryBuilder()
199 ->select( [
'old_id',
'old_flags',
'old_text' ] )
201 ->where( [
'old_id' => $externalRevs ] )
202 ->caller( __METHOD__ )->fetchResultSet();
203 foreach ( $res as $row ) {
204 $urlParts = explode(
'://', $row->old_text, 2 );
205 if ( count( $urlParts ) !== 2 || $urlParts[1] ==
'' ) {
206 $this->addError(
'restore text',
"Error: invalid URL \"{$row->old_text}\"", $row->old_id );
209 [ $proto, ] = $urlParts;
210 if ( $proto !=
'DB' ) {
213 "Error: invalid external protocol \"$proto\"",
217 $path = explode(
'/', $row->old_text );
220 if ( isset(
$path[4] ) ) {
221 $externalConcatBlobs[$cluster][$id][] = $row->old_id;
223 $externalNormalBlobs[$cluster][$id][] = $row->old_id;
229 $this->checkExternalConcatBlobs( $externalConcatBlobs );
232 if ( count( $externalNormalBlobs ) ) {
233 if ( $this->dbStore ===
null ) {
235 $this->dbStore = $esFactory->getStore(
'DB' );
237 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
238 $blobIds = array_keys( $xBlobIds );
239 $extDb = $this->dbStore->getReplica( $cluster );
240 $blobsTable = $this->dbStore->getTable( $cluster );
241 $res = $extDb->newSelectQueryBuilder()
242 ->select( [
'blob_id' ] )
243 ->from( $blobsTable )
244 ->where( [
'blob_id' => $blobIds ] )
245 ->caller( __METHOD__ )->fetchResultSet();
246 foreach ( $res as $row ) {
247 unset( $xBlobIds[$row->blob_id] );
250 foreach ( $xBlobIds as $blobId => $oldId ) {
253 "Error: missing target $blobId for one-part ES URL",
263 if ( count( $objectRevs ) ) {
265 $res = $dbr->newSelectQueryBuilder()
266 ->select( [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ] )
268 ->where( [
'old_id' => $objectRevs ] )
269 ->caller( __METHOD__ )->fetchResultSet();
270 foreach ( $res as $row ) {
271 $oldId = $row->old_id;
273 if ( !preg_match(
'/^O:(\d+):"(\w+)"/', $row->header,
$matches ) ) {
274 $this->addError(
'restore text',
"Error: invalid object header", $oldId );
278 $className = strtolower(
$matches[2] );
279 if ( strlen( $className ) !=
$matches[1] ) {
282 "Error: invalid object header, wrong class name length",
288 $objectStats += [ $className => 0 ];
289 $objectStats[$className]++;
291 switch ( $className ) {
292 case 'concatenatedgziphistoryblob':
295 case 'historyblobstub':
296 case 'historyblobcurstub':
297 if ( strlen( $row->header ) == $headerLength ) {
298 $this->addError(
'unfixable',
"Error: overlong stub header", $oldId );
301 $stubObj = unserialize( $row->header );
302 if ( !is_object( $stubObj ) ) {
303 $this->addError(
'restore text',
"Error: unable to unserialize stub object", $oldId );
306 if ( $className ==
'historyblobstub' ) {
307 $concatBlobs[$stubObj->getLocation()][] = $oldId;
309 $curIds[$stubObj->mCurId][] = $oldId;
313 $this->addError(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
319 $externalConcatBlobs = [];
320 if ( count( $concatBlobs ) ) {
322 $res = $dbr->newSelectQueryBuilder()
323 ->select( [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ] )
325 ->where( [
'old_id' => array_keys( $concatBlobs ) ] )
326 ->caller( __METHOD__ )->fetchResultSet();
327 foreach ( $res as $row ) {
328 $flags = explode(
',', $row->old_flags );
329 if ( in_array(
'external', $flags ) ) {
331 if ( in_array(
'object', $flags ) ) {
332 $urlParts = explode(
'/', $row->header );
333 if ( $urlParts[0] !=
'DB:' ) {
336 "Error: unrecognised external storage type \"{$urlParts[0]}",
340 $cluster = $urlParts[2];
342 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
343 $externalConcatBlobs[$cluster][$id] = [];
345 $externalConcatBlobs[$cluster][$id] = array_merge(
346 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
352 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
353 $concatBlobs[$row->old_id] );
355 } elseif ( strcasecmp(
356 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
361 "Error: Incorrect object header for concat bulk row {$row->old_id}",
362 $concatBlobs[$row->old_id]
366 unset( $concatBlobs[$row->old_id] );
371 $this->checkExternalConcatBlobs( $externalConcatBlobs );
375 print
"\n\nErrors:\n";
376 foreach ( $this->errors as $name =>
$errors ) {
378 $description = $this->errorDescriptions[$name];
379 echo
"$description: " . implode(
',', array_keys(
$errors ) ) .
"\n";
383 if ( count( $this->errors[
'restore text'] ) && $fix ) {
384 if ( (
string)$xml !==
'' ) {
385 $this->restoreText( array_keys( $this->errors[
'restore text'] ), $xml );
387 echo
"Can't fix text, no XML backup specified\n";
391 print
"\nFlag statistics:\n";
392 $total = array_sum( $flagStats );
393 foreach ( $flagStats as $flag => $count ) {
394 printf(
"%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
396 print
"\nLocal object statistics:\n";
397 $total = array_sum( $objectStats );
398 foreach ( $objectStats as $className => $count ) {
399 printf(
"%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );