Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
RESTBaseWebService
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 6
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 mapCode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doPairs
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 getQuery
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 parseResponse
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\WebService;
5
6use FormatJson;
7use MediaWiki\Http\HttpRequestFactory;
8
9/**
10 * Implements support for cxserver proxied through RESTBase
11 * @ingroup TranslationWebService
12 * @author Niklas Laxström
13 * @license GPL-2.0-or-later
14 * @since 2017.10
15 */
16class RESTBaseWebService extends TranslationWebService {
17    private HttpRequestFactory $httpRequestFactory;
18
19    public function __construct(
20        HttpRequestFactory $httpRequestFactory,
21        string $serviceName,
22        array $config
23    ) {
24        parent::__construct( $serviceName, $config );
25        $this->httpRequestFactory = $httpRequestFactory;
26    }
27
28    /** @inheritDoc */
29    public function getType(): string {
30        return 'mt';
31    }
32
33    /** @inheritDoc */
34    protected function mapCode( string $code ): string {
35        return $code;
36    }
37
38    /** @inheritDoc */
39    protected function doPairs(): array {
40        if ( !isset( $this->config['host'] ) ) {
41            throw new TranslationWebServiceConfigurationException( 'RESTBase host not set' );
42        }
43
44        $pairs = [];
45
46        $url = $this->config['host'] . '/rest_v1/transform/list/tool/mt/';
47        $json = $this->httpRequestFactory->get( $url, [ $this->config['timeout'] ], __METHOD__ );
48        $response = FormatJson::decode( $json, true );
49
50        if ( !is_array( $response ) ) {
51            $exception = 'Malformed reply from remote server: ' . $url . ' ' . (string)$json;
52            throw new TranslationWebServiceException( $exception );
53        }
54
55        foreach ( $response['Apertium'] as $source => $targets ) {
56            foreach ( $targets as $target ) {
57                $pairs[$source][$target] = true;
58            }
59        }
60
61        return $pairs;
62    }
63
64    /** @inheritDoc */
65    protected function getQuery( string $text, string $sourceLanguage, string $targetLanguage ): TranslationQuery {
66        if ( !isset( $this->config['host'] ) ) {
67            throw new TranslationWebServiceConfigurationException( 'RESTBase host not set' );
68        }
69
70        $text = trim( $text );
71        $text = $this->wrapUntranslatable( $text );
72        $url = $this->config['host'] . "/rest_v1/transform/html/from/$sourceLanguage/to/$targetLanguage/Apertium";
73
74        return TranslationQuery::factory( $url )
75            ->timeout( intval( $this->config['timeout'] ) )
76            ->postWithData( wfArrayToCgi( [ 'html' => $text ] ) );
77    }
78
79    /** @inheritDoc */
80    protected function parseResponse( TranslationQueryResponse $reply ): string {
81        $body = $reply->getBody();
82
83        $response = FormatJson::decode( $body );
84        if ( !is_object( $response ) ) {
85            throw new TranslationWebServiceException( 'Invalid json: ' . serialize( $body ) );
86        }
87
88        $text = $response->contents;
89        $text = $this->unwrapUntranslatable( $text );
90
91        return trim( $text );
92    }
93}