Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UploadCompleteHandler
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 onUploadComplete
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\MediaModeration\Hooks\Handlers;
22
23use MediaWiki\Config\Config;
24use MediaWiki\Deferred\DeferredUpdates;
25use MediaWiki\Extension\MediaModeration\Services\MediaModerationDatabaseLookup;
26use MediaWiki\Extension\MediaModeration\Services\MediaModerationEmailer;
27use MediaWiki\Extension\MediaModeration\Services\MediaModerationFileProcessor;
28use MediaWiki\Hook\UploadCompleteHook;
29use MediaWiki\Logger\LoggerFactory;
30use Psr\Log\LoggerInterface;
31
32class UploadCompleteHandler implements UploadCompleteHook {
33
34    private MediaModerationFileProcessor $mediaModerationFileProcessor;
35    private MediaModerationDatabaseLookup $mediaModerationDatabaseLookup;
36    private MediaModerationEmailer $mediaModerationEmailer;
37    private LoggerInterface $logger;
38    private Config $config;
39
40    /**
41     * @param MediaModerationFileProcessor $mediaModerationFileProcessor
42     * @param MediaModerationDatabaseLookup $mediaModerationDatabaseLookup
43     * @param MediaModerationEmailer $mediaModerationEmailer
44     * @param Config $config
45     */
46    public function __construct(
47        MediaModerationFileProcessor $mediaModerationFileProcessor,
48        MediaModerationDatabaseLookup $mediaModerationDatabaseLookup,
49        MediaModerationEmailer $mediaModerationEmailer,
50        Config $config
51    ) {
52        $this->mediaModerationFileProcessor = $mediaModerationFileProcessor;
53        $this->mediaModerationDatabaseLookup = $mediaModerationDatabaseLookup;
54        $this->mediaModerationEmailer = $mediaModerationEmailer;
55        $this->config = $config;
56        $this->logger = LoggerFactory::getInstance( 'mediamoderation' );
57    }
58
59    /** @inheritDoc */
60    public function onUploadComplete( $uploadBase ) {
61        $file = $uploadBase->getLocalFile();
62        if ( $file === null ) {
63            // This should not happen, but if the $file is null then log this as a warning.
64            $this->logger->warning( 'UploadBase::getLocalFile is null on run of UploadComplete hook.' );
65        } elseif ( $this->config->get( 'MediaModerationAddToScanTableOnUpload' ) ) {
66            // If the $file is not null, then process the file on POSTSEND.
67            DeferredUpdates::addCallableUpdate( function () use ( $file ) {
68                if (
69                    $this->mediaModerationDatabaseLookup->fileExistsInScanTable( $file ) &&
70                    $this->mediaModerationDatabaseLookup->getMatchStatusForSha1( $file->getSha1() )
71                ) {
72                    // Send an email for just this file if the SHA-1 is already marked as a match.
73                    // Previously uploaded files that match this SHA-1 have already been sent via email,
74                    // so sending them again is unnecessary.
75                    $this->mediaModerationEmailer->sendEmailForSha1( $file->getSha1(), $file->getTimestamp() );
76                }
77                // Add the file to the scan table if it doesn't already exist.
78                $this->mediaModerationFileProcessor->insertFile( $file );
79            } );
80        }
81    }
82}