Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
YandexWebService | |
0.00% |
0 / 42 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
mapCode | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
doPairs | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
getQuery | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
parseResponse | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\WebService; |
5 | |
6 | use FormatJson; |
7 | use MediaWiki\Http\HttpRequestFactory; |
8 | use Sanitizer; |
9 | |
10 | /** |
11 | * Implements support for Yandex translation API v1. |
12 | * @author Niklas Laxström |
13 | * @license GPL-2.0-or-later |
14 | * @since 2013.01 |
15 | * @ingroup TranslationWebService |
16 | * @see https://tech.yandex.com/translate/ |
17 | */ |
18 | class YandexWebService extends TranslationWebService { |
19 | private HttpRequestFactory $httpRequestFactory; |
20 | |
21 | public function __construct( |
22 | HttpRequestFactory $httpRequestFactory, |
23 | string $serviceName, |
24 | array $config |
25 | ) { |
26 | parent::__construct( $serviceName, $config ); |
27 | $this->httpRequestFactory = $httpRequestFactory; |
28 | |
29 | if ( !isset( $this->config['key'] ) ) { |
30 | throw new TranslationWebServiceConfigurationException( 'API key is not set' ); |
31 | } |
32 | } |
33 | |
34 | /** @inheritDoc */ |
35 | public function getType(): string { |
36 | return 'mt'; |
37 | } |
38 | |
39 | /** @inheritDoc */ |
40 | protected function mapCode( string $code ): string { |
41 | if ( $code === 'be-tarask' ) { |
42 | $code = 'be'; |
43 | } |
44 | return $code; |
45 | } |
46 | |
47 | /** @inheritDoc */ |
48 | protected function doPairs(): array { |
49 | $url = $this->config['pairs'] . '?' . wfArrayToCgi( [ 'key' => $this->config['key'] ] ); |
50 | $json = $this->httpRequestFactory->get( $url, [ 'timeout' => $this->config['timeout'] ], __METHOD__ ); |
51 | if ( $json === null ) { |
52 | throw new TranslationWebServiceException( 'Failure encountered when contacting remote server' ); |
53 | } |
54 | |
55 | $response = FormatJson::decode( $json ); |
56 | if ( !is_object( $response ) ) { |
57 | throw new TranslationWebServiceException( 'Malformed reply from remote server: ' . $json ); |
58 | } |
59 | |
60 | $pairs = []; |
61 | foreach ( $response->dirs as $pair ) { |
62 | [ $source, $target ] = explode( '-', $pair ); |
63 | $pairs[$source][$target] = true; |
64 | } |
65 | |
66 | return $pairs; |
67 | } |
68 | |
69 | /** @inheritDoc */ |
70 | protected function getQuery( string $text, string $sourceLanguage, string $targetLanguage ): TranslationQuery { |
71 | # https://tech.yandex.com/translate/doc/dg/reference/translate-docpage/ |
72 | if ( strlen( $text ) > 10000 ) { |
73 | throw new TranslationWebServiceInvalidInputException( 'Source text too long' ); |
74 | } |
75 | |
76 | $text = $this->wrapUntranslatable( trim( $text ) ); |
77 | |
78 | return TranslationQuery::factory( $this->config['url'] ) |
79 | ->timeout( intval( $this->config['timeout'] ) ) |
80 | ->postWithData( wfArrayToCgi( |
81 | [ |
82 | 'key' => $this->config['key'], |
83 | 'text' => $text, |
84 | 'lang' => "$sourceLanguage-$targetLanguage", |
85 | 'format' => 'html', |
86 | ] |
87 | ) ); |
88 | } |
89 | |
90 | /** @inheritDoc */ |
91 | protected function parseResponse( TranslationQueryResponse $response ): string { |
92 | $body = $response->getBody(); |
93 | $responseBody = FormatJson::decode( $body ); |
94 | if ( !is_object( $responseBody ) ) { |
95 | throw new TranslationWebServiceException( 'Invalid json: ' . serialize( $body ) ); |
96 | } elseif ( $responseBody->code !== 200 ) { |
97 | throw new TranslationWebServiceException( $responseBody->message ); |
98 | } |
99 | |
100 | $text = Sanitizer::decodeCharReferences( $responseBody->text[0] ); |
101 | $text = $this->unwrapUntranslatable( $text ); |
102 | |
103 | return trim( $text ); |
104 | } |
105 | } |