summaryrefslogtreecommitdiff
blob: 1992367d79dbe1bf5884c9c63b68ed23f0f6b00c (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
<?php
/**
 * Translation aid provider.
 *
 * @file
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 */

/**
 * Translation aid which gives suggestion from translation memory.
 *
 * @ingroup TranslationAids
 * @since 2013-01-01 | 2015.02 extends QueryAggregatorAwareTranslationAid
 */
class TTMServerAid extends QueryAggregatorAwareTranslationAid {
	/** @var array[] */
	private $services;

	public function populateQueries() {
		$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() {
		$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 ( 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 ) {
		$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,
			] );

			// ApiTTMServer 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;
	}

	/**
	 * @param array[] $queryData
	 * @param ReadableTTMServer $s
	 * @param string $serviceName
	 * @param string $sourceLanguage
	 * @return array[]
	 */
	protected function formatInternalSuggestions(
		array $queryData, ReadableTTMServer $s, $serviceName, $sourceLanguage
	) {
		$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] = TTMServer::factory( $config );
			} 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 = TTMServer::primary();
		$mirrors = $primary ? $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;
	}
}