Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
83 / 83 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| ResendMatchEmails | |
100.00% |
83 / 83 |
|
100.00% |
6 / 6 |
17 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| outputInformationBasedOnStatus | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| getSelectQueryBuilder | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 | |||
| initServices | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| parseTimestamps | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace MediaWiki\Extension\MediaModeration\Maintenance; |
| 5 | |
| 6 | use MediaWiki\Context\RequestContext; |
| 7 | use MediaWiki\Extension\MediaModeration\Services\MediaModerationDatabaseLookup; |
| 8 | use MediaWiki\Extension\MediaModeration\Services\MediaModerationEmailer; |
| 9 | use MediaWiki\Language\MessageLocalizer; |
| 10 | use MediaWiki\Maintenance\Maintenance; |
| 11 | use StatusValue; |
| 12 | use Wikimedia\Rdbms\IDBAccessObject; |
| 13 | use Wikimedia\Rdbms\SelectQueryBuilder; |
| 14 | use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 15 | |
| 16 | // @codeCoverageIgnoreStart |
| 17 | $IP = getenv( 'MW_INSTALL_PATH' ); |
| 18 | if ( $IP === false ) { |
| 19 | $IP = __DIR__ . '/../../..'; |
| 20 | } |
| 21 | require_once "$IP/maintenance/Maintenance.php"; |
| 22 | // @codeCoverageIgnoreEnd |
| 23 | |
| 24 | /** |
| 25 | * Re-sends emails for SHA-1 values determined to be a match for files from a given timestamp. |
| 26 | * Designed for use when emailing was broken and the emails need to be re-sent from the timestamp |
| 27 | * when emailing stopped working. |
| 28 | */ |
| 29 | class ResendMatchEmails extends Maintenance { |
| 30 | |
| 31 | private MediaModerationEmailer $mediaModerationEmailer; |
| 32 | private MediaModerationDatabaseLookup $mediaModerationDatabaseLookup; |
| 33 | private MessageLocalizer $messageLocalizer; |
| 34 | |
| 35 | private string $scannedSince; |
| 36 | private ?string $uploadedSince; |
| 37 | |
| 38 | public function __construct() { |
| 39 | parent::__construct(); |
| 40 | |
| 41 | $this->requireExtension( 'MediaModeration' ); |
| 42 | $this->addDescription( |
| 43 | 'Maintenance script to re-send emails for SHA-1 values marked as a match in the ' . |
| 44 | 'mediamoderation_scan table.' |
| 45 | ); |
| 46 | |
| 47 | $this->addArg( |
| 48 | 'scanned-since', |
| 49 | 'Only re-send emails for SHA-1 values that have been scanned since a given date.', |
| 50 | ); |
| 51 | $this->addOption( |
| 52 | 'uploaded-since', |
| 53 | 'Only include files in the email that were uploaded to the wiki after this timestamp. Default' . |
| 54 | 'is to not filter by upload timestamp.', |
| 55 | false, |
| 56 | true |
| 57 | ); |
| 58 | $this->addOption( |
| 59 | 'sleep', |
| 60 | 'How long to sleep (in seconds) after sending an email. Default: 1', |
| 61 | false, |
| 62 | true |
| 63 | ); |
| 64 | $this->addOption( |
| 65 | 'verbose', |
| 66 | 'Enables verbose mode which prints out information about the emails being sent.' |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | public function execute() { |
| 71 | $this->initServices(); |
| 72 | $this->parseTimestamps(); |
| 73 | |
| 74 | $previousBatchLastSha1Value = ''; |
| 75 | do { |
| 76 | $batch = $this->getSelectQueryBuilder( $previousBatchLastSha1Value ) |
| 77 | ->caller( __METHOD__ ) |
| 78 | ->fetchFieldValues(); |
| 79 | foreach ( $batch as $sha1 ) { |
| 80 | // Send an email for this batch. |
| 81 | $emailerStatus = $this->mediaModerationEmailer->sendEmailForSha1( $sha1, $this->uploadedSince ); |
| 82 | $this->outputInformationBasedOnStatus( $sha1, $emailerStatus ); |
| 83 | // Wait for --sleep seconds to avoid spamming the email address getting these reports. |
| 84 | sleep( intval( $this->getOption( 'sleep', 1 ) ) ); |
| 85 | // Update $previousBatchLastSha1Value to the current $sha1. Once this loop |
| 86 | // completes, the value will be the last SHA-1 in this batch. |
| 87 | $previousBatchLastSha1Value = $sha1; |
| 88 | } |
| 89 | } while ( count( $batch ) ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Outputs verbose information or errors based on the provided StatusValue. |
| 94 | * |
| 95 | * @param string $sha1 |
| 96 | * @param StatusValue $emailerStatus |
| 97 | * @return void |
| 98 | */ |
| 99 | protected function outputInformationBasedOnStatus( string $sha1, StatusValue $emailerStatus ) { |
| 100 | if ( $emailerStatus->isGood() ) { |
| 101 | if ( $this->hasOption( 'verbose' ) ) { |
| 102 | $this->output( "Sent email for SHA-1 $sha1.\n" ); |
| 103 | } |
| 104 | } else { |
| 105 | $this->error( "Email for SHA-1 $sha1 failed to send.\n" ); |
| 106 | $errorOutput = ''; |
| 107 | foreach ( $emailerStatus->getMessages() as $message ) { |
| 108 | $errorOutput .= '* ' . $this->messageLocalizer->msg( $message ) . "\n"; |
| 109 | } |
| 110 | $this->error( $errorOutput ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Gets a SelectQueryBuilder that can be used to produce a batch of SHA-1 values to be passed to |
| 116 | * MediaModerationEmailer::sendEmailForSha1. |
| 117 | * |
| 118 | * @param string $previousBatchLastSha1Value The last SHA-1 value from the previous batch, or an empty string |
| 119 | * if processing the first batch. |
| 120 | * @return SelectQueryBuilder |
| 121 | */ |
| 122 | protected function getSelectQueryBuilder( string $previousBatchLastSha1Value ): SelectQueryBuilder { |
| 123 | // Get a replica DB connection. |
| 124 | $dbr = $this->mediaModerationDatabaseLookup->getDb( IDBAccessObject::READ_NORMAL ); |
| 125 | $selectQueryBuilder = $dbr->newSelectQueryBuilder() |
| 126 | ->select( 'mms_sha1' ) |
| 127 | ->from( 'mediamoderation_scan' ) |
| 128 | ->where( [ |
| 129 | $dbr->expr( 'mms_last_checked', '>=', (int)$this->scannedSince ), |
| 130 | 'mms_is_match' => (int)MediaModerationDatabaseLookup::POSITIVE_MATCH_STATUS, |
| 131 | ] ) |
| 132 | ->orderBy( 'mms_sha1', SelectQueryBuilder::SORT_ASC ) |
| 133 | ->limit( 200 ); |
| 134 | if ( $previousBatchLastSha1Value ) { |
| 135 | $selectQueryBuilder->andWhere( [ |
| 136 | $dbr->expr( 'mms_sha1', '>', $previousBatchLastSha1Value ) |
| 137 | ] ); |
| 138 | } |
| 139 | return $selectQueryBuilder; |
| 140 | } |
| 141 | |
| 142 | protected function initServices(): void { |
| 143 | $services = $this->getServiceContainer(); |
| 144 | $this->mediaModerationDatabaseLookup = $services->get( 'MediaModerationDatabaseLookup' ); |
| 145 | $this->mediaModerationEmailer = $services->get( 'MediaModerationEmailer' ); |
| 146 | $this->messageLocalizer = RequestContext::getMain(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Parse the 'last-checked' timestamp provided via the command line, |
| 151 | * and cause a fatal error if it cannot be parsed. |
| 152 | * |
| 153 | * @return void |
| 154 | */ |
| 155 | protected function parseTimestamps(): void { |
| 156 | $scannedSince = $this->getArg( 'scanned-since' ); |
| 157 | if ( !is_string( $scannedSince ) ) { |
| 158 | $this->fatalError( 'The scanned-since argument must be a string.' ); |
| 159 | } |
| 160 | if ( strlen( $scannedSince ) === 8 && $scannedSince === strval( intval( $scannedSince ) ) ) { |
| 161 | // The 'scanned-since' argument is likely to be in the form YYYYMMDD because: |
| 162 | // * The length of the argument is 8 (which is the length of a YYYYMMDD format) |
| 163 | // * The intval of the 'scanned-since' parameter can be converted to an integer |
| 164 | // and from a string without any changes in value (thus it must be an integer |
| 165 | // in string form). |
| 166 | $this->scannedSince = $scannedSince; |
| 167 | } elseif ( ConvertibleTimestamp::convert( TS_MW, $scannedSince ) ) { |
| 168 | // If the 'scanned-since' argument is recognised as a timestamp by ConvertibleTimestamp::convert, |
| 169 | // then get the date part and discard the time part. |
| 170 | $this->scannedSince = $this->mediaModerationDatabaseLookup->getDateFromTimestamp( $scannedSince ); |
| 171 | } else { |
| 172 | // The 'scanned-since' argument could not be parsed, so raise an error |
| 173 | $this->fatalError( |
| 174 | 'The scanned-since argument passed to this script could not be parsed. This can take a ' . |
| 175 | 'timestamp in string form, or a date in YYYYMMDD format.' |
| 176 | ); |
| 177 | } |
| 178 | // Uploaded since takes any supported timestamp by ConvertibleTimestamp |
| 179 | $uploadedSince = $this->getOption( 'uploaded-since' ); |
| 180 | if ( $uploadedSince !== null ) { |
| 181 | $uploadedSince = ConvertibleTimestamp::convert( TS_MW, $uploadedSince ); |
| 182 | if ( !$uploadedSince ) { |
| 183 | $this->fatalError( 'The uploaded-since timestamp could not be parsed as a valid timestamp' ); |
| 184 | } |
| 185 | } |
| 186 | $this->uploadedSince = $uploadedSince; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // @codeCoverageIgnoreStart |
| 191 | $maintClass = ResendMatchEmails::class; |
| 192 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 193 | // @codeCoverageIgnoreEnd |