summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/webservices/RemoteTTMServerWebService.php
blob: c54d319a8228b2d91d643bc051ffcc88efa8e743 (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
<?php
/**
 * Contains a class for querying external translation service.
 *
 * @file
 * @author Niklas Laxström
 * @copyright Copyright © 2010-2013 Niklas Laxström
 * @license GPL-2.0-or-later
 */

/**
 * Implements support for ttmserver via MediaWiki API.
 * @see https://www.mediawiki.org/wiki/Help:Extension:Translate/Translation_memories
 * @ingroup TranslationWebService
 * @since 2013-01-01
 */
class RemoteTTMServerWebService extends TranslationWebService {
	public function getType() {
		return 'ttmserver';
	}

	protected function mapCode( $code ) {
		return $code; // Unused
	}

	protected function doPairs() {
		return null; // Unused
	}

	protected function getQuery( $text, $from, $to ) {
		$params = [
			'format' => 'json',
			'action' => 'ttmserver',
			'sourcelanguage' => $from,
			'targetlanguage' => $to,
			'text' => $text
		];

		if ( isset( $this->config['service'] ) ) {
			$params['service'] = $this->config['service'];
		}

		return TranslationQuery::factory( $this->config['url'] )
			->timeout( $this->config['timeout'] )
			->queryParameters( $params );
	}

	protected function parseResponse( TranslationQueryResponse $reply ) {
		$body = $reply->getBody();
		$parsed = FormatJson::decode( $body, true );
		if ( !is_array( $parsed ) ) {
			throw new TranslationWebServiceException( 'Invalid json: ' . serialize( $body ) );
		}

		if ( !isset( $parsed['ttmserver'] ) ) {
			throw new TranslationWebServiceException( 'Unexpected reply from remote server' );
		}

		return $parsed['ttmserver'];
	}
}