Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
DebugPhotoDNA
n/a
0 / 0
n/a
0 / 0
13
n/a
0 / 0
 __construct
n/a
0 / 0
n/a
0 / 0
1
 execute
n/a
0 / 0
n/a
0 / 0
12
1<?php
2declare( strict_types=1 );
3
4namespace MediaWiki\Extension\MediaModeration\Maintenance;
5
6use MediaWiki\Context\RequestContext;
7use MediaWiki\Extension\MediaModeration\PhotoDNA\Response;
8use MediaWiki\Extension\MediaModeration\Services\MediaModerationPhotoDNAServiceProvider;
9use MediaWiki\Json\FormatJson;
10use MediaWiki\Maintenance\Maintenance;
11use MediaWiki\Title\Title;
12
13// This is a developer script, no need to count it in code coverage metrics.
14// @codeCoverageIgnoreStart
15$IP = getenv( 'MW_INSTALL_PATH' );
16if ( $IP === false ) {
17    $IP = __DIR__ . '/../../..';
18}
19require_once "$IP/maintenance/Maintenance.php";
20
21/**
22 * Helper script to check how the result from transmitting a local file
23 * via the service provider implementation to PhotoDNA. Does not have
24 * any side effects in the mediamoderation_scan table.
25 */
26class DebugPhotoDNA extends Maintenance {
27
28    public function __construct() {
29        parent::__construct();
30        $this->requireExtension( 'MediaModeration' );
31
32        $this->addDescription( 'Debug sending a single file to the PhotoDNA service endpoint via MediaModeration.' );
33        $this->addArg( 'filename', 'Filename (without File: prefix) to use in check.' );
34        $this->addOption( 'use-mock-endpoint', 'Use the mock API endpoint. Default is false, and to use production.' );
35        $this->addOption( 'raw-json', 'Return only the raw JSON from the endpoint, useful if you want to pipe to jq' );
36    }
37
38    public function execute() {
39        $services = $this->getServiceContainer();
40        $useMock = $this->getOption( 'use-mock-endpoint', false );
41        $photoDNAServiceProvider = $useMock ?
42                $services->get( '_MediaModerationMockPhotoDNAServiceProvider' ) :
43                $services->get( 'MediaModerationPhotoDNAServiceProvider' );
44        if ( !$useMock && !( $photoDNAServiceProvider instanceof MediaModerationPhotoDNAServiceProvider ) ) {
45            $this->fatalError(
46                'Unable to get production service provider. ' .
47                 'Check that $wgMediaModerationPhotoDNAUrl and $wgMediaModerationPhotoDNASubscriptionKey are set.'
48            );
49        }
50        $file = $services->getRepoGroup()->getLocalRepo()->findFile(
51            Title::newFromText( $this->getArg(), NS_FILE )
52        );
53        if ( !$file ) {
54            $this->fatalError( 'Unable to get file for ' . $this->getArg() );
55        }
56        $result = $photoDNAServiceProvider->check( $file );
57        /** @var Response|null $response */
58        $response = $result->getValue();
59        $statusFormatter = $services->getFormatterFactory()->getStatusFormatter( RequestContext::getMain() );
60
61        if ( $this->getOption( 'raw-json' ) ) {
62            if ( !$response ) {
63                $this->fatalError( FormatJson::encode( [ 'error' => $statusFormatter->getWikiText( $result ) ] ) );
64            }
65            $this->output( $response->getRawResponse() );
66            return;
67        }
68
69        $this->output( 'StatusValue: ' . ( $result->isGood() ? 'good' : 'bad' ) . "\n" );
70        if ( $response ) {
71            $this->output( 'Status code: ' . $response->getStatusCode() . "\n" );
72        }
73        if ( $result->isGood() && $response ) {
74            $this->output( 'IsMatch: ' . (int)$response->isMatch() . "\n" );
75        } else {
76            $this->output(
77                'Error: ' . $statusFormatter->getWikiText( $result ) . "\n"
78            );
79        }
80        if ( $response ) {
81            $this->output( "Raw response:\n" . $response->getRawResponse() . "\n" );
82        }
83    }
84}
85
86$maintClass = DebugPhotoDNA::class;
87require_once RUN_MAINTENANCE_IF_MAIN;
88// @codeCoverageIgnoreEnd