summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/translationaids/QueryAggregatorAwareTranslationAid.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/Translate/translationaids/QueryAggregatorAwareTranslationAid.php')
-rw-r--r--www/wiki/extensions/Translate/translationaids/QueryAggregatorAwareTranslationAid.php83
1 files changed, 83 insertions, 0 deletions
diff --git a/www/wiki/extensions/Translate/translationaids/QueryAggregatorAwareTranslationAid.php b/www/wiki/extensions/Translate/translationaids/QueryAggregatorAwareTranslationAid.php
new file mode 100644
index 00000000..11358315
--- /dev/null
+++ b/www/wiki/extensions/Translate/translationaids/QueryAggregatorAwareTranslationAid.php
@@ -0,0 +1,83 @@
+<?php
+/**
+ * Translation aid helper class.
+ *
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0-or-later
+ */
+
+/**
+ * Helper class for translation aids which use web services.
+ *
+ * @ingroup TranslationAids
+ * @since 2015.02
+ */
+abstract class QueryAggregatorAwareTranslationAid
+ extends TranslationAid
+ implements QueryAggregatorAware
+{
+ private $queries = [];
+ private $aggregator;
+
+ // Interface: QueryAggregatorAware
+ public function setQueryAggregator( QueryAggregator $aggregator ) {
+ $this->aggregator = $aggregator;
+ }
+
+ /**
+ * Stores a web service query for later execution.
+ * @param TranslationWebService $service
+ * @param string $from Source language
+ * @param string $to Target language
+ * @param string $text Source text
+ */
+ protected function storeQuery( TranslationWebService $service, $from, $to, $text ) {
+ $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() {
+ foreach ( $this->queries as &$queryData ) {
+ $queryData['response'] = $this->aggregator->getResponse( $queryData['id'] );
+ unset( $queryData['id'] );
+ }
+
+ return $this->queries;
+ }
+
+ /**
+ * Returns all web services of given type.
+ * @param string $type
+ * @return TranslationWebService[]
+ */
+ protected function getWebServices( $type ) {
+ global $wgTranslateTranslationServices;
+
+ $services = [];
+ foreach ( $wgTranslateTranslationServices as $name => $config ) {
+ $service = TranslationWebService::factory( $name, $config );
+ if ( !$service || $service->getType() !== $type ) {
+ continue;
+ }
+
+ $services[$name] = $service;
+ }
+
+ return $services;
+ }
+}