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
|
<?php
/**
* WebAPI module for stashing translations.
*
* @file
* @author Niklas Laxström
* @license GPL-2.0-or-later
*/
/**
* WebAPI module for storing translations for users who are in a sandbox.
* Access is controlled by hooks in TranslateSandbox class.
* @since 2013.06
*/
class ApiTranslationStash extends ApiBase {
public function execute() {
$params = $this->extractRequestParams();
// The user we are operating on, not necessarly the user making the request
$user = $this->getUser();
if ( isset( $params['username'] ) ) {
if ( $this->getUser()->isAllowed( 'translate-sandboxmanage' ) ) {
$user = User::newFromName( $params['username'] );
if ( !$user ) {
$this->dieWithError( [ 'apierror-badparameter', 'username' ], 'invalidparam' );
}
} else {
$this->dieWithError( [ 'apierror-badparameter', 'username' ], 'invalidparam' );
}
}
$stash = new TranslationStashStorage( wfGetDB( DB_MASTER ) );
$action = $params['subaction'];
if ( $action === 'add' ) {
if ( !isset( $params['title'] ) ) {
$this->dieWithError( [ 'apierror-missingparam', 'title' ] );
}
if ( !isset( $params['translation'] ) ) {
$this->dieWithError( [ 'apierror-missingparam', 'translation' ] );
}
// @todo: Return value of Title::newFromText not checked
$translation = new StashedTranslation(
$user,
Title::newFromText( $params['title'] ),
$params['translation'],
FormatJson::decode( $params['metadata'], true )
);
$stash->addTranslation( $translation );
}
$output = [];
if ( $action === 'query' ) {
$output['translations'] = [];
$translations = $stash->getTranslations( $user );
foreach ( $translations as $translation ) {
$output['translations'][] = $this->formatTranslation( $translation );
}
}
// If we got this far, nothing has failed
$output['result'] = 'ok';
$this->getResult()->addValue( null, $this->getModuleName(), $output );
}
protected function formatTranslation( StashedTranslation $translation ) {
$title = $translation->getTitle();
$handle = new MessageHandle( $title );
// Prepare for the worst
$definition = '';
$comparison = '';
if ( $handle->isValid() ) {
$groupId = MessageIndex::getPrimaryGroupId( $handle );
$group = MessageGroups::getGroup( $groupId );
$key = $handle->getKey();
$definition = $group->getMessage( $key, $group->getSourceLanguage() );
$comparison = $group->getMessage( $key, $handle->getCode() );
}
return [
'title' => $title->getPrefixedText(),
'definition' => $definition,
'translation' => $translation->getValue(),
'comparison' => $comparison,
'metadata' => $translation->getMetadata(),
];
}
public function isWriteMode() {
return true;
}
public function needsToken() {
return 'csrf';
}
public function getAllowedParams() {
return [
'subaction' => [
ApiBase::PARAM_TYPE => [ 'add', 'query' ],
ApiBase::PARAM_REQUIRED => true,
],
'title' => [
ApiBase::PARAM_TYPE => 'string',
],
'translation' => [
ApiBase::PARAM_TYPE => 'string',
],
'metadata' => [
ApiBase::PARAM_TYPE => 'string',
],
'token' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
'username' => [
ApiBase::PARAM_TYPE => 'string',
],
];
}
protected function getExamplesMessages() {
return [
'action=translationstash&subaction=add&title=MediaWiki:Jan/fi&translation=tammikuu&metadata={}'
=> 'apihelp-translationstash-example-1',
'action=translationstash&subaction=query'
=> 'apihelp-translationstash-example-2',
];
}
}
|