Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
QueryAggregator | |
0.00% |
0 / 29 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
addQuery | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getResponse | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
run | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
getMultiHttpQueries | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\WebService; |
5 | |
6 | use MediaWiki\Extension\Translate\Utilities\Utilities; |
7 | use MediaWiki\MediaWikiServices; |
8 | use RuntimeException; |
9 | |
10 | /** |
11 | * Web service utility class. Runs multiple web service queries asynchronously to save time. |
12 | * @author Niklas Laxström |
13 | * @license GPL-2.0-or-later |
14 | * @since 2015.02 |
15 | * @ingroup TranslationWebService |
16 | */ |
17 | class QueryAggregator { |
18 | protected array $queries = []; |
19 | protected array $responses = []; |
20 | protected float $timeout = 0; |
21 | protected bool $hasRun = false; |
22 | |
23 | /** |
24 | * Register a query to be run. |
25 | * @return mixed Query id that can be used to fetch results. |
26 | */ |
27 | public function addQuery( TranslationQuery $query ) { |
28 | $this->queries[] = $query; |
29 | |
30 | $this->timeout = max( $query->getTimeout(), $this->timeout ); |
31 | return count( $this->queries ) - 1; |
32 | } |
33 | |
34 | /** |
35 | * Returns a response for a query. |
36 | * @param mixed $id Query id. |
37 | * @throws RuntimeException if called before run() has been called. |
38 | */ |
39 | public function getResponse( $id ): TranslationQueryResponse { |
40 | if ( !$this->hasRun ) { |
41 | throw new RuntimeException( 'Tried to get response before queries ran' ); |
42 | } |
43 | |
44 | return new TranslationQueryResponse( $this->responses[$id], $this->queries[$id] ); |
45 | } |
46 | |
47 | /** Runs all the queries. */ |
48 | public function run(): void { |
49 | global $wgSitename; |
50 | |
51 | $version = Utilities::getVersion(); |
52 | |
53 | $clientOptions = [ |
54 | 'reqTimeout' => $this->timeout, |
55 | 'connTimeout' => 3, |
56 | 'userAgent' => "MediaWiki Translate extension $version for $wgSitename" |
57 | ]; |
58 | |
59 | $httpRequestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory(); |
60 | |
61 | $http = $httpRequestFactory->createMultiClient( $clientOptions ); |
62 | $responses = $http->runMulti( $this->getMultiHttpQueries( $this->queries ) ); |
63 | foreach ( $responses as $index => $response ) { |
64 | $this->responses[$index] = $response; |
65 | } |
66 | $this->hasRun = true; |
67 | } |
68 | |
69 | /** |
70 | * Formats queries for format used by MultiHttpClient class. |
71 | * @param TranslationQuery[] $queries |
72 | * @return array[] |
73 | */ |
74 | protected function getMultiHttpQueries( array $queries ): array { |
75 | $converter = static function ( TranslationQuery $q ) { |
76 | return [ |
77 | 'url' => $q->getUrl(), |
78 | 'method' => $q->getMethod(), |
79 | 'query' => $q->getQueryParameters(), |
80 | 'body' => $q->getBody(), |
81 | 'headers' => $q->getHeaders(), |
82 | ]; |
83 | }; |
84 | |
85 | return array_map( $converter, $queries ); |
86 | } |
87 | } |