summaryrefslogtreecommitdiff
blob: 9a5dca2a6cb35fa459d369030767e554895f2f9a (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
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid;

use QueryAggregator;
use QueryAggregatorAware;
use TranslationWebService;

/**
 * Helper class for translation aids that use web services.
 * @ingroup TranslationAids
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @since 2015.02
 */
abstract class QueryAggregatorAwareTranslationAid
	extends TranslationAid
	implements QueryAggregatorAware
{
	private $queries = [];
	/** @var QueryAggregator */
	private $aggregator;

	public function setQueryAggregator( QueryAggregator $aggregator ): void {
		$this->aggregator = $aggregator;
	}

	/**
	 * Stores a web service query for later execution.
	 * @param TranslationWebService $service
	 * @param string $from
	 * @param string $to
	 * @param string $text
	 * @return void
	 */
	protected function storeQuery(
		TranslationWebService $service,
		string $from,
		string $to,
		string $text
	): void {
		$queries = $service->getQueries( $text, $from, $to );
		foreach ( $queries as $query ) {
			$this->queries[] = [
				'id' => $this->aggregator->addQuery( $query ),
				'language' => $from,
				'text' => $text,
				'service' => $service,
			];
		}
	}

	/**
	 * Returns all stored queries.
	 * @return array Map of executed queries:
	 *  - language: string: source language
	 *  - text: string: source text
	 *  - response: TranslationQueryResponse
	 */
	protected function getQueryData(): array {
		foreach ( $this->queries as &$queryData ) {
			$queryData['response'] = $this->aggregator->getResponse( $queryData['id'] );
			unset( $queryData['id'] );
		}

		return $this->queries;
	}
}