summaryrefslogtreecommitdiff
blob: f209100f490ca53ee1707efbe1c15c3063303740 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid;

use Exception;
use IContextSource;
use MediaWiki\Extension\Translate\Services;
use MediaWiki\Extension\Translate\TranslatorInterface\TranslationHelperException;
use MediaWiki\Extension\Translate\TtmServer\TtmServerFactory;
use MediaWiki\Extension\Translate\WebService\RemoteTTMServerWebService;
use MediaWiki\Extension\Translate\WebService\TranslationWebService;
use MessageGroup;
use MessageHandle;
use ReadableTTMServer;
use Title;
use TranslateUtils;
use TTMServer;

/**
 * Translation aid that provides suggestion from translation memory.
 * @ingroup TranslationAids
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @since 2013-01-01 | 2015.02 extends QueryAggregatorAwareTranslationAid
 */
class TTMServerAid extends QueryAggregatorAwareTranslationAid {
	/** @var array[] */
	private $services;
	/** @var TtmServerFactory */
	private $ttmServerFactory;

	public function __construct(
		MessageGroup $group,
		MessageHandle $handle,
		IContextSource $context,
		TranslationAidDataProvider $dataProvider
	) {
		parent::__construct( $group, $handle, $context, $dataProvider );
		$this->ttmServerFactory = Services::getInstance()->getTtmServerFactory();
	}

	public function populateQueries(): void {
		$text = $this->dataProvider->getDefinition();
		$from = $this->group->getSourceLanguage();
		$to = $this->handle->getCode();

		if ( trim( $text ) === '' ) {
			return;
		}

		foreach ( $this->getWebServices() as $service ) {
			$this->storeQuery( $service, $from, $to, $text );
		}
	}

	public function getData(): array {
		$text = $this->dataProvider->getDefinition();
		if ( trim( $text ) === '' ) {
			return [];
		}

		$suggestions = [];
		$from = $this->group->getSourceLanguage();
		$to = $this->handle->getCode();

		foreach ( $this->getInternalServices() as $name => $service ) {
			try {
				$queryData = $service->query( $from, $to, $text );
			} catch ( TranslationHelperException $e ) {
				throw $e;
			} catch ( Exception $e ) {
				// Not ideal to catch all exceptions
				continue;
			}

			$sugs = $this->formatInternalSuggestions( $queryData, $service, $name, $from );
			$suggestions = array_merge( $suggestions, $sugs );
		}

		// Results from web services
		foreach ( $this->getQueryData() as $queryData ) {
			$sugs = $this->formatWebSuggestions( $queryData );
			$suggestions = array_merge( $suggestions, $sugs );
		}

		$suggestions = TTMServer::sortSuggestions( $suggestions );
		// Must be here to not mess up the sorting function
		$suggestions['**'] = 'suggestion';

		return $suggestions;
	}

	protected function formatWebSuggestions( array $queryData ): array {
		$service = $queryData['service'];
		$response = $queryData['response'];
		$sourceLanguage = $queryData['language'];
		$sourceText = $queryData['text'];

		// getResultData returns a null on failure instead of throwing an exception
		$items = $service->getResultData( $response );
		if ( $items === null ) {
			return [];
		}

		$localPrefix = Title::makeTitle( NS_MAIN, '' )->getFullURL( '', false, PROTO_CANONICAL );
		$localPrefixLength = strlen( $localPrefix );

		foreach ( $items as &$item ) {
			$local = strncmp( $item['uri'], $localPrefix, $localPrefixLength ) === 0;
			$item = array_merge( $item, [
				'service' => $service->getName(),
				'source_language' => $sourceLanguage,
				'source' => $sourceText,
				'local' => $local,
			] );

			// TtmServerActionApi expands this... need to fix it again to be the bare name
			if ( $local ) {
				$pagename = urldecode( substr( $item['location'], $localPrefixLength ) );
				$item['location'] = $pagename;
				$handle = new MessageHandle( Title::newfromText( $pagename ) );
				$item['editorUrl'] = TranslateUtils::getEditorUrl( $handle );
			}
		}
		return $items;
	}

	protected function formatInternalSuggestions(
		array $queryData,
		ReadableTTMServer $s,
		string $serviceName,
		string $sourceLanguage
	): array {
		$items = [];

		foreach ( $queryData as $item ) {
			$local = $s->isLocalSuggestion( $item );

			$item['service'] = $serviceName;
			$item['source_language'] = $sourceLanguage;
			$item['local'] = $local;
			// Likely only needed for non-public DatabaseTTMServer
			$item['uri'] = $item['uri'] ?? $s->expandLocation( $item );
			if ( $local ) {
				$handle = new MessageHandle( Title::newfromText( $item[ 'location' ] ) );
				$item['editorUrl'] = TranslateUtils::getEditorUrl( $handle );
			}
			$items[] = $item;
		}

		return $items;
	}

	/** @return ReadableTTMServer[] */
	private function getInternalServices(): array {
		$services = $this->getQueryableServices();
		foreach ( $services as $name => $config ) {
			if ( $config['type'] === 'ttmserver' ) {
				$services[$name] = $this->ttmServerFactory->create( $name );
			} else {
				unset( $services[$name] );
			}
		}

		return $services;
	}

	/** @return RemoteTTMServerWebService[] */
	private function getWebServices(): array {
		$services = $this->getQueryableServices();
		foreach ( $services as $name => $config ) {
			if ( $config['type'] === 'remote-ttmserver' ) {
				$services[$name] = TranslationWebService::factory( $name, $config );
			} else {
				unset( $services[$name] );
			}
		}

		return $services;
	}

	private function getQueryableServices(): array {
		if ( !$this->services ) {
			global $wgTranslateTranslationServices;
			$this->services = $this->getQueryableServicesUncached(
				$wgTranslateTranslationServices );
		}

		return $this->services;
	}

	private function getQueryableServicesUncached( array $services ): array {
		// First remove mirrors of the primary service
		$primary = $this->ttmServerFactory->getDefault();
		$mirrors = $primary->getMirrors();
		foreach ( $mirrors as $mirrorName ) {
			unset( $services[$mirrorName] );
		}

		// Then remove non-ttmservers
		foreach ( $services as $name => $config ) {
			$type = $config['type'];
			if ( $type !== 'ttmserver' && $type !== 'remote-ttmserver' ) {
				unset( $services[$name] );
			}
		}

		// Then determine the query method. Prefer HTTP queries that can be run parallel.
		foreach ( $services as $name => &$config ) {
			$public = $config['public'] ?? false;
			if ( $config['type'] === 'ttmserver' && $public ) {
				$config['type'] = 'remote-ttmserver';
				$config['service'] = $name;
				$config['url'] = wfExpandUrl( wfScript( 'api' ), PROTO_CANONICAL );
			}
		}

		return $services;
	}
}