21 private $ttmServerFactory;
25 private const CONSTRUCTOR_OPTIONS = [
27 'TranslateTranslationDefaultService',
28 'TranslateTranslationServices',
31 public function __construct(
37 parent::__construct( $main, $moduleName );
38 $this->options =
new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $config );
39 $this->ttmServerFactory = $ttmServerFactory;
42 public function execute():
void {
43 if ( !$this->getSearchableTtmServers() ) {
44 $this->dieWithError(
'apierror-translate-notranslationservices' );
47 $params = $this->extractRequestParams();
49 $server = $this->ttmServerFactory->create( $params[
'service' ] );
51 $this->dieWithError(
'apierror-translate-notranslationservices' );
54 $result = $this->getResult();
56 if ( $params[
'filter'] !==
'' ) {
58 $documents = $translationSearch->getDocuments();
59 $total = $translationSearch->getTotalHits();
61 $searchResults = $server->search(
66 $documents = $server->getDocuments( $searchResults );
67 $total = $server->getTotalHits( $searchResults );
69 $result->addValue( [
'search',
'metadata' ],
'total', $total );
70 $result->addValue(
'search',
'translations', $documents );
74 private function getSearchableTtmServers(): array {
75 $ttmServiceIds = $this->ttmServerFactory->getNames();
78 foreach ( $ttmServiceIds as $serviceId ) {
79 $ttmServer = $this->ttmServerFactory->create( $serviceId );
88 protected function getAllowedFilters(): array {
97 protected function getAllowedParams(): array {
98 $available = $this->getSearchableTtmServers();
100 $filters = $this->getAllowedFilters();
104 ParamValidator::PARAM_TYPE => $available,
107 ParamValidator::PARAM_TYPE =>
'string',
108 ParamValidator::PARAM_REQUIRED =>
true,
110 'sourcelanguage' => [
111 ParamValidator::PARAM_TYPE =>
'string',
112 ParamValidator::PARAM_DEFAULT => $this->options->get(
'LanguageCode' ),
115 ParamValidator::PARAM_TYPE =>
'string',
116 ParamValidator::PARAM_DEFAULT =>
'',
119 ParamValidator::PARAM_TYPE =>
'string',
120 ParamValidator::PARAM_DEFAULT =>
'',
123 ParamValidator::PARAM_TYPE => $filters,
124 ParamValidator::PARAM_DEFAULT =>
'',
127 ParamValidator::PARAM_TYPE =>
'string',
128 ParamValidator::PARAM_DEFAULT =>
'',
131 ParamValidator::PARAM_TYPE =>
'string',
132 ParamValidator::PARAM_DEFAULT =>
'0',
135 ParamValidator::PARAM_TYPE =>
'integer',
136 ParamValidator::PARAM_DEFAULT => 0,
139 ParamValidator::PARAM_DEFAULT => 25,
140 ParamValidator::PARAM_TYPE =>
'limit',
141 IntegerDef::PARAM_MIN => 1,
142 IntegerDef::PARAM_MAX => ApiBase::LIMIT_SML1,
143 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_SML2
150 $ret[
'service'][ParamValidator::PARAM_DEFAULT] =
151 $this->options->get(
'TranslateTranslationDefaultService' );
157 protected function getExamplesMessages(): array {
159 'action=searchtranslations&language=fr&query=aide'
160 =>
'apihelp-searchtranslations-example-1',
161 'action=searchtranslations&language=fr&query=edit&filter=untranslated'
162 =>
'apihelp-searchtranslations-example-2',