31 parent::__construct();
34 $this->
addDescription(
'Find and mark bad content blobs. Marked blobs will be read as empty. '
35 .
'Use --scan-from to find revisions with bad blobs, use --mark to mark them.' );
36 $this->
addOption(
'scan-from',
'Start scanning revisions at the given date. '
37 .
'Format: Anything supported by MediaWiki, e.g. YYYYMMDDHHMMSS or YYYY-MM-DDTHH:MM:SS',
39 $this->
addOption(
'scan-to',
'End of scan date range. '
40 .
'Format: Anything supported by MediaWiki, e.g. YYYYMMDDHHMMSS or YYYY-MM-DDTHH:MM:SS',
42 $this->
addOption(
'revisions',
'A list of revision IDs to process, separated by comma or '
43 .
'colon or whitespace. Revisions belonging to deleted pages will work. '
44 .
'If set to "-" IDs are read from stdin, one per line.',
false,
true );
45 $this->
addOption(
'limit',
'Maximum number of revisions for --scan-from to scan. '
46 .
'Default: 1000',
false,
true );
47 $this->
addOption(
'mark',
'Mark the blob as "known bad", to avoid errors when '
48 .
'attempting to read it. The value given is the reason for marking the blob as bad, '
49 .
'typically a ticket ID. Requires --revisions to also be set.',
false,
true );
55 private function getStartTimestamp() {
57 if ( strlen( $tsOpt ) < 14 ) {
59 .
', please provide time and date down to the second.' );
64 $this->
fatalError(
'Bad timestamp: ' . $tsOpt );
70 private function getEndTimestamp(): string {
72 if ( strlen( $tsOpt ) < 14 ) {
74 .
', please provide time and date down to the second.' );
79 $this->
fatalError(
'Bad timestamp: ' . $tsOpt );
88 private function getRevisionIds() {
89 $opt = $this->getOption(
'revisions' );
92 $opt = stream_get_contents( STDIN );
99 return $this->parseIntList( $opt );
106 $services = $this->getServiceContainer();
107 $this->revisionStore = $services->getRevisionStore();
108 $this->blobStore = $services->getBlobStore();
110 if ( $this->hasOption(
'revisions' ) ) {
111 if ( $this->hasOption(
'scan-from' ) || $this->hasOption(
'scan-to' ) ) {
112 $this->fatalError(
'Cannot use --revisions together with --scan-from or --scan-to' );
115 $ids = $this->getRevisionIds();
117 $count = $this->scanRevisionsById( $ids );
118 } elseif ( $this->hasOption(
'scan-from' ) ) {
119 if ( $this->hasOption(
'mark' ) ) {
120 $this->fatalError(
'Cannot use --mark with --scan-from, '
121 .
'use --revisions to specify revisions to mark.' );
124 if ( $this->hasOption(
'scan-to' ) && $this->hasOption(
'limit' ) ) {
125 $this->fatalError(
'Cannot use --limit with --scan-to' );
128 $count = $this->scanRevisionsByTimestamp();
129 $this->output(
"The range of archive rows scanned is based on the range of revision IDs "
130 .
"scanned in the revision table.\n" );
132 if ( $this->hasOption(
'mark' ) ) {
133 $this->fatalError(
'The --mark must be used together with --revisions' );
135 $this->fatalError(
'Must specify one of --revisions or --scan-from' );
139 if ( $this->hasOption(
'mark' ) ) {
140 $this->output(
"Marked $count bad revisions.\n" );
142 $this->output(
"Found $count bad revisions.\n" );
145 $this->output(
"On a unix/linux environment, you can use grep and cut to list of IDs\n" );
146 $this->output(
"that can then be used with the --revisions option. E.g.\n" );
147 $this->output(
" grep '! Found bad blob' | cut -s -f 3\n" );
155 private function scanRevisionsByTimestamp() {
156 $fromTimestamp = $this->getStartTimestamp();
157 if ( $this->getOption(
'scan-to' ) ) {
158 $toTimestamp = $this->getEndTimestamp();
163 $total = $this->getOption(
'limit', 1000 );
167 $lastTimestamp = $fromTimestamp;
168 $revisionRowsScanned = 0;
169 $archiveRowsScanned = 0;
171 $this->output(
"Scanning revisions table, "
172 .
"$total rows starting at rev_timestamp $fromTimestamp\n" );
174 while ( $toTimestamp ===
null ? $revisionRowsScanned < $total : true ) {
175 $batchSize = min( $total - $revisionRowsScanned, $this->getBatchSize() );
176 $revisions = $this->loadRevisionsByTimestamp( $lastRevId, $lastTimestamp, $batchSize, $toTimestamp );
181 foreach ( $revisions as $rev ) {
183 $firstRevId = $firstRevId ? min( $firstRevId, $rev->getId() ) : $rev->getId();
184 $lastRevId = max( $lastRevId, $rev->getId() );
186 $count += $this->checkRevision( $rev );
189 $lastTimestamp = $rev->getTimestamp();
190 $batchSize = count( $revisions );
191 $revisionRowsScanned += $batchSize;
193 "\t- Scanned a batch of $batchSize revisions, "
194 .
"up to revision $lastRevId ($lastTimestamp)\n"
197 $this->waitForReplication();
205 $fromArchived = $this->getNextRevision( $firstRevId,
'<',
'DESC' );
206 $maxArchived = $this->getNextRevision( $lastRevId,
'>',
'ASC' );
207 $maxArchived = $maxArchived ?: PHP_INT_MAX;
209 $this->output(
"Scanning archive table by ar_rev_id, $fromArchived to $maxArchived\n" );
210 while ( $firstRevId > 0 && $fromArchived < $maxArchived ) {
211 $batchSize = min( $total - $archiveRowsScanned, $this->getBatchSize() );
212 $revisions = $this->loadArchiveByRevisionId( $fromArchived, $maxArchived, $batchSize );
217 foreach ( $revisions as $rev ) {
218 $count += $this->checkRevision( $rev );
220 $fromArchived = $rev->getId();
221 $batchSize = count( $revisions );
222 $archiveRowsScanned += $batchSize;
224 "\t- Scanned a batch of $batchSize archived revisions, "
225 .
"up to revision $fromArchived ($lastTimestamp)\n"
228 $this->waitForReplication();
242 private function loadRevisionsByTimestamp(
int $afterId,
string $fromTimestamp, $batchSize, $toTimestamp ) {
243 $db = $this->getReplicaDB();
244 $queryBuilder = $this->revisionStore->newSelectQueryBuilder( $db )
246 ->where( $db->buildComparison(
'>', [
247 'rev_timestamp' => $fromTimestamp,
248 'rev_id' => $afterId,
250 ->useIndex( [
'revision' =>
'rev_timestamp' ] )
251 ->orderBy( [
'rev_timestamp',
'rev_id' ] )
252 ->limit( $batchSize );
254 if ( $toTimestamp ) {
255 $queryBuilder->where( $db->expr(
'rev_timestamp',
'<', $toTimestamp ) );
258 $rows = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
259 $result = $this->revisionStore->newRevisionsFromBatch( $rows, [
'slots' =>
true ] );
260 $this->handleStatus( $result );
262 $records = array_filter( $result->value );
264 '@phan-var RevisionStoreRecord[] $records';
275 private function loadArchiveByRevisionId(
int $afterId,
int $uptoId, $batchSize ) {
276 $db = $this->getReplicaDB();
277 $rows = $this->revisionStore->newArchiveSelectQueryBuilder( $db )
279 ->where( [ $db->expr(
'ar_rev_id',
'>', $afterId ), $db->expr(
'ar_rev_id',
'<=', $uptoId ) ] )
280 ->orderBy(
'ar_rev_id' )
281 ->limit( $batchSize )
282 ->caller( __METHOD__ )->fetchResultSet();
283 $result = $this->revisionStore->newRevisionsFromBatch(
285 [
'archive' =>
true,
'slots' =>
true ]
287 $this->handleStatus( $result );
289 $records = array_filter( $result->value );
291 '@phan-var RevisionArchiveRecord[] $records';
304 private function getNextRevision(
int $revId,
string $comp,
string $dir ) {
305 $db = $this->getReplicaDB();
306 $next = $db->newSelectQueryBuilder()
309 ->where(
"rev_id $comp $revId" )
310 ->orderBy( [
"rev_id" ], $dir )
311 ->caller( __METHOD__ )
321 private function scanRevisionsById( array $ids ) {
323 $total = count( $ids );
325 $this->output(
"Scanning $total ids\n" );
327 foreach ( array_chunk( $ids, $this->getBatchSize() ) as $batch ) {
328 $revisions = $this->loadRevisionsById( $batch );
335 foreach ( $revisions as $rev ) {
336 $count += $this->checkRevision( $rev );
339 $batchSize = count( $revisions );
340 $this->output(
"\t- Scanned a batch of $batchSize revisions\n" );
351 private function loadRevisionsById( array $ids ) {
352 $db = $this->getReplicaDB();
353 $queryBuilder = $this->revisionStore->newSelectQueryBuilder( $db );
355 $rows = $queryBuilder
357 ->where( [
'rev_id' => $ids ] )
358 ->caller( __METHOD__ )->fetchResultSet();
360 $result = $this->revisionStore->newRevisionsFromBatch( $rows, [
'slots' =>
true ] );
362 $this->handleStatus( $result );
364 $revisions = array_filter( $result->value );
365 '@phan-var RevisionArchiveRecord[] $revisions';
368 if ( count( $revisions ) < count( $ids ) ) {
369 $rows = $this->revisionStore->newArchiveSelectQueryBuilder( $db )
371 ->where( [
'ar_rev_id' => array_diff( $ids, array_keys( $revisions ) ) ] )
372 ->caller( __METHOD__ )->fetchResultSet();
374 $archiveResult = $this->revisionStore->newRevisionsFromBatch(
376 [
'slots' =>
true,
'archive' =>
true ]
379 $this->handleStatus( $archiveResult );
382 $revisions += array_filter( $archiveResult->value );
395 foreach ( $rev->
getSlots()->getSlots() as $slot ) {
396 $count += $this->checkSlot( $rev, $slot );
399 if ( $count === 0 && $this->hasOption(
'mark' ) ) {
400 $this->output(
"\t# No bad blob found on revision {$rev->getId()}, skipped!\n" );
416 $blob = $this->blobStore->getBlob( $address );
417 if ( mb_check_encoding( $blob ) ) {
421 $type =
'invalid-utf-8';
422 $error =
'Invalid UTF-8';
424 }
catch ( Exception $ex ) {
425 $error = $ex->getMessage();
426 $type = get_class( $ex );
431 $this->output(
"\t! Found bad blob on revision {$rev->getId()} "
432 .
"from {$rev->getTimestamp()} ({$slot->getRole()} slot): "
433 .
"content_id={$slot->getContentId()}, address=<{$slot->getAddress()}>, "
434 .
"error='$error', type='$type'. ID:\t{$rev->getId()}\n" );
436 if ( $this->hasOption(
'mark' ) ) {
437 $newAddress = $this->markBlob( $slot, $error );
438 $this->output(
"\tChanged address to <$newAddress>\n" );
450 private function markBlob(
SlotRecord $slot, ?
string $error =
null ) {
453 if ( $this->hasOption(
'mark' ) ) {
454 $args[
'reason'] = $this->getOption(
'mark' );
458 $args[
'error'] = $error;
462 $badAddress =
'bad:' . urlencode( $address );
468 $badAddress = substr( $badAddress, 0, 255 );
470 $dbw = $this->getPrimaryDB();
471 $dbw->newUpdateQueryBuilder()
472 ->update(
'content' )
473 ->set( [
'content_address' => $badAddress ] )
475 ->caller( __METHOD__ )->execute();
480 private function handleStatus(
StatusValue $status ) {
481 if ( !$status->
isOK() ) {
482 $this->fatalError( $status );
484 if ( !$status->
isGood() ) {
485 $this->error( $status );