Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
CxserverWebService | |
0.00% |
0 / 25 |
|
0.00% |
0 / 6 |
110 | |
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 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doPairs | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
getQuery | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
parseResponse | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
handlePairsForService | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getServiceName | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
handleServiceResponse | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\WebService; |
5 | |
6 | use FormatJson; |
7 | use MediaWiki\Http\HttpRequestFactory; |
8 | |
9 | /** |
10 | * Used for interacting with translation services supported by Cxserver |
11 | * @ingroup TranslationWebService |
12 | * @author Abijeet Patro |
13 | * @license GPL-2.0-or-later |
14 | * @since 2023.06 |
15 | */ |
16 | abstract class CxserverWebService extends TranslationWebService { |
17 | private HttpRequestFactory $httpRequestFactory; |
18 | |
19 | public function __construct( |
20 | HttpRequestFactory $httpRequestFactory, |
21 | string $service, |
22 | array $config |
23 | ) { |
24 | parent::__construct( $service, $config ); |
25 | if ( !isset( $this->config['host'] ) ) { |
26 | throw new TranslationWebServiceConfigurationException( 'Cxserver host not set' ); |
27 | } |
28 | $this->httpRequestFactory = $httpRequestFactory; |
29 | } |
30 | |
31 | public function getType(): string { |
32 | return 'mt'; |
33 | } |
34 | |
35 | protected function mapCode( string $code ): string { |
36 | return $code; |
37 | } |
38 | |
39 | protected function doPairs(): array { |
40 | $url = $this->config['host'] . '/v1/list/mt'; |
41 | $json = $this->httpRequestFactory->get( $url, [ $this->config['timeout'] ], __METHOD__ ); |
42 | if ( $json === null ) { |
43 | throw new TranslationWebServiceException( 'Failure encountered when contacting remote server' ); |
44 | } |
45 | |
46 | $response = FormatJson::decode( $json, true ); |
47 | if ( !is_array( $response ) ) { |
48 | throw new TranslationWebServiceException( 'Malformed reply from remote server: ' . $json ); |
49 | } |
50 | |
51 | return $this->handlePairsForService( $response ); |
52 | } |
53 | |
54 | protected function getQuery( string $text, string $sourceLanguage, string $targetLanguage ): TranslationQuery { |
55 | $text = trim( $text ); |
56 | $text = $this->wrapUntranslatable( $text ); |
57 | $url = $this->config['host'] . "/v1/mt/$sourceLanguage/$targetLanguage/{$this->getServiceName()}"; |
58 | |
59 | return TranslationQuery::factory( $url ) |
60 | ->timeout( intval( $this->config['timeout'] ) ) |
61 | ->postWithData( wfArrayToCgi( [ 'html' => $text ] ) ); |
62 | } |
63 | |
64 | protected function parseResponse( TranslationQueryResponse $response ) { |
65 | $body = $response->getBody(); |
66 | $parsedBody = FormatJson::decode( $body, true ); |
67 | if ( !is_array( $parsedBody ) ) { |
68 | throw new TranslationWebServiceException( 'Invalid json: ' . serialize( $body ) ); |
69 | } |
70 | |
71 | return $this->handleServiceResponse( $parsedBody ); |
72 | } |
73 | |
74 | abstract protected function handlePairsForService( array $response ): array; |
75 | |
76 | abstract protected function getServiceName(): string; |
77 | |
78 | abstract protected function handleServiceResponse( array $responseBody ): string; |
79 | } |