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 $passwordValidityStatus = $user->checkPasswordValidity( $password );
117 if ( !$passwordValidityStatus->isGood() ) {
118 $this->dieStatus( $passwordValidityStatus );
119 }
120
121 $email = $params['email'];
122 if ( !Sanitizer::validateEmail( $email ) ) {
123 $this->dieWithError( 'invalidemailaddress', 'invalidemail' );
124 }
125
126 $user = TranslateSandbox::addUser( $username, $email, $password );
127 $output = [ 'user' => [
128 'name' => $user->getName(),
129 'id' => $user->getId(),
130 ] ];
131
132 $this->userOptionsManager->setOption( $user, 'language', $this->getContext()->getLanguage()->getCode() );
133 $this->userOptionsManager->saveOptions( $user );
134
135 $this->getResult()->addValue( null, $this->getModuleName(), $output );
136 }
137
138 private function doDelete(): void {
139 $this->checkUserRightsAny( 'translate-sandboxmanage' );
140
141 $params = $this->extractRequestParams();
142
143 foreach ( $params['userid'] as $userId ) {
144 $user = $this->userFactory->newFromId( $userId );
145 $userpage = $user->getUserPage();
146
147 TranslateSandbox::sendEmail( $this->getUser(), $user, 'rejection' );
148
149 try {
150 TranslateSandbox::deleteUser( $user );
151 } catch ( MWException $e ) {
152 $this->dieWithError(
153 [ 'apierror-translate-sandbox-invalidparam', wfEscapeWikiText( $e->getMessage() ) ],
154 'invalidparam'
155 );
156 }
157
158 $logEntry = new ManualLogEntry( 'translatorsandbox', 'rejected' );
159 $logEntry->setPerformer( $this->getUser() );
160 $logEntry->setTarget( $userpage );
161 $logid = $logEntry->insert();
162 $logEntry->publish( $logid );
163 }
164 }
165
166 private function doPromote(): void {
167 $this->checkUserRightsAny( 'translate-sandboxmanage' );
168
169 $params = $this->extractRequestParams();
170
171 foreach ( $params['userid'] as $userId ) {
172 $user = $this->userFactory->newFromId( $userId );
173
174 try {
175 TranslateSandbox::promoteUser( $user );
176 } catch ( MWException $e ) {
177 $this->dieWithError(
178 [ 'apierror-translate-sandbox-invalidparam', wfEscapeWikiText( $e->getMessage() ) ],
179 'invalidparam'
180 );
181 }
182
183 TranslateSandbox::sendEmail( $this->getUser(), $user, 'promotion' );
184
185 $logEntry = new ManualLogEntry( 'translatorsandbox', 'promoted' );
186 $logEntry->setPerformer( $this->getUser() );
187 $logEntry->setTarget( $user->getUserPage() );
188 $logEntry->setParameters( [
189 '4::userid' => $user->getId(),
190 ] );
191 $logid = $logEntry->insert();
192 $logEntry->publish( $logid );
193
194 $this->createUserPage( $user );
195 }
196 }
197
198 private function doRemind(): void {
199 $params = $this->extractRequestParams();
200
201 foreach ( $params['userid'] as $userId ) {
202 $target = $this->userFactory->newFromId( $userId );
203
204 try {
205 TranslateSandbox::sendEmail( $this->getUser(), $target, 'reminder' );
206 } catch ( MWException $e ) {
207 $this->dieWithError(
208 [ 'apierror-translate-sandbox-invalidparam', wfEscapeWikiText( $e->getMessage() ) ],
209 'invalidparam'
210 );
211 }
212 }
213 }
214
216 private function createUserPage( User $user ): void {
217 $userpage = $user->getUserPage();
218
219 if ( $userpage->exists() ) {
220 return;
221 }
222
223 $languagePrefs = FormatJson::decode(
224 $this->userOptionsLookup->getOption( $user, 'translate-sandbox' ),
225 true
226 );
227 $languages = implode( '|', $languagePrefs[ 'languages' ] ?? [] );
228 $babeltext = "{{#babel:$languages}}";
229 $summary = $this->msg( 'tsb-create-user-page' )->inContentLanguage()->text();
230
231 $page = $this->wikiPageFactory->newFromTitle( $userpage );
232 $content = ContentHandler::makeContent( $babeltext, $userpage );
233
234 $page->newPageUpdater( $user )
235 ->setContent( SlotRecord::MAIN, $content )
236 ->saveRevision( CommentStoreComment::newUnsavedComment( trim( $summary ) ), EDIT_NEW );
237 }
238
239 public function isWriteMode(): bool {
240 return true;
241 }
242
243 public function needsToken(): string {
244 return 'csrf';
245 }
246
247 protected function getAllowedParams(): array {
248 return [
249 'do' => [
250 ParamValidator::PARAM_TYPE => [ 'create', 'delete', 'promote', 'remind' ],
251 ParamValidator::PARAM_REQUIRED => true,
252 ],
253 'userid' => [
254 ParamValidator::PARAM_TYPE => 'integer',
255 ParamValidator::PARAM_DEFAULT => 0,
256 ParamValidator::PARAM_ISMULTI => true,
257 ],
258 'token' => [
259 ParamValidator::PARAM_TYPE => 'string',
260 ParamValidator::PARAM_REQUIRED => true,
261 ],
262 'username' => [ ParamValidator::PARAM_TYPE => 'string' ],
263 'password' => [ ParamValidator::PARAM_TYPE => 'string' ],
264 'email' => [ ParamValidator::PARAM_TYPE => 'string' ],
265 ];
266 }
267}
Utility class for the sandbox feature of Translate.