21 private $ttmServerFactory;
25 private const CONSTRUCTOR_OPTIONS = [
27 'TranslateTranslationDefaultService',
28 'TranslateTranslationServices',
31 public function __construct(
37 parent::__construct( $mainModule, $moduleName );
38 $this->ttmServerFactory = $ttmServerFactory;
39 $this->options =
new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $config );
42 public function execute():
void {
43 if ( !$this->getAvailableTranslationServices() ) {
44 $this->dieWithError(
'apierror-translate-notranslationservices' );
47 $params = $this->extractRequestParams();
49 $server = $this->ttmServerFactory->create( $params[
'service' ] );
51 $this->dieWithError(
'apierror-translate-notranslationservices' );
54 $suggestions = $server->query(
55 $params[
'sourcelanguage'],
56 $params[
'targetlanguage'],
60 $result = $this->getResult();
61 foreach ( $suggestions as $sug ) {
62 $sug[
'location'] = $server->expandLocation( $sug );
63 unset( $sug[
'wiki'] );
64 $result->addValue( $this->getModuleName(),
null, $sug );
67 $result->addIndexedTagName( $this->getModuleName(),
'suggestion' );
70 private function getAvailableTranslationServices(): array {
71 $translationServices = $this->options->get(
'TranslateTranslationServices' );
74 foreach ( $translationServices as $id => $config ) {
75 $public = $config[
'public'] ??
false;
76 if ( $config[
'type'] ===
'ttmserver' && $public ) {
84 protected function getAllowedParams(): array {
85 $available = $this->getAvailableTranslationServices();
89 ParamValidator::PARAM_TYPE => $available,
92 ParamValidator::PARAM_TYPE =>
'string',
93 ParamValidator::PARAM_REQUIRED =>
true,
96 ParamValidator::PARAM_TYPE =>
'string',
97 ParamValidator::PARAM_REQUIRED =>
true,
100 ParamValidator::PARAM_TYPE =>
'string',
101 ParamValidator::PARAM_REQUIRED =>
true,
108 $ret[
'service'][ParamValidator::PARAM_DEFAULT] =
109 $this->options->get(
'TranslateTranslationDefaultService' );
115 protected function getExamplesMessages(): array {
117 'action=ttmserver&sourcelanguage=en&targetlanguage=fi&text=Help'
118 =>
'apihelp-ttmserver-example-1',