Update git submodules
[mediawiki.git] / maintenance / findBadBlobs.php
blob6c0d6c58c6745aebf5877ec83473265ab6c32758
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @ingroup Maintenance
22 use MediaWiki\Revision\RevisionArchiveRecord;
23 use MediaWiki\Revision\RevisionRecord;
24 use MediaWiki\Revision\RevisionStore;
25 use MediaWiki\Revision\RevisionStoreRecord;
26 use MediaWiki\Revision\SlotRecord;
27 use MediaWiki\Status\Status;
28 use MediaWiki\Storage\BlobStore;
29 use Wikimedia\Rdbms\LBFactory;
30 use Wikimedia\Rdbms\LoadBalancer;
32 require_once __DIR__ . '/Maintenance.php';
34 /**
35 * Maintenance script for finding and marking bad content blobs.
37 * @ingroup Maintenance
39 class FindBadBlobs extends Maintenance {
41 /**
42 * @var RevisionStore|null
44 private $revisionStore;
46 /**
47 * @var BlobStore|null
49 private $blobStore;
51 /**
52 * @var LoadBalancer|null
54 private $loadBalancer;
56 /**
57 * @var LBFactory
59 private $lbFactory;
61 public function __construct() {
62 parent::__construct();
64 $this->setBatchSize( 1000 );
65 $this->addDescription( 'Find and mark bad content blobs. Marked blobs will be read as empty. '
66 . 'Use --scan-from to find revisions with bad blobs, use --mark to mark them.' );
67 $this->addOption( 'scan-from', 'Start scanning revisions at the given date. '
68 . 'Format: Anything supported by MediaWiki, e.g. YYYYMMDDHHMMSS or YYYY-MM-DDTHH:MM:SS',
69 false, true );
70 $this->addOption( 'revisions', 'A list of revision IDs to process, separated by comma or '
71 . 'colon or whitespace. Revisions belonging to deleted pages will work. '
72 . 'If set to "-" IDs are read from stdin, one per line.', false, true );
73 $this->addOption( 'limit', 'Maximum number of revisions for --scan-from to scan. '
74 . 'Default: 1000', false, true );
75 $this->addOption( 'mark', 'Mark the blob as "known bad", to avoid errors when '
76 . 'attempting to read it. The value given is the reason for marking the blob as bad, '
77 . 'typically a ticket ID. Requires --revisions to also be set.', false, true );
80 public function initializeServices(
81 ?RevisionStore $revisionStore = null,
82 ?BlobStore $blobStore = null,
83 ?LoadBalancer $loadBalancer = null,
84 ?LBFactory $lbFactory = null
85 ) {
86 $services = $this->getServiceContainer();
88 $this->revisionStore = $revisionStore ?? $this->revisionStore ?? $services->getRevisionStore();
89 $this->blobStore = $blobStore ?? $this->blobStore ?? $services->getBlobStore();
90 $this->loadBalancer = $loadBalancer ?? $this->loadBalancer ?? $services->getDBLoadBalancer();
91 $this->lbFactory = $lbFactory ?? $this->lbFactory ?? $services->getDBLoadBalancerFactory();
94 /**
95 * @return string
97 private function getStartTimestamp() {
98 $tsOpt = $this->getOption( 'scan-from' );
99 if ( strlen( $tsOpt ) < 14 ) {
100 $this->fatalError( 'Bad timestamp: ' . $tsOpt
101 . ', please provide time and date down to the second.' );
104 $ts = wfTimestamp( TS_MW, $tsOpt );
105 if ( !$ts ) {
106 $this->fatalError( 'Bad timestamp: ' . $tsOpt );
109 return $ts;
113 * @return int[]
115 private function getRevisionIds() {
116 $opt = $this->getOption( 'revisions' );
118 if ( $opt === '-' ) {
119 $opt = stream_get_contents( STDIN );
121 if ( !$opt ) {
122 return [];
126 return $this->parseIntList( $opt );
130 * @inheritDoc
132 public function execute() {
133 $this->initializeServices();
135 if ( $this->hasOption( 'revisions' ) ) {
136 if ( $this->hasOption( 'scan-from' ) ) {
137 $this->fatalError( 'Cannot use --revisions together with --scan-from' );
140 $ids = $this->getRevisionIds();
142 $count = $this->scanRevisionsById( $ids );
143 } elseif ( $this->hasOption( 'scan-from' ) ) {
144 if ( $this->hasOption( 'mark' ) ) {
145 $this->fatalError( 'Cannot use --mark with --scan-from, '
146 . 'use --revisions to specify revisions to mark.' );
149 $fromTimestamp = $this->getStartTimestamp();
150 $total = $this->getOption( 'limit', 1000 );
152 $count = $this->scanRevisionsByTimestamp( $fromTimestamp, $total );
154 $this->output( "The range of archive rows scanned is based on the range of revision IDs "
155 . "scanned in the revision table.\n" );
156 } else {
157 if ( $this->hasOption( 'mark' ) ) {
158 $this->fatalError( 'The --mark must be used together with --revisions' );
159 } else {
160 $this->fatalError( 'Must specify one of --revisions or --scan-from' );
164 if ( $this->hasOption( 'mark' ) ) {
165 $this->output( "Marked $count bad revisions.\n" );
166 } else {
167 $this->output( "Found $count bad revisions.\n" );
169 if ( $count > 0 ) {
170 $this->output( "On a unix/linux environment, you can use grep and cut to list of IDs\n" );
171 $this->output( "that can then be used with the --revisions option. E.g.\n" );
172 $this->output( " grep '! Found bad blob' | cut -s -f 3\n" );
178 * @param string $fromTimestamp
179 * @param int $total
181 * @return int
183 private function scanRevisionsByTimestamp( $fromTimestamp, $total ) {
184 $count = 0;
185 $lastRevId = 0;
186 $firstRevId = 0;
187 $lastTimestamp = $fromTimestamp;
188 $revisionRowsScanned = 0;
189 $archiveRowsScanned = 0;
191 $this->output( "Scanning revisions table, "
192 . "$total rows starting at rev_timestamp $fromTimestamp\n" );
194 while ( $revisionRowsScanned < $total ) {
195 $batchSize = min( $total - $revisionRowsScanned, $this->getBatchSize() );
196 $revisions = $this->loadRevisionsByTimestamp( $lastRevId, $lastTimestamp, $batchSize );
197 if ( !$revisions ) {
198 break;
201 foreach ( $revisions as $rev ) {
202 // we are sorting by timestamp, so we may encounter revision IDs out of sequence
203 $firstRevId = $firstRevId ? min( $firstRevId, $rev->getId() ) : $rev->getId();
204 $lastRevId = max( $lastRevId, $rev->getId() );
206 $count += $this->checkRevision( $rev );
209 $lastTimestamp = $rev->getTimestamp();
210 $batchSize = count( $revisions );
211 $revisionRowsScanned += $batchSize;
212 $this->output(
213 "\t- Scanned a batch of $batchSize revisions, "
214 . "up to revision $lastRevId ($lastTimestamp)\n"
217 $this->waitForReplication();
220 // NOTE: the archive table isn't indexed by timestamp, so the best we can do is use the
221 // revision ID just before the first revision ID we found above as the starting point
222 // of the scan, and scan up to on revision after the last revision ID we found above.
223 // If $firstRevId is 0, the loop body above didn't execute,
224 // so we should skip the one below as well.
225 $fromArchived = $this->getNextRevision( $firstRevId, '<', 'DESC' );
226 $maxArchived = $this->getNextRevision( $lastRevId, '>', 'ASC' );
227 $maxArchived = $maxArchived ?: PHP_INT_MAX;
229 $this->output( "Scanning archive table by ar_rev_id, $fromArchived to $maxArchived\n" );
230 while ( $firstRevId > 0 && $fromArchived < $maxArchived ) {
231 $batchSize = min( $total - $archiveRowsScanned, $this->getBatchSize() );
232 $revisions = $this->loadArchiveByRevisionId( $fromArchived, $maxArchived, $batchSize );
233 if ( !$revisions ) {
234 break;
236 /** @var RevisionRecord $rev */
237 foreach ( $revisions as $rev ) {
238 $count += $this->checkRevision( $rev );
240 $fromArchived = $rev->getId();
241 $batchSize = count( $revisions );
242 $archiveRowsScanned += $batchSize;
243 $this->output(
244 "\t- Scanned a batch of $batchSize archived revisions, "
245 . "up to revision $fromArchived ($lastTimestamp)\n"
248 $this->waitForReplication();
251 return $count;
255 * @param int $afterId
256 * @param string $fromTimestamp
257 * @param int $batchSize
259 * @return RevisionStoreRecord[]
261 private function loadRevisionsByTimestamp( int $afterId, string $fromTimestamp, $batchSize ) {
262 $db = $this->lbFactory->getReplicaDatabase();
263 $queryBuilder = $this->revisionStore->newSelectQueryBuilder( $db );
264 $rows = $queryBuilder->joinComment()
265 ->where( $db->buildComparison( '>', [
266 'rev_timestamp' => $fromTimestamp,
267 'rev_id' => $afterId,
268 ] ) )
269 ->useIndex( [ 'revision' => 'rev_timestamp' ] )
270 ->orderBy( [ 'rev_timestamp', 'rev_id' ] )
271 ->limit( $batchSize )
272 ->caller( __METHOD__ )->fetchResultSet();
273 $result = $this->revisionStore->newRevisionsFromBatch( $rows, [ 'slots' => true ] );
274 $this->handleStatus( $result );
276 $records = array_filter( $result->value );
278 '@phan-var RevisionStoreRecord[] $records';
279 return $records;
283 * @param int $afterId
284 * @param int $uptoId
285 * @param int $batchSize
287 * @return RevisionArchiveRecord[]
289 private function loadArchiveByRevisionId( int $afterId, int $uptoId, $batchSize ) {
290 $db = $this->lbFactory->getReplicaDatabase();
291 $rows = $this->revisionStore->newArchiveSelectQueryBuilder( $db )
292 ->joinComment()
293 ->where( [ "ar_rev_id > $afterId", "ar_rev_id <= $uptoId" ] )
294 ->orderBy( 'ar_rev_id' )
295 ->limit( $batchSize )
296 ->caller( __METHOD__ )->fetchResultSet();
297 $result = $this->revisionStore->newRevisionsFromBatch(
298 $rows,
299 [ 'archive' => true, 'slots' => true ]
301 $this->handleStatus( $result );
303 $records = array_filter( $result->value );
305 '@phan-var RevisionArchiveRecord[] $records';
306 return $records;
310 * Returns the revision ID next to $revId, according to $comp and $dir
312 * @param int $revId
313 * @param string $comp the comparator, either '<' or '>', to go with $dir
314 * @param string $dir the sort direction to go with $comp, either 'ARC' or 'DESC'
316 * @return int
318 private function getNextRevision( int $revId, string $comp, string $dir ) {
319 $db = $this->loadBalancer->getConnectionRef( DB_REPLICA );
320 $next = $db->newSelectQueryBuilder()
321 ->select( 'rev_id' )
322 ->from( 'revision' )
323 ->where( "rev_id $comp $revId" )
324 ->orderBy( [ "rev_id" ], $dir )
325 ->caller( __METHOD__ )
326 ->fetchField();
327 return (int)$next;
331 * @param array $ids
333 * @return int
335 private function scanRevisionsById( array $ids ) {
336 $count = 0;
337 $total = count( $ids );
339 $this->output( "Scanning $total ids\n" );
341 foreach ( array_chunk( $ids, $this->getBatchSize() ) as $batch ) {
342 $revisions = $this->loadRevisionsById( $batch );
344 if ( !$revisions ) {
345 continue;
348 /** @var RevisionRecord $rev */
349 foreach ( $revisions as $rev ) {
350 $count += $this->checkRevision( $rev );
353 $batchSize = count( $revisions );
354 $this->output( "\t- Scanned a batch of $batchSize revisions\n" );
357 return $count;
361 * @param int[] $ids
363 * @return RevisionRecord[]
365 private function loadRevisionsById( array $ids ) {
366 $db = $this->lbFactory->getReplicaDatabase();
367 $queryBuilder = $this->revisionStore->newSelectQueryBuilder( $db );
369 $rows = $queryBuilder
370 ->joinComment()
371 ->where( [ 'rev_id' => $ids ] )
372 ->caller( __METHOD__ )->fetchResultSet();
374 $result = $this->revisionStore->newRevisionsFromBatch( $rows, [ 'slots' => true ] );
376 $this->handleStatus( $result );
378 $revisions = array_filter( $result->value );
379 '@phan-var RevisionArchiveRecord[] $revisions';
381 // if not all revisions were found, check the archive table.
382 if ( count( $revisions ) < count( $ids ) ) {
383 $rows = $this->revisionStore->newArchiveSelectQueryBuilder( $db )
384 ->joinComment()
385 ->where( [ 'ar_rev_id' => array_diff( $ids, array_keys( $revisions ) ) ] )
386 ->caller( __METHOD__ )->fetchResultSet();
388 $archiveResult = $this->revisionStore->newRevisionsFromBatch(
389 $rows,
390 [ 'slots' => true, 'archive' => true ]
393 $this->handleStatus( $archiveResult );
395 // don't use array_merge, since it will re-index
396 $revisions += array_filter( $archiveResult->value );
399 return $revisions;
403 * @param RevisionRecord $rev
405 * @return int
407 private function checkRevision( RevisionRecord $rev ) {
408 $count = 0;
409 foreach ( $rev->getSlots()->getSlots() as $slot ) {
410 $count += $this->checkSlot( $rev, $slot );
413 if ( $count === 0 && $this->hasOption( 'mark' ) ) {
414 $this->output( "\t# No bad blob found on revision {$rev->getId()}, skipped!\n" );
417 return $count;
421 * @param RevisionRecord $rev
422 * @param SlotRecord $slot
424 * @return int
426 private function checkSlot( RevisionRecord $rev, SlotRecord $slot ) {
427 $address = $slot->getAddress();
429 try {
430 $this->blobStore->getBlob( $address );
431 // nothing to do
432 return 0;
433 } catch ( Exception $ex ) {
434 $error = $ex->getMessage();
435 $type = get_class( $ex );
438 // NOTE: output the revision ID again at the end in a separate column for easy processing
439 // via the "cut" shell command.
440 $this->output( "\t! Found bad blob on revision {$rev->getId()} "
441 . "from {$rev->getTimestamp()} ({$slot->getRole()} slot): "
442 . "content_id={$slot->getContentId()}, address=<{$slot->getAddress()}>, "
443 . "error='$error', type='$type'. ID:\t{$rev->getId()}\n" );
445 if ( $this->hasOption( 'mark' ) ) {
446 $newAddress = $this->markBlob( $slot, $error );
447 $this->output( "\tChanged address to <$newAddress>\n" );
450 return 1;
454 * @param SlotRecord $slot
455 * @param string|null $error
457 * @return false|string
459 private function markBlob( SlotRecord $slot, string $error = null ) {
460 $args = [];
462 if ( $this->hasOption( 'mark' ) ) {
463 $args['reason'] = $this->getOption( 'mark' );
466 if ( $error ) {
467 $args['error'] = $error;
470 $address = $slot->getAddress() ?: 'empty';
471 $badAddress = 'bad:' . urlencode( $address );
473 if ( $args ) {
474 $badAddress .= '?' . wfArrayToCgi( $args );
477 $badAddress = substr( $badAddress, 0, 255 );
479 $dbw = $this->loadBalancer->getConnectionRef( DB_PRIMARY );
480 $dbw->update(
481 'content',
482 [ 'content_address' => $badAddress ],
483 [ 'content_id' => $slot->getContentId() ],
484 __METHOD__
487 return $badAddress;
490 private function handleStatus( StatusValue $status ) {
491 if ( !$status->isOK() ) {
492 $this->fatalError(
493 Status::wrap( $status )->getMessage( false, false, 'en' )->text()
496 if ( !$status->isGood() ) {
497 $this->error(
498 "\t! " . Status::wrap( $status )->getMessage( false, false, 'en' )->text()
505 $maintClass = FindBadBlobs::class;
506 require_once RUN_MAINTENANCE_IF_MAIN;