Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
QueryAggregator.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\WebService;
5
7use MediaWiki\MediaWikiServices;
8use RuntimeException;
9
18 protected array $queries = [];
19 protected array $responses = [];
20 protected float $timeout = 0;
21 protected bool $hasRun = false;
22
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
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
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
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}
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31
addQuery(TranslationQuery $query)
Register a query to be run.
getMultiHttpQueries(array $queries)
Formats queries for format used by MultiHttpClient class.
Value object that represents a HTTP(S) query response.
Mutable objects that represents an HTTP(S) query.