Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
44 / 44 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| MediaModerationFileScanner | |
100.00% |
44 / 44 |
|
100.00% |
2 / 2 |
11 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| scanSha1 | |
100.00% |
43 / 43 |
|
100.00% |
1 / 1 |
10 | |||
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace MediaWiki\Extension\MediaModeration\Services; |
| 5 | |
| 6 | use MediaWiki\Extension\MediaModeration\PhotoDNA\IMediaModerationPhotoDNAServiceProvider; |
| 7 | use MediaWiki\Extension\MediaModeration\PhotoDNA\Response; |
| 8 | use MediaWiki\Language\RawMessage; |
| 9 | use MediaWiki\Status\StatusFormatter; |
| 10 | use MediaWiki\WikiMap\WikiMap; |
| 11 | use Psr\Log\LoggerInterface; |
| 12 | use StatusValue; |
| 13 | use Wikimedia\Stats\StatsFactory; |
| 14 | |
| 15 | /** |
| 16 | * Scans SHA-1 values in batches to reduce the effect that slow requests to |
| 17 | * PhotoDNA have on the overall speed of scanning. |
| 18 | */ |
| 19 | class MediaModerationFileScanner { |
| 20 | |
| 21 | public function __construct( |
| 22 | private readonly MediaModerationDatabaseLookup $mediaModerationDatabaseLookup, |
| 23 | private readonly MediaModerationDatabaseManager $mediaModerationDatabaseManager, |
| 24 | private readonly MediaModerationFileLookup $mediaModerationFileLookup, |
| 25 | private readonly MediaModerationFileProcessor $mediaModerationFileProcessor, |
| 26 | private readonly IMediaModerationPhotoDNAServiceProvider $mediaModerationPhotoDNAServiceProvider, |
| 27 | private readonly MediaModerationEmailer $mediaModerationEmailer, |
| 28 | private readonly StatusFormatter $statusFormatter, |
| 29 | private readonly StatsFactory $statsFactory, |
| 30 | private readonly LoggerInterface $logger, |
| 31 | ) { |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Scans the files that have the given SHA-1 |
| 36 | * |
| 37 | * @param string $sha1 |
| 38 | * @return StatusValue |
| 39 | */ |
| 40 | public function scanSha1( string $sha1 ): StatusValue { |
| 41 | $wiki = WikiMap::getCurrentWikiId(); |
| 42 | $returnStatus = new StatusValue(); |
| 43 | // Until a match is got from PhotoDNA, the return status should be not okay as the operation has not completed. |
| 44 | $returnStatus->setOK( false ); |
| 45 | // Get the current scan status from the DB, so that we keep the current value if |
| 46 | // nothing matches but still update the last checked value. |
| 47 | $oldMatchStatus = $this->mediaModerationDatabaseLookup->getMatchStatusForSha1( $sha1 ); |
| 48 | $newMatchStatus = null; |
| 49 | foreach ( $this->mediaModerationFileLookup->getFileObjectsForSha1( $sha1 ) as $file ) { |
| 50 | if ( !$this->mediaModerationFileProcessor->canScanFile( $file ) ) { |
| 51 | // If this $file cannot be scanned, then try the next file with this SHA-1 |
| 52 | // and if in verbose mode output to the console about this. |
| 53 | $this->statsFactory->withComponent( 'MediaModeration' ) |
| 54 | ->getCounter( 'file_scanner_found_unscannable_file_total' ) |
| 55 | ->setLabel( 'wiki', $wiki ) |
| 56 | ->increment(); |
| 57 | $returnStatus->fatal( new RawMessage( "The file {$file->getName()} cannot be scanned." ) ); |
| 58 | continue; |
| 59 | } |
| 60 | // Run the check using the PhotoDNA API. |
| 61 | $checkResult = $this->mediaModerationPhotoDNAServiceProvider->check( $file ); |
| 62 | /** @var Response|null $response */ |
| 63 | $response = $checkResult->getValue(); |
| 64 | if ( $response === null || $response->getStatusCode() !== Response::STATUS_OK ) { |
| 65 | // Assume something is wrong with the thumbnail or source file if the request fails, |
| 66 | // and just try a new $file with this SHA-1. Add the information about the |
| 67 | // failure to the return status for tracking and logging. |
| 68 | $returnStatus->merge( $checkResult ); |
| 69 | continue; |
| 70 | } |
| 71 | $newMatchStatus = $response->isMatch(); |
| 72 | // Stop processing this SHA-1 as we have a result. |
| 73 | break; |
| 74 | } |
| 75 | // Update the match status, even if none of the $file objects could be scanned. |
| 76 | // If no scanning was successful, then the status will remain |
| 77 | $this->mediaModerationDatabaseManager->updateMatchStatusForSha1( $sha1, $newMatchStatus ?? $oldMatchStatus ); |
| 78 | if ( $newMatchStatus && $newMatchStatus !== $oldMatchStatus ) { |
| 79 | // Send an email for this SHA-1 if the match status has changed to positive. |
| 80 | // If the match status was already positive, then an email has already been sent. |
| 81 | $this->mediaModerationEmailer->sendEmailForSha1( $sha1 ); |
| 82 | } |
| 83 | if ( $newMatchStatus !== null ) { |
| 84 | $returnStatus->setResult( true, $newMatchStatus ); |
| 85 | } |
| 86 | if ( !$returnStatus->isOK() ) { |
| 87 | // Create a info if the SHA-1 could not be scanned. |
| 88 | $this->logger->info( |
| 89 | 'Unable to scan SHA-1 {sha1}. MediaModerationFileScanner::scanSha1 returned this: {return-message}', |
| 90 | [ |
| 91 | 'sha1' => $sha1, |
| 92 | 'return-message' => $this->statusFormatter->getMessage( $returnStatus, [ 'lang' => 'en' ] ), |
| 93 | ] |
| 94 | ); |
| 95 | } elseif ( !$returnStatus->isGood() ) { |
| 96 | // Create a debug if the SHA-1 scanning succeeded with warnings. |
| 97 | $this->logger->debug( |
| 98 | 'Scan of SHA-1 {sha1} succeeded with warnings. MediaModerationFileScanner::scanSha1 ' . |
| 99 | 'returned this: {return-message}', |
| 100 | [ |
| 101 | 'sha1' => $sha1, |
| 102 | 'return-message' => $this->statusFormatter->getMessage( $returnStatus, [ 'lang' => 'en' ] ), |
| 103 | ] |
| 104 | ); |
| 105 | } |
| 106 | return $returnStatus; |
| 107 | } |
| 108 | } |