45 parent::__construct();
48 $this->
addDescription(
'Find and mark bad content blobs. Marked blobs will be read as empty. '
49 .
'Use --scan-from to find revisions with bad blobs, use --mark to mark them.' );
50 $this->
addOption(
'scan-from',
'Start scanning revisions at the given date. '
51 .
'Format: Anything supported by MediaWiki, e.g. YYYYMMDDHHMMSS or YYYY-MM-DDTHH:MM:SS',
53 $this->
addOption(
'revisions',
'A list of revision IDs to process, separated by comma or '
54 .
'colon or whitespace. Revisions belonging to deleted pages will work. '
55 .
'If set to "-" IDs are read from stdin, one per line.',
false,
true );
56 $this->
addOption(
'limit',
'Maximum number of revisions for --scan-from to scan. '
57 .
'Default: 1000',
false,
true );
58 $this->
addOption(
'mark',
'Mark the blob as "known bad", to avoid errors when '
59 .
'attempting to read it. The value given is the reason for marking the blob as bad, '
60 .
'typically a ticket ID. Requires --revisions to also be set.',
false,
true );
66 private function getStartTimestamp() {
68 if ( strlen( $tsOpt ) < 14 ) {
70 .
', please provide time and date down to the second.' );
75 $this->
fatalError(
'Bad timestamp: ' . $tsOpt );
84 private function getRevisionIds() {
88 $opt = stream_get_contents( STDIN );
103 $this->revisionStore = $services->getRevisionStore();
104 $this->blobStore = $services->getBlobStore();
108 $this->
fatalError(
'Cannot use --revisions together with --scan-from' );
111 $ids = $this->getRevisionIds();
113 $count = $this->scanRevisionsById( $ids );
114 } elseif ( $this->
hasOption(
'scan-from' ) ) {
116 $this->
fatalError(
'Cannot use --mark with --scan-from, '
117 .
'use --revisions to specify revisions to mark.' );
120 $fromTimestamp = $this->getStartTimestamp();
121 $total = $this->
getOption(
'limit', 1000 );
123 $count = $this->scanRevisionsByTimestamp( $fromTimestamp, $total );
125 $this->
output(
"The range of archive rows scanned is based on the range of revision IDs "
126 .
"scanned in the revision table.\n" );
129 $this->
fatalError(
'The --mark must be used together with --revisions' );
131 $this->
fatalError(
'Must specify one of --revisions or --scan-from' );
136 $this->
output(
"Marked $count bad revisions.\n" );
138 $this->
output(
"Found $count bad revisions.\n" );
141 $this->
output(
"On a unix/linux environment, you can use grep and cut to list of IDs\n" );
142 $this->
output(
"that can then be used with the --revisions option. E.g.\n" );
143 $this->
output(
" grep '! Found bad blob' | cut -s -f 3\n" );
154 private function scanRevisionsByTimestamp( $fromTimestamp, $total ) {
158 $lastTimestamp = $fromTimestamp;
159 $revisionRowsScanned = 0;
160 $archiveRowsScanned = 0;
162 $this->
output(
"Scanning revisions table, "
163 .
"$total rows starting at rev_timestamp $fromTimestamp\n" );
165 while ( $revisionRowsScanned < $total ) {
166 $batchSize = min( $total - $revisionRowsScanned, $this->
getBatchSize() );
167 $revisions = $this->loadRevisionsByTimestamp( $lastRevId, $lastTimestamp, $batchSize );
172 foreach ( $revisions as $rev ) {
174 $firstRevId = $firstRevId ? min( $firstRevId, $rev->getId() ) : $rev->getId();
175 $lastRevId = max( $lastRevId, $rev->getId() );
177 $count += $this->checkRevision( $rev );
180 $lastTimestamp = $rev->getTimestamp();
181 $batchSize = count( $revisions );
182 $revisionRowsScanned += $batchSize;
184 "\t- Scanned a batch of $batchSize revisions, "
185 .
"up to revision $lastRevId ($lastTimestamp)\n"
196 $fromArchived = $this->getNextRevision( $firstRevId,
'<',
'DESC' );
197 $maxArchived = $this->getNextRevision( $lastRevId,
'>',
'ASC' );
198 $maxArchived = $maxArchived ?: PHP_INT_MAX;
200 $this->
output(
"Scanning archive table by ar_rev_id, $fromArchived to $maxArchived\n" );
201 while ( $firstRevId > 0 && $fromArchived < $maxArchived ) {
202 $batchSize = min( $total - $archiveRowsScanned, $this->
getBatchSize() );
203 $revisions = $this->loadArchiveByRevisionId( $fromArchived, $maxArchived, $batchSize );
208 foreach ( $revisions as $rev ) {
209 $count += $this->checkRevision( $rev );
211 $fromArchived = $rev->getId();
212 $batchSize = count( $revisions );
213 $archiveRowsScanned += $batchSize;
215 "\t- Scanned a batch of $batchSize archived revisions, "
216 .
"up to revision $fromArchived ($lastTimestamp)\n"
232 private function loadRevisionsByTimestamp(
int $afterId,
string $fromTimestamp, $batchSize ) {
234 $queryBuilder = $this->revisionStore->newSelectQueryBuilder( $db );
235 $rows = $queryBuilder->joinComment()
236 ->where( $db->buildComparison(
'>', [
237 'rev_timestamp' => $fromTimestamp,
238 'rev_id' => $afterId,
240 ->useIndex( [
'revision' =>
'rev_timestamp' ] )
241 ->orderBy( [
'rev_timestamp',
'rev_id' ] )
242 ->limit( $batchSize )
243 ->caller( __METHOD__ )->fetchResultSet();
244 $result = $this->revisionStore->newRevisionsFromBatch( $rows, [
'slots' =>
true ] );
245 $this->handleStatus( $result );
247 $records = array_filter( $result->value );
249 '@phan-var RevisionStoreRecord[] $records';
260 private function loadArchiveByRevisionId(
int $afterId,
int $uptoId, $batchSize ) {
262 $rows = $this->revisionStore->newArchiveSelectQueryBuilder( $db )
264 ->where( [ $db->expr(
'ar_rev_id',
'>', $afterId ), $db->expr(
'ar_rev_id',
'<=', $uptoId ) ] )
265 ->orderBy(
'ar_rev_id' )
266 ->limit( $batchSize )
267 ->caller( __METHOD__ )->fetchResultSet();
268 $result = $this->revisionStore->newRevisionsFromBatch(
270 [
'archive' =>
true,
'slots' =>
true ]
272 $this->handleStatus( $result );
274 $records = array_filter( $result->value );
276 '@phan-var RevisionArchiveRecord[] $records';
289 private function getNextRevision(
int $revId,
string $comp,
string $dir ) {
291 $next = $db->newSelectQueryBuilder()
294 ->where(
"rev_id $comp $revId" )
295 ->orderBy( [
"rev_id" ], $dir )
296 ->caller( __METHOD__ )
306 private function scanRevisionsById( array $ids ) {
308 $total = count( $ids );
310 $this->
output(
"Scanning $total ids\n" );
312 foreach ( array_chunk( $ids, $this->
getBatchSize() ) as $batch ) {
313 $revisions = $this->loadRevisionsById( $batch );
320 foreach ( $revisions as $rev ) {
321 $count += $this->checkRevision( $rev );
324 $batchSize = count( $revisions );
325 $this->
output(
"\t- Scanned a batch of $batchSize revisions\n" );
336 private function loadRevisionsById( array $ids ) {
338 $queryBuilder = $this->revisionStore->newSelectQueryBuilder( $db );
340 $rows = $queryBuilder
342 ->where( [
'rev_id' => $ids ] )
343 ->caller( __METHOD__ )->fetchResultSet();
345 $result = $this->revisionStore->newRevisionsFromBatch( $rows, [
'slots' =>
true ] );
347 $this->handleStatus( $result );
349 $revisions = array_filter( $result->value );
350 '@phan-var RevisionArchiveRecord[] $revisions';
353 if ( count( $revisions ) < count( $ids ) ) {
354 $rows = $this->revisionStore->newArchiveSelectQueryBuilder( $db )
356 ->where( [
'ar_rev_id' => array_diff( $ids, array_keys( $revisions ) ) ] )
357 ->caller( __METHOD__ )->fetchResultSet();
359 $archiveResult = $this->revisionStore->newRevisionsFromBatch(
361 [
'slots' =>
true,
'archive' =>
true ]
364 $this->handleStatus( $archiveResult );
367 $revisions += array_filter( $archiveResult->value );
380 foreach ( $rev->
getSlots()->getSlots() as $slot ) {
381 $count += $this->checkSlot( $rev, $slot );
384 if ( $count === 0 && $this->
hasOption(
'mark' ) ) {
385 $this->
output(
"\t# No bad blob found on revision {$rev->getId()}, skipped!\n" );
401 $this->blobStore->getBlob( $address );
404 }
catch ( Exception $ex ) {
405 $error = $ex->getMessage();
406 $type = get_class( $ex );
411 $this->
output(
"\t! Found bad blob on revision {$rev->getId()} "
412 .
"from {$rev->getTimestamp()} ({$slot->getRole()} slot): "
413 .
"content_id={$slot->getContentId()}, address=<{$slot->getAddress()}>, "
414 .
"error='$error', type='$type'. ID:\t{$rev->getId()}\n" );
417 $newAddress = $this->markBlob( $slot, $error );
418 $this->
output(
"\tChanged address to <$newAddress>\n" );
430 private function markBlob(
SlotRecord $slot, ?
string $error =
null ) {
434 $args[
'reason'] = $this->
getOption(
'mark' );
438 $args[
'error'] = $error;
442 $badAddress =
'bad:' . urlencode( $address );
448 $badAddress = substr( $badAddress, 0, 255 );
451 $dbw->newUpdateQueryBuilder()
452 ->update(
'content' )
453 ->set( [
'content_address' => $badAddress ] )
455 ->caller( __METHOD__ )->execute();
460 private function handleStatus(
StatusValue $status ) {
461 if ( !$status->
isOK() ) {
464 if ( !$status->
isGood() ) {
465 $this->
error( $status );