Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
35 / 35 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| MediaModerationDatabaseManager | |
100.00% |
35 / 35 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| insertFileToScanTable | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| insertSha1ToScanTable | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| insertToScanTableInternal | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| updateMatchStatus | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| updateMatchStatusForSha1 | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace MediaWiki\Extension\MediaModeration\Services; |
| 5 | |
| 6 | use MediaWiki\FileRepo\File\ArchivedFile; |
| 7 | use MediaWiki\FileRepo\File\File; |
| 8 | use Wikimedia\Rdbms\IDatabase; |
| 9 | use Wikimedia\Rdbms\IDBAccessObject; |
| 10 | use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 11 | |
| 12 | class MediaModerationDatabaseManager { |
| 13 | |
| 14 | public function __construct( |
| 15 | private readonly IDatabase $dbw, |
| 16 | private readonly MediaModerationDatabaseLookup $mediaModerationDatabaseLookup, |
| 17 | ) { |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Takes a File object and adds a reference to the file in the |
| 22 | * mediamoderation_scan table if the reference to the file does |
| 23 | * not already exist. |
| 24 | * |
| 25 | * @param File|ArchivedFile $file The file to be added to the mediamoderation_scan table. |
| 26 | */ |
| 27 | public function insertFileToScanTable( File|ArchivedFile $file ): void { |
| 28 | if ( !$this->mediaModerationDatabaseLookup->fileExistsInScanTable( |
| 29 | $file, IDBAccessObject::READ_LATEST |
| 30 | ) ) { |
| 31 | $this->insertToScanTableInternal( $file ); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Adds the given SHA-1 value to the mediamoderation_scan table if it is not already in the table. |
| 37 | * |
| 38 | * If you have a {@link File} object, you should then use |
| 39 | * {@link MediaModerationDatabaseManager::insertFileToScanTable} instead. |
| 40 | * |
| 41 | * @param string $sha1 The SHA-1 of the file |
| 42 | */ |
| 43 | public function insertSha1ToScanTable( string $sha1 ): void { |
| 44 | $rowExists = $this->dbw->newSelectQueryBuilder() |
| 45 | ->select( '1' ) |
| 46 | ->from( 'mediamoderation_scan' ) |
| 47 | ->where( [ 'mms_sha1' => $sha1 ] ) |
| 48 | ->caller( __METHOD__ ) |
| 49 | ->fetchField(); |
| 50 | |
| 51 | if ( !$rowExists ) { |
| 52 | $this->dbw->newInsertQueryBuilder() |
| 53 | ->insert( 'mediamoderation_scan' ) |
| 54 | ->row( [ 'mms_sha1' => $sha1 ] ) |
| 55 | ->caller( __METHOD__ ) |
| 56 | ->execute(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Inserts a given File to the mediamoderation_scan table. |
| 62 | * Does not check for the existence of the File in the scan |
| 63 | * table, so will cause a DBError if the File is already in |
| 64 | * the table. |
| 65 | * |
| 66 | * @param File|ArchivedFile $file |
| 67 | * @return void |
| 68 | */ |
| 69 | private function insertToScanTableInternal( $file ) { |
| 70 | // Insert a row to the mediamoderation_scan table with the SHA-1 of the file. |
| 71 | $this->dbw->newInsertQueryBuilder() |
| 72 | ->insert( 'mediamoderation_scan' ) |
| 73 | ->row( [ 'mms_sha1' => $file->getSha1() ] ) |
| 74 | ->caller( __METHOD__ ) |
| 75 | ->execute(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Updates the mediamoderation_scan row with for the given file |
| 80 | * with the match status as determined by PhotoDNA. |
| 81 | * |
| 82 | * This also sets the mss_last_checked column to the current time |
| 83 | * to indicate that now is the last time the file was checked. |
| 84 | * |
| 85 | * If the SHA-1 of the $file does not exist in the scan table, a row |
| 86 | * will be created for it before the update occurs. |
| 87 | * |
| 88 | * @param File|ArchivedFile $file The file that was scanned by PhotoDNA |
| 89 | * @param null|bool $isMatch Whether the file is a match (null if the scan failed) |
| 90 | * @return void |
| 91 | */ |
| 92 | public function updateMatchStatus( $file, ?bool $isMatch ) { |
| 93 | // Check if the SHA-1 exists in the scan table. If not, then add it to the |
| 94 | // mediamoderation_scan table first before attempting to update the match status. |
| 95 | $this->insertFileToScanTable( $file ); |
| 96 | $this->updateMatchStatusForSha1( $file->getSha1(), $isMatch ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Updates the mediamoderation_scan row for the given SHA-1 |
| 101 | * with the match status as determined by PhotoDNA. |
| 102 | * |
| 103 | * If you have a $file object, you should pass use ::updateMatchStatus |
| 104 | * instead as this method does not first check if the file is referenced |
| 105 | * in the mediamoderation_scan table. |
| 106 | * |
| 107 | * @param string $sha1 |
| 108 | * @param null|bool $isMatch Whether the file is a match (null if the scan failed) |
| 109 | * @return void |
| 110 | */ |
| 111 | public function updateMatchStatusForSha1( string $sha1, ?bool $isMatch ) { |
| 112 | // If $isMatch is a boolean, convert this to the string representation of a boolean |
| 113 | // for storage in the DB. |
| 114 | if ( $isMatch !== null ) { |
| 115 | $isMatch = intval( $isMatch ); |
| 116 | } |
| 117 | // Update the match status for the $sha1 and also update the last checked timestamp. |
| 118 | $this->dbw->newUpdateQueryBuilder() |
| 119 | ->table( 'mediamoderation_scan' ) |
| 120 | ->set( [ |
| 121 | 'mms_is_match' => $isMatch, |
| 122 | // Take the MW_TS current timestamp and keep the first 8 characters which is YYYYMMDD. |
| 123 | 'mms_last_checked' => intval( substr( ConvertibleTimestamp::now(), 0, 8 ) ) |
| 124 | ] ) |
| 125 | ->where( [ 'mms_sha1' => $sha1 ] ) |
| 126 | ->caller( __METHOD__ ) |
| 127 | ->execute(); |
| 128 | } |
| 129 | } |