1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
<?php
/**
* API module for search translations
* @since 2015.07
* @license GPL-2.0-or-later
*/
class ApiSearchTranslations extends ApiBase {
public function execute() {
global $wgTranslateTranslationServices;
if ( !$this->getAvailableTranslationServices() ) {
$this->dieWithError( 'apierror-translate-notranslationservices' );
}
$params = $this->extractRequestParams();
$config = $wgTranslateTranslationServices[$params['service']];
/** @var SearchableTTMServer $server */
$server = TTMServer::factory( $config );
'@phan-var SearchableTTMServer $server';
$result = $this->getResult();
if ( $params['filter'] !== '' ) {
$translationSearch = new CrossLanguageTranslationSearchQuery( $params, $server );
$documents = $translationSearch->getDocuments();
$total = $translationSearch->getTotalHits();
} else {
$searchResults = $server->search(
$params['query'],
$params,
[ '', '' ]
);
$documents = $server->getDocuments( $searchResults );
$total = $server->getTotalHits( $searchResults );
}
$result->addValue( [ 'search', 'metadata' ], 'total', $total );
$result->addValue( 'search', 'translations', $documents );
}
protected function getAvailableTranslationServices() {
global $wgTranslateTranslationServices;
$good = [];
foreach ( $wgTranslateTranslationServices as $id => $config ) {
if ( TTMServer::factory( $config ) instanceof SearchableTTMServer ) {
$good[] = $id;
}
}
return $good;
}
protected function getAllowedFilters() {
return [
'',
'translated',
'fuzzy',
'untranslated'
];
}
public function getAllowedParams() {
global $wgLanguageCode,
$wgTranslateTranslationDefaultService;
$available = $this->getAvailableTranslationServices();
$filters = $this->getAllowedFilters();
$ret = [
'service' => [
ApiBase::PARAM_TYPE => $available,
],
'query' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
'sourcelanguage' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_DFLT => $wgLanguageCode,
],
'language' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_DFLT => '',
],
'group' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_DFLT => '',
],
'filter' => [
ApiBase::PARAM_TYPE => $filters,
ApiBase::PARAM_DFLT => '',
],
'match' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_DFLT => '',
],
'case' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_DFLT => '0',
],
'offset' => [
ApiBase::PARAM_TYPE => 'integer',
ApiBase::PARAM_DFLT => 0,
],
'limit' => [
ApiBase::PARAM_DFLT => 25,
ApiBase::PARAM_TYPE => 'limit',
ApiBase::PARAM_MIN => 1,
ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2
],
];
if ( $available ) {
// Don't add this if no services are available, it makes
// ApiStructureTest unhappy
$ret['service'][ApiBase::PARAM_DFLT] = $wgTranslateTranslationDefaultService;
}
return $ret;
}
protected function getExamplesMessages() {
return [
'action=searchtranslations&language=fr&query=aide'
=> 'apihelp-searchtranslations-example-1',
'action=searchtranslations&language=fr&query=edit&filter=untranslated'
=> 'apihelp-searchtranslations-example-2',
];
}
}
|