68 public function check( $fix =
false, $xml =
'' ) {
71 print
"Checking, will fix errors if possible...\n";
73 print
"Checking...\n";
75 $maxRevId = $dbr->newSelectQueryBuilder()
76 ->select(
'MAX(rev_id)' )
78 ->caller( __METHOD__ )->fetchField();
82 $knownFlags = [
'external',
'gzip',
'object',
'utf-8' ];
85 'restore revision' => [],
91 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
92 $chunkEnd = $chunkStart + $chunkSize - 1;
99 $res = $dbr->newSelectQueryBuilder()
100 ->select( [
'slot_revision_id',
'content_address' ] )
102 ->join(
'content',
null,
'content_id = slot_content_id' )
104 $dbr->expr(
'slot_revision_id',
'>=', $chunkStart ),
105 $dbr->expr(
'slot_revision_id',
'<=', $chunkEnd ),
107 ->caller( __METHOD__ )->fetchResultSet();
110 '@phan-var \MediaWiki\Storage\SqlBlobStore $blobStore';
111 foreach ( $res as $row ) {
112 $textId = $blobStore->getTextIdFromAddress( $row->content_address );
114 if ( !isset( $this->oldIdMap[$textId] ) ) {
115 $this->oldIdMap[ $textId ] = [ $row->slot_revision_id ];
116 } elseif ( !in_array( $row->slot_revision_id, $this->oldIdMap[$textId] ) ) {
117 $this->oldIdMap[ $textId ][] = $row->slot_revision_id;
122 if ( !count( $this->oldIdMap ) ) {
130 $res = $dbr->newSelectQueryBuilder()
131 ->select( [
'old_id',
'old_flags' ] )
133 ->where( [
'old_id' => array_keys( $this->oldIdMap ) ] )
134 ->caller( __METHOD__ )->fetchResultSet();
135 foreach ( $res as $row ) {
139 $flags = $row->old_flags;
143 $flagStats += [ $flags => 0 ];
145 $flagStats[$flags]++;
148 unset( $missingTextRows[$row->old_id] );
151 if ( $flags ==
'' ) {
154 $flagArray = explode(
',', $flags );
156 if ( in_array(
'external', $flagArray ) ) {
157 $externalRevs[] = $id;
158 } elseif ( in_array(
'object', $flagArray ) ) {
163 if ( $flags ==
'0' ) {
167 $this->addError(
'fixed',
"Warning: old_flags set to 0", $id );
170 $dbw->newUpdateQueryBuilder()
172 ->set( [
'old_flags' =>
'' ] )
173 ->where( [
'old_id' => $id ] )
174 ->caller( __METHOD__ )
178 $this->addError(
'fixable',
"Warning: old_flags set to 0", $id );
180 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
181 $this->addError(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
186 foreach ( $missingTextRows as $oldId => $revIds ) {
187 $this->addError(
'restore revision',
"Error: missing text row", $oldId );
191 $externalConcatBlobs = [];
192 $externalNormalBlobs = [];
193 if ( count( $externalRevs ) ) {
194 $res = $dbr->newSelectQueryBuilder()
195 ->select( [
'old_id',
'old_flags',
'old_text' ] )
197 ->where( [
'old_id' => $externalRevs ] )
198 ->caller( __METHOD__ )->fetchResultSet();
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 [ $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;
225 $this->checkExternalConcatBlobs( $externalConcatBlobs );
228 if ( count( $externalNormalBlobs ) ) {
229 if ( $this->dbStore ===
null ) {
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->newSelectQueryBuilder()
238 ->select( [
'blob_id' ] )
239 ->from( $blobsTable )
240 ->where( [
'blob_id' => $blobIds ] )
241 ->caller( __METHOD__ )->fetchResultSet();
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 ) ) {
261 $res = $dbr->newSelectQueryBuilder()
262 ->select( [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ] )
264 ->where( [
'old_id' => $objectRevs ] )
265 ->caller( __METHOD__ )->fetchResultSet();
266 foreach ( $res as $row ) {
267 $oldId = $row->old_id;
269 if ( !preg_match(
'/^O:(\d+):"(\w+)"/', $row->header,
$matches ) ) {
270 $this->addError(
'restore text',
"Error: invalid object header", $oldId );
274 $className = strtolower(
$matches[2] );
275 if ( strlen( $className ) !=
$matches[1] ) {
278 "Error: invalid object header, wrong class name length",
284 $objectStats += [ $className => 0 ];
285 $objectStats[$className]++;
287 switch ( $className ) {
288 case 'concatenatedgziphistoryblob':
291 case 'historyblobstub':
292 case 'historyblobcurstub':
293 if ( strlen( $row->header ) == $headerLength ) {
294 $this->addError(
'unfixable',
"Error: overlong stub header", $oldId );
297 $stubObj = unserialize( $row->header );
298 if ( !is_object( $stubObj ) ) {
299 $this->addError(
'restore text',
"Error: unable to unserialize stub object", $oldId );
302 if ( $className ==
'historyblobstub' ) {
303 $concatBlobs[$stubObj->getLocation()][] = $oldId;
305 $curIds[$stubObj->mCurId][] = $oldId;
309 $this->addError(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
315 $externalConcatBlobs = [];
316 if ( count( $concatBlobs ) ) {
318 $res = $dbr->newSelectQueryBuilder()
319 ->select( [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ] )
321 ->where( [
'old_id' => array_keys( $concatBlobs ) ] )
322 ->caller( __METHOD__ )->fetchResultSet();
323 foreach ( $res as $row ) {
324 $flags = explode(
',', $row->old_flags );
325 if ( in_array(
'external', $flags ) ) {
327 if ( in_array(
'object', $flags ) ) {
328 $urlParts = explode(
'/', $row->header );
329 if ( $urlParts[0] !=
'DB:' ) {
332 "Error: unrecognised external storage type \"{$urlParts[0]}",
336 $cluster = $urlParts[2];
338 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
339 $externalConcatBlobs[$cluster][$id] = [];
341 $externalConcatBlobs[$cluster][$id] = array_merge(
342 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
348 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
349 $concatBlobs[$row->old_id] );
351 } elseif ( strcasecmp(
352 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
357 "Error: Incorrect object header for concat bulk row {$row->old_id}",
358 $concatBlobs[$row->old_id]
362 unset( $concatBlobs[$row->old_id] );
367 $this->checkExternalConcatBlobs( $externalConcatBlobs );
371 print
"\n\nErrors:\n";
372 foreach ( $this->errors as $name =>
$errors ) {
374 $description = $this->errorDescriptions[$name];
375 echo
"$description: " . implode(
',', array_keys(
$errors ) ) .
"\n";
379 if ( count( $this->errors[
'restore text'] ) && $fix ) {
380 if ( (
string)$xml !==
'' ) {
381 $this->restoreText( array_keys( $this->errors[
'restore text'] ), $xml );
383 echo
"Can't fix text, no XML backup specified\n";
387 print
"\nFlag statistics:\n";
388 $total = array_sum( $flagStats );
389 foreach ( $flagStats as $flag => $count ) {
390 printf(
"%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
392 print
"\nLocal object statistics:\n";
393 $total = array_sum( $objectStats );
394 foreach ( $objectStats as $className => $count ) {
395 printf(
"%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );