Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
RemoteTTMServerWebService | |
0.00% |
0 / 22 |
|
0.00% |
0 / 5 |
72 | |
0.00% |
0 / 1 |
getType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
mapCode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doPairs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getQuery | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
parseResponse | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\WebService; |
5 | |
6 | use MediaWiki\Json\FormatJson; |
7 | |
8 | /** |
9 | * Class for querying external translation service. Implements support for ttmserver via MediaWiki API. |
10 | * @author Niklas Laxström |
11 | * @license GPL-2.0-or-later |
12 | * @since 2013-01-01 |
13 | * @ingroup TranslationWebService |
14 | * @see https://www.mediawiki.org/wiki/Help:Extension:Translate/Translation_memories |
15 | */ |
16 | class RemoteTTMServerWebService extends TranslationWebService { |
17 | /** @inheritDoc */ |
18 | public function getType(): string { |
19 | return 'ttmserver'; |
20 | } |
21 | |
22 | /** @inheritDoc */ |
23 | protected function mapCode( string $code ): string { |
24 | return $code; // Unused |
25 | } |
26 | |
27 | /** @inheritDoc */ |
28 | protected function doPairs(): array { |
29 | return []; // Unused |
30 | } |
31 | |
32 | /** @inheritDoc */ |
33 | protected function getQuery( string $text, string $sourceLanguage, string $targetLanguage ): TranslationQuery { |
34 | $params = [ |
35 | 'format' => 'json', |
36 | 'action' => 'ttmserver', |
37 | 'sourcelanguage' => $sourceLanguage, |
38 | 'targetlanguage' => $targetLanguage, |
39 | 'text' => $text |
40 | ]; |
41 | |
42 | if ( isset( $this->config['service'] ) ) { |
43 | $params['service'] = $this->config['service']; |
44 | } |
45 | |
46 | return TranslationQuery::factory( $this->config['url'] ) |
47 | ->timeout( intval( $this->config['timeout'] ) ) |
48 | ->queryParameters( $params ); |
49 | } |
50 | |
51 | /** @inheritDoc */ |
52 | protected function parseResponse( TranslationQueryResponse $reply ) { |
53 | $body = $reply->getBody(); |
54 | $parsed = FormatJson::decode( $body, true ); |
55 | if ( !is_array( $parsed ) ) { |
56 | throw new TranslationWebServiceException( 'Invalid json: ' . serialize( $body ) ); |
57 | } |
58 | |
59 | if ( !isset( $parsed['ttmserver'] ) ) { |
60 | throw new TranslationWebServiceException( 'Unexpected reply from remote server' ); |
61 | } |
62 | |
63 | return $parsed['ttmserver']; |
64 | } |
65 | } |