blob: 0a028507797b24b39358c4d274762400513b7d5b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
<?php
/**
* Contains a class for querying external translation service.
*
* @file
* @author Niklas Laxström
* @license GPL-2.0-or-later
*/
use MediaWiki\MediaWikiServices;
/**
* Implements support for cxserver proxied through RESTBase
* @ingroup TranslationWebService
* @since 2017.10
*/
class RESTBaseWebService extends TranslationWebService {
public function getType() {
return 'mt';
}
protected function mapCode( $code ) {
return $code;
}
protected function doPairs() {
if ( !isset( $this->config['host'] ) ) {
throw new TranslationWebServiceConfigurationException( 'RESTBase host not set' );
}
$pairs = [];
$url = $this->config['host'] . '/rest_v1/transform/list/tool/mt/';
$json = MediaWikiServices::getInstance()->getHttpRequestFactory()->get(
$url,
[ $this->config['timeout'] ],
__METHOD__
);
$response = FormatJson::decode( $json, true );
if ( !is_array( $response ) ) {
$exception = 'Malformed reply from remote server: ' . $url . ' ' . (string)$json;
throw new TranslationWebServiceException( $exception );
}
foreach ( $response['Apertium'] as $source => $targets ) {
foreach ( $targets as $target ) {
$pairs[$source][$target] = true;
}
}
return $pairs;
}
protected function getQuery( $text, $from, $to ) {
if ( !isset( $this->config['host'] ) ) {
throw new TranslationWebServiceConfigurationException( 'RESTBase host not set' );
}
$text = trim( $text );
$text = $this->wrapUntranslatable( $text );
$url = $this->config['host'] . "/rest_v1/transform/html/from/$from/to/$to/Apertium";
return TranslationQuery::factory( $url )
->timeout( $this->config['timeout'] )
->postWithData( wfArrayToCgi( [ 'html' => $text ] ) );
}
protected function parseResponse( TranslationQueryResponse $reply ) {
$body = $reply->getBody();
$response = FormatJson::decode( $body );
if ( !is_object( $response ) ) {
throw new TranslationWebServiceException( 'Invalid json: ' . serialize( $body ) );
}
$text = $response->contents;
$text = $this->unwrapUntranslatable( $text );
return trim( $text );
}
}
|