22 private $ttmServerFactory;
26 private const CONSTRUCTOR_OPTIONS = [
28 'TranslateTranslationDefaultService',
29 'TranslateTranslationServices',
32 public function __construct(
38 parent::__construct( $mainModule, $moduleName );
39 $this->ttmServerFactory = $ttmServerFactory;
40 $this->options =
new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $config );
43 public function execute():
void {
44 if ( !$this->getAvailableTranslationServices() ) {
45 $this->dieWithError(
'apierror-translate-notranslationservices' );
48 $params = $this->extractRequestParams();
50 $server = $this->ttmServerFactory->create( $params[
'service' ] );
52 $this->dieWithError(
'apierror-translate-notranslationservices' );
55 $suggestions = $server->query(
56 $params[
'sourcelanguage'],
57 $params[
'targetlanguage'],
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 );
68 $result->addIndexedTagName( $this->getModuleName(),
'suggestion' );
71 private function getAvailableTranslationServices(): array {
72 $translationServices = $this->options->get(
'TranslateTranslationServices' );
75 foreach ( $translationServices as $id => $config ) {
76 $public = $config[
'public'] ??
false;
77 if ( $config[
'type'] ===
'ttmserver' && $public ) {
85 protected function getAllowedParams(): array {
86 $available = $this->getAvailableTranslationServices();
90 ParamValidator::PARAM_TYPE => $available,
93 ParamValidator::PARAM_TYPE =>
'string',
94 ParamValidator::PARAM_REQUIRED =>
true,
97 ParamValidator::PARAM_TYPE =>
'string',
98 ParamValidator::PARAM_REQUIRED =>
true,
101 ParamValidator::PARAM_TYPE =>
'string',
102 ParamValidator::PARAM_REQUIRED =>
true,
109 $ret[
'service'][ParamValidator::PARAM_DEFAULT] =
110 $this->options->get(
'TranslateTranslationDefaultService' );
116 protected function getExamplesMessages(): array {
118 'action=ttmserver&sourcelanguage=en&targetlanguage=fi&text=Help'
119 =>
'apihelp-ttmserver-example-1',