-
Notifications
You must be signed in to change notification settings - Fork 10
/
ApiQueryContentTranslation.php
228 lines (199 loc) · 6.68 KB
/
ApiQueryContentTranslation.php
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* Api module for querying Content translations.
*
* @copyright See AUTHORS.txt
* @license GPL-2.0-or-later
*/
use ContentTranslation\CorporaLookup;
use ContentTranslation\Database;
use ContentTranslation\Translation;
use ContentTranslation\TranslationWork;
use ContentTranslation\Translator;
/**
* Api module for querying ContentTranslation.
*/
class ApiQueryContentTranslation extends ApiQueryGeneratorBase {
public function __construct( $query, $moduleName ) {
parent::__construct( $query, $moduleName );
}
public function execute() {
$this->run();
}
public function executeGenerator( $resultPageSet ) {
$this->run( $resultPageSet );
}
/**
* @param ApiPageSet $resultPageSet
*/
private function run( $resultPageSet = null ) {
$params = $this->extractRequestParams();
$result = $this->getResult();
$user = $this->getUser();
// Case A: Find a translation for given work from anonymous context
if ( $user->isAnon() ) {
if ( $params['translationid'] ) {
$this->dieWithError( 'apierror-cx-mustbeloggedin-viewtranslations', 'notloggedin' );
}
if ( $params['sourcetitle'] && $params['from'] && $params['to'] ) {
$translation = Translation::find(
$params['from'], $params['to'], $params[ 'sourcetitle' ]
);
$result->addValue(
[ 'query', 'contenttranslation' ],
'translation',
$translation->translation
);
}
return;
}
$translator = new Translator( $user );
// Case B: Find a translation for given work for the current user.
if ( $params['sourcetitle'] && $params['from'] && $params['to'] ) {
$work = new TranslationWork( $params['sourcetitle'], $params['from'], $params['to'] );
$this->find( $work, $translator );
return;
}
// Case C: Find a translation for given id
if ( $params['translationid'] ) {
$translation = $translator->getTranslation( $params['translationid'] );
if ( $translation !== null ) {
$unitsAndCategories = $this->getTranslationUnitsAndCategories( $translation );
$translation->translation['translationUnits'] = $unitsAndCategories['sections'];
// Only target categories are fetched when translation draft is restored
$translation->translation['targetCategories'] = $unitsAndCategories['categories'];
$result->addValue(
[ 'query', 'contenttranslation' ],
'translation',
$translation->translation
);
} else {
$this->dieWithError( 'apierror-cx-missingdraft', 'missingdraft' );
}
return;
}
// Case D: Find list of translations
$translations = $translator->getAllTranslations(
$params['limit'],
$params['offset'],
$params['type'],
$params['from'],
$params['to']
);
// We will have extra continue in case the last batch is exactly the size of the limit
$count = count( $translations );
if ( $count === $params['limit'] ) {
$offset = $translations[$count - 1]->translation['lastUpdateTimestamp'];
$this->setContinueEnumParameter( 'offset', $offset );
}
$result->addValue(
[ 'query', 'contenttranslation' ],
'translations',
$translations
);
// Simple optimization
if ( $params['offset'] === null ) {
$result->addValue(
[ 'query', 'contenttranslation' ],
'languages',
$translator->getLanguages( $params['type'] )
);
}
}
/**
* Find a translation with any status for the given language pair and title.
* @param TranslationWork $work
* @param Translator $translator
*/
public function find( TranslationWork $work, Translator $translator ) {
$translation = null;
$result = $this->getResult();
$translation = Translation::findForTranslator( $work, $translator );
// Check for other drafts. If one exists, return that to the UI which will then
// know to display an error to the user because we disallow two users to start
// drafts on the same translation work.
if ( $translation === null ) {
$translations = Translation::getConflictingTranslations( $work );
if ( $translations !== [] ) {
// Take the first one due to UI limitations
$translation = array_shift( $translations );
}
}
if ( $translation === null ) {
// Return empty result. The UI will treat it as a new translation.
return;
}
// Add name and gender information to the returned result. The UI can use this
// to display the conflict message.
$globalUserId = $translation->getData()['lastUpdatedTranslator'];
$centralIdLookup = CentralIdLookup::factory();
// $user can be null, but should never happen in our case because
// we redirect translators to the target wiki, and they cannot do
// translations without logging in.
$user = $centralIdLookup->localUserFromCentralId( $globalUserId );
$gender = GenderCache::singleton()->getGenderOf( $user, __METHOD__ );
$translation->translation['translatorName'] = $user->getName();
$translation->translation['translatorGender'] = $gender;
$result->addValue(
[ 'query', 'contenttranslation' ],
'translation',
$translation->translation
);
}
/**
* Retrieve translation units and target categories. Only target categories are fetched
* when translation draft is restored. Source categories are saved into cx_corpora table for
* pairing with target categories, but not retrieved when translation draft is restored.
*
* @param Translation $translation
* @return array Associative array with 'translationUnits' and 'categories' data
*/
protected function getTranslationUnitsAndCategories( Translation $translation ) {
$db = Database::getConnection( DB_REPLICA );
$lookup = new CorporaLookup( $db );
$content = $lookup->getByTranslationId( $translation->getTranslationId() );
return $content;
}
public function getAllowedParams() {
$allowedParams = [
'translationid' => [
ApiBase::PARAM_TYPE => 'string',
],
'from' => [
ApiBase::PARAM_TYPE => 'string',
],
'to' => [
ApiBase::PARAM_TYPE => 'string',
],
'sourcetitle' => [
ApiBase::PARAM_TYPE => 'string',
],
'limit' => [
ApiBase::PARAM_DFLT => 100,
ApiBase::PARAM_TYPE => 'limit',
ApiBase::PARAM_MIN => 1,
ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
],
'offset' => [
ApiBase::PARAM_DFLT => null,
ApiBase::PARAM_TYPE => 'string',
],
'type' => [
ApiBase::PARAM_DFLT => null,
ApiBase::PARAM_TYPE => [ 'draft', 'published' ],
],
];
return $allowedParams;
}
protected function getExamplesMessages() {
return [
'action=query&list=contenttranslation' =>
'apihelp-query+contenttranslation-example-1',
'action=query&list=contenttranslation&translationid=94' =>
'apihelp-query+contenttranslation-example-2',
'action=query&list=contenttranslation&from=en&to=es&sourcetitle=Hibiscus' =>
'apihelp-query+contenttranslation-example-3',
];
}
}