Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
SearchTranslationsActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TtmServer;
5
6use ApiBase;
7use ApiMain;
8use Config;
10use MediaWiki\Config\ServiceOptions;
12use Wikimedia\ParamValidator\ParamValidator;
13use Wikimedia\ParamValidator\TypeDef\IntegerDef;
14
19class SearchTranslationsActionApi extends ApiBase {
21 private $ttmServerFactory;
23 private $options;
24
25 private const CONSTRUCTOR_OPTIONS = [
26 'LanguageCode',
27 'TranslateTranslationDefaultService',
28 'TranslateTranslationServices',
29 ];
30
31 public function __construct(
32 ApiMain $main,
33 string $moduleName,
34 Config $config,
35 TtmServerFactory $ttmServerFactory
36 ) {
37 parent::__construct( $main, $moduleName );
38 $this->options = new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $config );
39 $this->ttmServerFactory = $ttmServerFactory;
40 }
41
42 public function execute(): void {
43 if ( !$this->getSearchableTtmServers() ) {
44 $this->dieWithError( 'apierror-translate-notranslationservices' );
45 }
46
47 $params = $this->extractRequestParams();
48
49 $server = $this->ttmServerFactory->create( $params[ 'service' ] );
50 if ( !$server instanceof SearchableTTMServer ) {
51 $this->dieWithError( 'apierror-translate-notranslationservices' );
52 }
53
54 $result = $this->getResult();
55
56 if ( $params['filter'] !== '' ) {
57 $translationSearch = new CrossLanguageTranslationSearchQuery( $params, $server );
58 $documents = $translationSearch->getDocuments();
59 $total = $translationSearch->getTotalHits();
60 } else {
61 $searchResults = $server->search(
62 $params['query'],
63 $params,
64 [ '', '' ]
65 );
66 $documents = $server->getDocuments( $searchResults );
67 $total = $server->getTotalHits( $searchResults );
68 }
69 $result->addValue( [ 'search', 'metadata' ], 'total', $total );
70 $result->addValue( 'search', 'translations', $documents );
71 }
72
74 private function getSearchableTtmServers(): array {
75 $ttmServiceIds = $this->ttmServerFactory->getNames();
76
77 $good = [];
78 foreach ( $ttmServiceIds as $serviceId ) {
79 $ttmServer = $this->ttmServerFactory->create( $serviceId );
80 if ( $ttmServer instanceof SearchableTTMServer ) {
81 $good[] = $serviceId;
82 }
83 }
84
85 return $good;
86 }
87
88 protected function getAllowedFilters(): array {
89 return [
90 '',
91 'translated',
92 'fuzzy',
93 'untranslated'
94 ];
95 }
96
97 protected function getAllowedParams(): array {
98 $available = $this->getSearchableTtmServers();
99
100 $filters = $this->getAllowedFilters();
101
102 $ret = [
103 'service' => [
104 ParamValidator::PARAM_TYPE => $available,
105 ],
106 'query' => [
107 ParamValidator::PARAM_TYPE => 'string',
108 ParamValidator::PARAM_REQUIRED => true,
109 ],
110 'sourcelanguage' => [
111 ParamValidator::PARAM_TYPE => 'string',
112 ParamValidator::PARAM_DEFAULT => $this->options->get( 'LanguageCode' ),
113 ],
114 'language' => [
115 ParamValidator::PARAM_TYPE => 'string',
116 ParamValidator::PARAM_DEFAULT => '',
117 ],
118 'group' => [
119 ParamValidator::PARAM_TYPE => 'string',
120 ParamValidator::PARAM_DEFAULT => '',
121 ],
122 'filter' => [
123 ParamValidator::PARAM_TYPE => $filters,
124 ParamValidator::PARAM_DEFAULT => '',
125 ],
126 'match' => [
127 ParamValidator::PARAM_TYPE => 'string',
128 ParamValidator::PARAM_DEFAULT => '',
129 ],
130 'case' => [
131 ParamValidator::PARAM_TYPE => 'string',
132 ParamValidator::PARAM_DEFAULT => '0',
133 ],
134 'offset' => [
135 ParamValidator::PARAM_TYPE => 'integer',
136 ParamValidator::PARAM_DEFAULT => 0,
137 ],
138 'limit' => [
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
144 ],
145 ];
146
147 if ( $available ) {
148 // Don't add this if no services are available, it makes
149 // ApiStructureTest unhappy
150 $ret['service'][ParamValidator::PARAM_DEFAULT] =
151 $this->options->get( 'TranslateTranslationDefaultService' );
152 }
153
154 return $ret;
155 }
156
157 protected function getExamplesMessages(): array {
158 return [
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',
163 ];
164 }
165}
API module for search translations @license GPL-2.0-or-later.
Interface for TTMServer that can act as backend for translation search.