Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatorSandboxActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorSandbox;
5
6use ApiBase;
7use ApiMain;
8use CommentStoreComment;
9use ContentHandler;
10use FormatJson;
11use ManualLogEntry;
12use MediaWiki\Config\ServiceOptions;
13use MediaWiki\Page\WikiPageFactory;
14use MediaWiki\Revision\SlotRecord;
15use MediaWiki\User\UserFactory;
16use MediaWiki\User\UserNameUtils;
17use MediaWiki\User\UserOptionsLookup;
18use MediaWiki\User\UserOptionsManager;
19use MWException;
20use Sanitizer;
22use User;
23use Wikimedia\ParamValidator\ParamValidator;
24
31class TranslatorSandboxActionApi extends ApiBase {
33 private $userFactory;
35 private $userNameUtils;
37 private $userOptionsManager;
39 private $wikiPageFactory;
41 private $userOptionsLookup;
43 private $options;
44
45 public const CONSTRUCTOR_OPTIONS = [
46 'TranslateUseSandbox',
47 ];
48
49 public function __construct(
50 ApiMain $mainModule,
51 string $moduleName,
52 UserFactory $userFactory,
53 UserNameUtils $userNameUtils,
54 UserOptionsManager $userOptionsManager,
55 WikiPageFactory $wikiPageFactory,
56 UserOptionsLookup $userOptionsLookup,
57 ServiceOptions $options
58 ) {
59 parent::__construct( $mainModule, $moduleName );
60 $this->userFactory = $userFactory;
61 $this->userNameUtils = $userNameUtils;
62 $this->userOptionsManager = $userOptionsManager;
63 $this->wikiPageFactory = $wikiPageFactory;
64 $this->userOptionsLookup = $userOptionsLookup;
65 $this->options = $options;
66 }
67
68 public function execute(): void {
69 if ( !$this->options->get( 'TranslateUseSandbox' ) ) {
70 $this->dieWithError( 'apierror-translate-sandboxdisabled', 'sandboxdisabled' );
71 }
72
73 $params = $this->extractRequestParams();
74 switch ( $params['do'] ) {
75 case 'create':
76 $this->doCreate();
77 break;
78 case 'delete':
79 $this->doDelete();
80 break;
81 case 'promote':
82 $this->doPromote();
83 break;
84 case 'remind':
85 $this->doRemind();
86 break;
87 default:
88 $this->dieWithError( [ 'apierror-badparameter', 'do' ] );
89 }
90 }
91
92 private function doCreate(): void {
93 $params = $this->extractRequestParams();
94
95 // Do validations
96 foreach ( explode( '|', 'username|password|email' ) as $field ) {
97 if ( !isset( $params[$field] ) ) {
98 $this->dieWithError( [ 'apierror-missingparam', $field ], 'missingparam' );
99 }
100 }
101
102 $username = $params['username'];
103
104 $canonicalName = $this->userNameUtils->getCanonical( $username, UserNameUtils::RIGOR_CREATABLE );
105
106 if ( $canonicalName === false ) {
107 $this->dieWithError( 'noname', 'invalidusername' );
108 }
109
110 $user = $this->userFactory->newFromName( $username );
111 if ( $user->getId() !== 0 ) {
112 $this->dieWithError( 'userexists', 'nonfreeusername' );
113 }
114
115 $password = $params['password'];
116 if ( !$user->isValidPassword( $password ) ) {
117 $this->dieWithError( 'apierror-translate-sandbox-invalidpassword', 'invalidpassword' );
118 }
119
120 $email = $params['email'];
121 if ( !Sanitizer::validateEmail( $email ) ) {
122 $this->dieWithError( 'invalidemailaddress', 'invalidemail' );
123 }
124
125 $user = TranslateSandbox::addUser( $username, $email, $password );
126 $output = [ 'user' => [
127 'name' => $user->getName(),
128 'id' => $user->getId(),
129 ] ];
130
131 $this->userOptionsManager->setOption( $user, 'language', $this->getContext()->getLanguage()->getCode() );
132 $this->userOptionsManager->saveOptions( $user );
133
134 $this->getResult()->addValue( null, $this->getModuleName(), $output );
135 }
136
137 private function doDelete(): void {
138 $this->checkUserRightsAny( 'translate-sandboxmanage' );
139
140 $params = $this->extractRequestParams();
141
142 foreach ( $params['userid'] as $userId ) {
143 $user = $this->userFactory->newFromId( $userId );
144 $userpage = $user->getUserPage();
145
146 TranslateSandbox::sendEmail( $this->getUser(), $user, 'rejection' );
147
148 try {
149 TranslateSandbox::deleteUser( $user );
150 } catch ( MWException $e ) {
151 $this->dieWithError(
152 [ 'apierror-translate-sandbox-invalidparam', wfEscapeWikiText( $e->getMessage() ) ],
153 'invalidparam'
154 );
155 }
156
157 $logEntry = new ManualLogEntry( 'translatorsandbox', 'rejected' );
158 $logEntry->setPerformer( $this->getUser() );
159 $logEntry->setTarget( $userpage );
160 $logid = $logEntry->insert();
161 $logEntry->publish( $logid );
162 }
163 }
164
165 private function doPromote(): void {
166 $this->checkUserRightsAny( 'translate-sandboxmanage' );
167
168 $params = $this->extractRequestParams();
169
170 foreach ( $params['userid'] as $userId ) {
171 $user = $this->userFactory->newFromId( $userId );
172
173 try {
174 TranslateSandbox::promoteUser( $user );
175 } catch ( MWException $e ) {
176 $this->dieWithError(
177 [ 'apierror-translate-sandbox-invalidparam', wfEscapeWikiText( $e->getMessage() ) ],
178 'invalidparam'
179 );
180 }
181
182 TranslateSandbox::sendEmail( $this->getUser(), $user, 'promotion' );
183
184 $logEntry = new ManualLogEntry( 'translatorsandbox', 'promoted' );
185 $logEntry->setPerformer( $this->getUser() );
186 $logEntry->setTarget( $user->getUserPage() );
187 $logEntry->setParameters( [
188 '4::userid' => $user->getId(),
189 ] );
190 $logid = $logEntry->insert();
191 $logEntry->publish( $logid );
192
193 $this->createUserPage( $user );
194 }
195 }
196
197 private function doRemind(): void {
198 $params = $this->extractRequestParams();
199
200 foreach ( $params['userid'] as $userId ) {
201 $target = $this->userFactory->newFromId( $userId );
202
203 try {
204 TranslateSandbox::sendEmail( $this->getUser(), $target, 'reminder' );
205 } catch ( MWException $e ) {
206 $this->dieWithError(
207 [ 'apierror-translate-sandbox-invalidparam', wfEscapeWikiText( $e->getMessage() ) ],
208 'invalidparam'
209 );
210 }
211 }
212 }
213
215 private function createUserPage( User $user ): void {
216 $userpage = $user->getUserPage();
217
218 if ( $userpage->exists() ) {
219 return;
220 }
221
222 $languagePrefs = FormatJson::decode(
223 $this->userOptionsLookup->getOption( $user, 'translate-sandbox' ),
224 true
225 );
226 $languages = implode( '|', $languagePrefs[ 'languages' ] ?? [] );
227 $babeltext = "{{#babel:$languages}}";
228 $summary = $this->msg( 'tsb-create-user-page' )->inContentLanguage()->text();
229
230 $page = $this->wikiPageFactory->newFromTitle( $userpage );
231 $content = ContentHandler::makeContent( $babeltext, $userpage );
232
233 $page->newPageUpdater( $user )
234 ->setContent( SlotRecord::MAIN, $content )
235 ->saveRevision( CommentStoreComment::newUnsavedComment( trim( $summary ) ), EDIT_NEW );
236 }
237
238 public function isWriteMode(): bool {
239 return true;
240 }
241
242 public function needsToken(): string {
243 return 'csrf';
244 }
245
246 protected function getAllowedParams(): array {
247 return [
248 'do' => [
249 ParamValidator::PARAM_TYPE => [ 'create', 'delete', 'promote', 'remind' ],
250 ParamValidator::PARAM_REQUIRED => true,
251 ],
252 'userid' => [
253 ParamValidator::PARAM_TYPE => 'integer',
254 ParamValidator::PARAM_DEFAULT => 0,
255 ParamValidator::PARAM_ISMULTI => true,
256 ],
257 'token' => [
258 ParamValidator::PARAM_TYPE => 'string',
259 ParamValidator::PARAM_REQUIRED => true,
260 ],
261 'username' => [ ParamValidator::PARAM_TYPE => 'string' ],
262 'password' => [ ParamValidator::PARAM_TYPE => 'string' ],
263 'email' => [ ParamValidator::PARAM_TYPE => 'string' ],
264 ];
265 }
266}
Utility class for the sandbox feature of Translate.