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