Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
CRAP
61.54% covered (warning)
61.54%
16 / 26
RemoteApiImportTitleChecker
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
6.42
61.54% covered (warning)
61.54%
16 / 26
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 importAllowed
0.00% covered (danger)
0.00%
0 / 1
4.54
44.44% covered (danger)
44.44%
8 / 18
 getParams
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
<?php
namespace FileImporter\Remote\MediaWiki;
use FileImporter\Data\SourceUrl;
use FileImporter\Exceptions\HttpRequestException;
use FileImporter\Exceptions\ImportException;
use FileImporter\Interfaces\ImportTitleChecker;
use FileImporter\Services\Http\HttpRequestExecutor;
use Psr\Log\LoggerInterface;
/**
 * @license GPL-2.0-or-later
 * @author Addshore
 */
class RemoteApiImportTitleChecker implements ImportTitleChecker {
    private const ERROR_TITLE_STATE = 'noTitleStateFetched';
    private $httpApiLookup;
    private $httpRequestExecutor;
    private $logger;
    /**
     * @param HttpApiLookup $httpApiLookup
     * @param HttpRequestExecutor $httpRequestExecutor
     * @param LoggerInterface $logger
     */
    public function __construct(
        HttpApiLookup $httpApiLookup,
        HttpRequestExecutor $httpRequestExecutor,
        LoggerInterface $logger
    ) {
        $this->httpApiLookup = $httpApiLookup;
        $this->httpRequestExecutor = $httpRequestExecutor;
        $this->logger = $logger;
    }
    /**
     * @param SourceUrl $sourceUrl
     * @param string $intendedFileName File name without the File: prefix
     *
     * @return bool false if a file with this name already exists
     * @throws ImportException when the request failed
     */
    public function importAllowed( SourceUrl $sourceUrl, string $intendedFileName ): bool {
        $api = $this->httpApiLookup->getApiUrl( $sourceUrl );
        $apiParameters = $this->getParams( $intendedFileName );
        try {
            $imageInfoRequest = $this->httpRequestExecutor->execute( $api, $apiParameters );
        } catch ( HttpRequestException $e ) {
            $this->logger->error(
                __METHOD__ . ' failed to check title state from: ' . $api,
                [
                    'url' => $e->getHttpRequest()->getFinalUrl(),
                    'content' => $e->getHttpRequest()->getContent(),
                    'errors' => $e->getStatusValue()->getErrors(),
                    'apiUrl' => $api,
                    'apiParameters' => $apiParameters,
                ]
            );
            throw new ImportException(
                'Failed to check title state from: ' . $api, self::ERROR_TITLE_STATE, $e );
        }
        $requestData = json_decode( $imageInfoRequest->getContent(), true );
        if ( !isset( $requestData['query']['pages'][0] ) ) {
            $this->logger->error( __METHOD__ . ' failed, could not find page in result.' );
            return false;
        }
        // Possible return values in output format version 2:
        // { "query": { "pages": [ { "pageid": 123, … when the title exists
        // { "query": { "pages": [ { "missing": true, … otherwise
        // Note the legacy format uses { "missing": "" }, and -1 etc. as keys for missing pages.
        return array_key_exists( 'missing', $requestData['query']['pages'][0] );
    }
    private function getParams( string $titleString ): array {
        return [
            'format' => 'json',
            'action' => 'query',
            'titles' => 'File:' . $titleString,
            'formatversion' => 2,
        ];
    }
}