Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TtmServerActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TtmServer;
5
6use ApiBase;
7use ApiMain;
8use Config;
9use MediaWiki\Config\ServiceOptions;
11use Wikimedia\ParamValidator\ParamValidator;
12
20class TtmServerActionApi extends ApiBase {
22 private $ttmServerFactory;
24 private $options;
25
26 private const CONSTRUCTOR_OPTIONS = [
27 'LanguageCode',
28 'TranslateTranslationDefaultService',
29 'TranslateTranslationServices',
30 ];
31
32 public function __construct(
33 ApiMain $mainModule,
34 string $moduleName,
35 TtmServerFactory $ttmServerFactory,
36 Config $config
37 ) {
38 parent::__construct( $mainModule, $moduleName );
39 $this->ttmServerFactory = $ttmServerFactory;
40 $this->options = new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $config );
41 }
42
43 public function execute(): void {
44 if ( !$this->getAvailableTranslationServices() ) {
45 $this->dieWithError( 'apierror-translate-notranslationservices' );
46 }
47
48 $params = $this->extractRequestParams();
49
50 $server = $this->ttmServerFactory->create( $params[ 'service' ] );
51 if ( !$server instanceof ReadableTTMServer ) {
52 $this->dieWithError( 'apierror-translate-notranslationservices' );
53 }
54
55 $suggestions = $server->query(
56 $params['sourcelanguage'],
57 $params['targetlanguage'],
58 $params['text']
59 );
60
61 $result = $this->getResult();
62 foreach ( $suggestions as $sug ) {
63 $sug['location'] = $server->expandLocation( $sug );
64 unset( $sug['wiki'] );
65 $result->addValue( $this->getModuleName(), null, $sug );
66 }
67
68 $result->addIndexedTagName( $this->getModuleName(), 'suggestion' );
69 }
70
71 private function getAvailableTranslationServices(): array {
72 $translationServices = $this->options->get( 'TranslateTranslationServices' );
73
74 $good = [];
75 foreach ( $translationServices as $id => $config ) {
76 $public = $config['public'] ?? false;
77 if ( $config['type'] === 'ttmserver' && $public ) {
78 $good[] = $id;
79 }
80 }
81
82 return $good;
83 }
84
85 protected function getAllowedParams(): array {
86 $available = $this->getAvailableTranslationServices();
87
88 $ret = [
89 'service' => [
90 ParamValidator::PARAM_TYPE => $available,
91 ],
92 'sourcelanguage' => [
93 ParamValidator::PARAM_TYPE => 'string',
94 ParamValidator::PARAM_REQUIRED => true,
95 ],
96 'targetlanguage' => [
97 ParamValidator::PARAM_TYPE => 'string',
98 ParamValidator::PARAM_REQUIRED => true,
99 ],
100 'text' => [
101 ParamValidator::PARAM_TYPE => 'string',
102 ParamValidator::PARAM_REQUIRED => true,
103 ],
104 ];
105
106 if ( $available ) {
107 // Don't add this if no services are available, it makes
108 // ApiStructureTest unhappy
109 $ret['service'][ParamValidator::PARAM_DEFAULT] =
110 $this->options->get( 'TranslateTranslationDefaultService' );
111 }
112
113 return $ret;
114 }
115
116 protected function getExamplesMessages(): array {
117 return [
118 'action=ttmserver&sourcelanguage=en&targetlanguage=fi&text=Help'
119 => 'apihelp-ttmserver-example-1',
120 ];
121 }
122}
Interface for TTMServer that can be queried (=all of them).