32 private $ttmServerFactory;
34 public function __construct(
37 IContextSource $context,
40 parent::__construct( $group, $handle, $context, $dataProvider );
41 $this->ttmServerFactory = Services::getInstance()->getTtmServerFactory();
44 public function populateQueries():
void {
45 $text = $this->dataProvider->getDefinition();
46 $from = $this->group->getSourceLanguage();
47 $to = $this->handle->getCode();
49 if ( trim( $text ) ===
'' ) {
53 foreach ( $this->getWebServices() as $service ) {
54 $this->
storeQuery( $service, $from, $to, $text );
59 $text = $this->dataProvider->getDefinition();
60 if ( trim( $text ) ===
'' ) {
65 $from = $this->group->getSourceLanguage();
66 $to = $this->handle->getCode();
68 foreach ( $this->getInternalServices() as $name => $service ) {
70 $queryData = $service->query( $from, $to, $text );
73 }
catch ( Exception $e ) {
78 $sugs = $this->formatInternalSuggestions( $queryData, $service, $name, $from );
79 $suggestions = array_merge( $suggestions, $sugs );
84 $sugs = $this->formatWebSuggestions( $queryData );
85 $suggestions = array_merge( $suggestions, $sugs );
90 $suggestions[
'**'] =
'suggestion';
95 protected function formatWebSuggestions( array $queryData ): array {
96 $service = $queryData[
'service'];
97 $response = $queryData[
'response'];
98 $sourceLanguage = $queryData[
'language'];
99 $sourceText = $queryData[
'text'];
102 $items = $service->getResultData( $response );
103 if ( $items ===
null ) {
107 $localPrefix = Title::makeTitle( NS_MAIN,
'' )->getFullURL(
'',
false, PROTO_CANONICAL );
108 $localPrefixLength = strlen( $localPrefix );
110 foreach ( $items as &$item ) {
111 $local = strncmp( $item[
'uri'], $localPrefix, $localPrefixLength ) === 0;
112 $item = array_merge( $item, [
113 'service' => $service->getName(),
114 'source_language' => $sourceLanguage,
115 'source' => $sourceText,
121 $pagename = urldecode( substr( $item[
'location'], $localPrefixLength ) );
122 $item[
'location'] = $pagename;
123 $handle =
new MessageHandle( Title::newFromText( $pagename ) );
124 $item[
'editorUrl'] = Utilities::getEditorUrl( $handle );
130 protected function formatInternalSuggestions(
132 ReadableTtmServer $s,
134 string $sourceLanguage
138 foreach ( $queryData as $item ) {
139 $local = $s->isLocalSuggestion( $item );
141 $item[
'service'] = $serviceName;
142 $item[
'source_language'] = $sourceLanguage;
143 $item[
'local'] = $local;
145 $item[
'uri'] = $item[
'uri'] ?? $s->expandLocation( $item );
147 $handle =
new MessageHandle( Title::newFromText( $item[
'location' ] ) );
148 $item[
'editorUrl'] = Utilities::getEditorUrl( $handle );
157 private function getInternalServices(): array {
158 $services = $this->getQueryableServices();
159 foreach ( $services as $name => $config ) {
160 if ( $config[
'type'] ===
'ttmserver' ) {
161 $services[$name] = $this->ttmServerFactory->create( $name );
163 unset( $services[$name] );
171 private function getWebServices(): array {
172 $services = $this->getQueryableServices();
173 foreach ( $services as $name => $config ) {
174 if ( $config[
'type'] ===
'remote-ttmserver' ) {
175 $services[$name] = TranslationWebService::factory( $name, $config );
177 unset( $services[$name] );
184 private function getQueryableServices(): array {
185 if ( !$this->services ) {
186 global $wgTranslateTranslationServices;
187 $this->services = $this->getQueryableServicesUncached(
188 $wgTranslateTranslationServices );
191 return $this->services;
194 private function getQueryableServicesUncached( array $services ): array {
196 $primary = $this->ttmServerFactory->getDefaultForQuerying();
197 if ( $primary instanceof WritableTtmServer ) {
198 $mirrors = $primary->getMirrors();
199 foreach ( $mirrors as $mirrorName ) {
200 unset( $services[$mirrorName] );
205 $writableServices = $this->ttmServerFactory->getWriteOnly();
206 foreach ( array_keys( $writableServices ) as $serviceId ) {
207 unset( $services[ $serviceId ] );
211 foreach ( $services as $name => $config ) {
212 $type = $config[
'type'];
213 if ( $type !==
'ttmserver' && $type !==
'remote-ttmserver' ) {
214 unset( $services[$name] );
219 foreach ( $services as $name => &$config ) {
220 $public = $config[
'public'] ??
false;
221 if ( $config[
'type'] ===
'ttmserver' && $public ) {
222 $config[
'type'] =
'remote-ttmserver';
223 $config[
'service'] = $name;
224 $config[
'url'] = wfExpandUrl( wfScript(
'api' ), PROTO_CANONICAL );