31 private $ttmServerFactory;
33 public function __construct(
36 IContextSource $context,
39 parent::__construct( $group, $handle, $context, $dataProvider );
40 $this->ttmServerFactory = Services::getInstance()->getTtmServerFactory();
43 public function populateQueries():
void {
44 $text = $this->dataProvider->getDefinition();
45 $from = $this->group->getSourceLanguage();
46 $to = $this->handle->getCode();
48 if ( trim( $text ) ===
'' ) {
52 foreach ( $this->getWebServices() as $service ) {
53 $this->
storeQuery( $service, $from, $to, $text );
58 $text = $this->dataProvider->getDefinition();
59 if ( trim( $text ) ===
'' ) {
64 $from = $this->group->getSourceLanguage();
65 $to = $this->handle->getCode();
67 foreach ( $this->getInternalServices() as $name => $service ) {
69 $queryData = $service->query( $from, $to, $text );
72 }
catch ( Exception $e ) {
77 $sugs = $this->formatInternalSuggestions( $queryData, $service, $name, $from );
78 $suggestions = array_merge( $suggestions, $sugs );
83 $sugs = $this->formatWebSuggestions( $queryData );
84 $suggestions = array_merge( $suggestions, $sugs );
89 $suggestions[
'**'] =
'suggestion';
94 protected function formatWebSuggestions( array $queryData ): array {
95 $service = $queryData[
'service'];
96 $response = $queryData[
'response'];
97 $sourceLanguage = $queryData[
'language'];
98 $sourceText = $queryData[
'text'];
101 $items = $service->getResultData( $response );
102 if ( $items ===
null ) {
106 $localPrefix = Title::makeTitle( NS_MAIN,
'' )->getFullURL(
'',
false, PROTO_CANONICAL );
107 $localPrefixLength = strlen( $localPrefix );
109 foreach ( $items as &$item ) {
110 $local = strncmp( $item[
'uri'], $localPrefix, $localPrefixLength ) === 0;
111 $item = array_merge( $item, [
112 'service' => $service->getName(),
113 'source_language' => $sourceLanguage,
114 'source' => $sourceText,
120 $pagename = urldecode( substr( $item[
'location'], $localPrefixLength ) );
121 $item[
'location'] = $pagename;
122 $handle =
new MessageHandle( Title::newFromText( $pagename ) );
123 $item[
'editorUrl'] = Utilities::getEditorUrl( $handle );
129 protected function formatInternalSuggestions(
133 string $sourceLanguage
137 foreach ( $queryData as $item ) {
140 $item[
'service'] = $serviceName;
141 $item[
'source_language'] = $sourceLanguage;
142 $item[
'local'] = $local;
146 $handle =
new MessageHandle( Title::newFromText( $item[
'location' ] ) );
147 $item[
'editorUrl'] = Utilities::getEditorUrl( $handle );
156 private function getInternalServices(): array {
157 $services = $this->getQueryableServices();
158 foreach ( $services as $name => $config ) {
159 if ( $config[
'type'] ===
'ttmserver' ) {
160 $services[$name] = $this->ttmServerFactory->create( $name );
162 unset( $services[$name] );
170 private function getWebServices(): array {
171 $services = $this->getQueryableServices();
172 foreach ( $services as $name => $config ) {
173 if ( $config[
'type'] ===
'remote-ttmserver' ) {
174 $services[$name] = TranslationWebService::factory( $name, $config );
176 unset( $services[$name] );
183 private function getQueryableServices(): array {
184 if ( !$this->services ) {
185 global $wgTranslateTranslationServices;
186 $this->services = $this->getQueryableServicesUncached(
187 $wgTranslateTranslationServices );
190 return $this->services;
193 private function getQueryableServicesUncached( array $services ): array {
195 $primary = $this->ttmServerFactory->getDefault();
196 $mirrors = $primary->getMirrors();
197 foreach ( $mirrors as $mirrorName ) {
198 unset( $services[$mirrorName] );
202 foreach ( $services as $name => $config ) {
203 $type = $config[
'type'];
204 if ( $type !==
'ttmserver' && $type !==
'remote-ttmserver' ) {
205 unset( $services[$name] );
210 foreach ( $services as $name => &$config ) {
211 $public = $config[
'public'] ??
false;
212 if ( $config[
'type'] ===
'ttmserver' && $public ) {
213 $config[
'type'] =
'remote-ttmserver';
214 $config[
'service'] = $name;
215 $config[
'url'] = wfExpandUrl( wfScript(
'api' ), PROTO_CANONICAL );