Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
QueryAggregatorAwareTranslationAid
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 setQueryAggregator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 storeQuery
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 getQueryData
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid;
5
6use MediaWiki\Extension\Translate\WebService\QueryAggregator;
7use MediaWiki\Extension\Translate\WebService\QueryAggregatorAware;
8use MediaWiki\Extension\Translate\WebService\TranslationWebService;
9
10/**
11 * Helper class for translation aids that use web services.
12 * @ingroup TranslationAids
13 * @author Niklas Laxström
14 * @license GPL-2.0-or-later
15 * @since 2015.02
16 */
17abstract class QueryAggregatorAwareTranslationAid
18    extends TranslationAid
19    implements QueryAggregatorAware
20{
21    /** @var array[] */
22    private $queries = [];
23    /** @var QueryAggregator */
24    private $aggregator;
25
26    public function setQueryAggregator( QueryAggregator $aggregator ): void {
27        $this->aggregator = $aggregator;
28    }
29
30    /**
31     * Stores a web service query for later execution.
32     * @param TranslationWebService $service
33     * @param string $from
34     * @param string $to
35     * @param string $text
36     * @return void
37     */
38    protected function storeQuery(
39        TranslationWebService $service,
40        string $from,
41        string $to,
42        string $text
43    ): void {
44        $queries = $service->getQueries( $text, $from, $to );
45        foreach ( $queries as $query ) {
46            $this->queries[] = [
47                'id' => $this->aggregator->addQuery( $query ),
48                'language' => $from,
49                'text' => $text,
50                'service' => $service,
51            ];
52        }
53    }
54
55    /**
56     * Returns all stored queries.
57     * @return array Map of executed queries:
58     *  - language: string: source language
59     *  - text: string: source text
60     *  - response: TranslationQueryResponse
61     */
62    protected function getQueryData(): array {
63        foreach ( $this->queries as &$queryData ) {
64            $queryData['response'] = $this->aggregator->getResponse( $queryData['id'] );
65            unset( $queryData['id'] );
66        }
67
68        return $this->queries;
69    }
70}