71 public function check( $fix =
false, $xml =
'' ) {
72 $dbr = $this->getReplicaDB();
74 print
"Checking, will fix errors if possible...\n";
76 print
"Checking...\n";
78 $maxRevId = $dbr->newSelectQueryBuilder()
79 ->select(
'MAX(rev_id)' )
81 ->caller( __METHOD__ )->fetchField();
85 $knownFlags = [
'external',
'gzip',
'object',
'utf-8' ];
88 'restore revision' => [],
94 for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
95 $chunkEnd = $chunkStart + $chunkSize - 1;
102 $res = $dbr->newSelectQueryBuilder()
103 ->select( [
'slot_revision_id',
'content_address' ] )
105 ->join(
'content',
null,
'content_id = slot_content_id' )
107 $dbr->expr(
'slot_revision_id',
'>=', $chunkStart ),
108 $dbr->expr(
'slot_revision_id',
'<=', $chunkEnd ),
110 ->caller( __METHOD__ )->fetchResultSet();
112 $blobStore = $this->getServiceContainer()->getBlobStore();
113 '@phan-var \MediaWiki\Storage\SqlBlobStore $blobStore';
114 foreach ( $res as $row ) {
115 $textId = $blobStore->getTextIdFromAddress( $row->content_address );
117 if ( !isset( $this->oldIdMap[$textId] ) ) {
118 $this->oldIdMap[ $textId ] = [ $row->slot_revision_id ];
119 } elseif ( !in_array( $row->slot_revision_id, $this->oldIdMap[$textId] ) ) {
120 $this->oldIdMap[ $textId ][] = $row->slot_revision_id;
125 if ( !count( $this->oldIdMap ) ) {
133 $res = $dbr->newSelectQueryBuilder()
134 ->select( [
'old_id',
'old_flags' ] )
136 ->where( [
'old_id' => array_keys( $this->oldIdMap ) ] )
137 ->caller( __METHOD__ )->fetchResultSet();
138 foreach ( $res as $row ) {
142 $flags = $row->old_flags;
146 $flagStats += [ $flags => 0 ];
148 $flagStats[$flags]++;
151 unset( $missingTextRows[$row->old_id] );
154 if ( $flags ==
'' ) {
157 $flagArray = explode(
',', $flags );
159 if ( in_array(
'external', $flagArray ) ) {
160 $externalRevs[] = $id;
161 } elseif ( in_array(
'object', $flagArray ) ) {
166 if ( $flags ==
'0' ) {
170 $this->addError(
'fixed',
"Warning: old_flags set to 0", $id );
171 $dbw = $this->getPrimaryDB();
173 $dbw->newUpdateQueryBuilder()
175 ->set( [
'old_flags' =>
'' ] )
176 ->where( [
'old_id' => $id ] )
177 ->caller( __METHOD__ )
181 $this->addError(
'fixable',
"Warning: old_flags set to 0", $id );
183 } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
184 $this->addError(
'unfixable',
"Error: invalid flags field \"$flags\"", $id );
189 foreach ( $missingTextRows as $oldId => $revIds ) {
190 $this->addError(
'restore revision',
"Error: missing text row", $oldId );
194 $externalConcatBlobs = [];
195 $externalNormalBlobs = [];
196 if ( count( $externalRevs ) ) {
197 $res = $dbr->newSelectQueryBuilder()
198 ->select( [
'old_id',
'old_flags',
'old_text' ] )
200 ->where( [
'old_id' => $externalRevs ] )
201 ->caller( __METHOD__ )->fetchResultSet();
202 foreach ( $res as $row ) {
203 $urlParts = explode(
'://', $row->old_text, 2 );
204 if ( count( $urlParts ) !== 2 || $urlParts[1] ==
'' ) {
205 $this->addError(
'restore text',
"Error: invalid URL \"{$row->old_text}\"", $row->old_id );
208 [ $proto, ] = $urlParts;
209 if ( $proto !=
'DB' ) {
212 "Error: invalid external protocol \"$proto\"",
216 $path = explode(
'/', $row->old_text );
219 if ( isset(
$path[4] ) ) {
220 $externalConcatBlobs[$cluster][$id][] = $row->old_id;
222 $externalNormalBlobs[$cluster][$id][] = $row->old_id;
228 $this->checkExternalConcatBlobs( $externalConcatBlobs );
231 if ( count( $externalNormalBlobs ) ) {
232 if ( $this->dbStore ===
null ) {
233 $esFactory = $this->getServiceContainer()->getExternalStoreFactory();
234 $this->dbStore = $esFactory->getStore(
'DB' );
236 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
237 $blobIds = array_keys( $xBlobIds );
238 $extDb = $this->dbStore->getReplica( $cluster );
239 $blobsTable = $this->dbStore->getTable( $cluster );
240 $res = $extDb->newSelectQueryBuilder()
241 ->select( [
'blob_id' ] )
242 ->from( $blobsTable )
243 ->where( [
'blob_id' => $blobIds ] )
244 ->caller( __METHOD__ )->fetchResultSet();
245 foreach ( $res as $row ) {
246 unset( $xBlobIds[$row->blob_id] );
249 foreach ( $xBlobIds as $blobId => $oldId ) {
252 "Error: missing target $blobId for one-part ES URL",
262 if ( count( $objectRevs ) ) {
264 $res = $dbr->newSelectQueryBuilder()
265 ->select( [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ] )
267 ->where( [
'old_id' => $objectRevs ] )
268 ->caller( __METHOD__ )->fetchResultSet();
269 foreach ( $res as $row ) {
270 $oldId = $row->old_id;
272 if ( !preg_match(
'/^O:(\d+):"(\w+)"/', $row->header,
$matches ) ) {
273 $this->addError(
'restore text',
"Error: invalid object header", $oldId );
277 $className = strtolower(
$matches[2] );
278 if ( strlen( $className ) !=
$matches[1] ) {
281 "Error: invalid object header, wrong class name length",
287 $objectStats += [ $className => 0 ];
288 $objectStats[$className]++;
290 switch ( $className ) {
291 case 'concatenatedgziphistoryblob':
294 case 'historyblobstub':
295 case 'historyblobcurstub':
296 if ( strlen( $row->header ) == $headerLength ) {
297 $this->addError(
'unfixable',
"Error: overlong stub header", $oldId );
300 $stubObj = unserialize( $row->header );
301 if ( !is_object( $stubObj ) ) {
302 $this->addError(
'restore text',
"Error: unable to unserialize stub object", $oldId );
305 if ( $className ==
'historyblobstub' ) {
306 $concatBlobs[$stubObj->getLocation()][] = $oldId;
308 $curIds[$stubObj->mCurId][] = $oldId;
312 $this->addError(
'unfixable',
"Error: unrecognised object class \"$className\"", $oldId );
318 $externalConcatBlobs = [];
319 if ( count( $concatBlobs ) ) {
321 $res = $dbr->newSelectQueryBuilder()
322 ->select( [
'old_id',
'old_flags',
"LEFT(old_text, $headerLength) AS header" ] )
324 ->where( [
'old_id' => array_keys( $concatBlobs ) ] )
325 ->caller( __METHOD__ )->fetchResultSet();
326 foreach ( $res as $row ) {
327 $flags = explode(
',', $row->old_flags );
328 if ( in_array(
'external', $flags ) ) {
330 if ( in_array(
'object', $flags ) ) {
331 $urlParts = explode(
'/', $row->header );
332 if ( $urlParts[0] !=
'DB:' ) {
335 "Error: unrecognised external storage type \"{$urlParts[0]}",
339 $cluster = $urlParts[2];
341 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
342 $externalConcatBlobs[$cluster][$id] = [];
344 $externalConcatBlobs[$cluster][$id] = array_merge(
345 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
351 "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
352 $concatBlobs[$row->old_id] );
354 } elseif ( strcasecmp(
355 substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
360 "Error: Incorrect object header for concat bulk row {$row->old_id}",
361 $concatBlobs[$row->old_id]
365 unset( $concatBlobs[$row->old_id] );
370 $this->checkExternalConcatBlobs( $externalConcatBlobs );
374 print
"\n\nErrors:\n";
375 foreach ( $this->errors as $name =>
$errors ) {
377 $description = $this->errorDescriptions[$name];
378 echo
"$description: " . implode(
',', array_keys(
$errors ) ) .
"\n";
382 if ( count( $this->errors[
'restore text'] ) && $fix ) {
383 if ( (
string)$xml !==
'' ) {
384 $this->restoreText( array_keys( $this->errors[
'restore text'] ), $xml );
386 echo
"Can't fix text, no XML backup specified\n";
390 print
"\nFlag statistics:\n";
391 $total = array_sum( $flagStats );
392 foreach ( $flagStats as $flag => $count ) {
393 printf(
"%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
395 print
"\nLocal object statistics:\n";
396 $total = array_sum( $objectStats );
397 foreach ( $objectStats as $className => $count ) {
398 printf(
"%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );