35 private $userNameUtils;
37 private $userOptionsManager;
39 private $wikiPageFactory;
41 private $userOptionsLookup;
45 public const CONSTRUCTOR_OPTIONS = [
46 'TranslateUseSandbox',
49 public function __construct(
52 UserFactory $userFactory,
53 UserNameUtils $userNameUtils,
54 UserOptionsManager $userOptionsManager,
55 WikiPageFactory $wikiPageFactory,
56 UserOptionsLookup $userOptionsLookup,
57 ServiceOptions $options
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;
68 public function execute():
void {
69 if ( !$this->options->get(
'TranslateUseSandbox' ) ) {
70 $this->dieWithError(
'apierror-translate-sandboxdisabled',
'sandboxdisabled' );
73 $params = $this->extractRequestParams();
74 switch ( $params[
'do'] ) {
88 $this->dieWithError( [
'apierror-badparameter',
'do' ] );
92 private function doCreate():
void {
93 $params = $this->extractRequestParams();
96 foreach ( explode(
'|',
'username|password|email' ) as $field ) {
97 if ( !isset( $params[$field] ) ) {
98 $this->dieWithError( [
'apierror-missingparam', $field ],
'missingparam' );
102 $username = $params[
'username'];
104 $canonicalName = $this->userNameUtils->getCanonical( $username, UserNameUtils::RIGOR_CREATABLE );
106 if ( $canonicalName ===
false ) {
107 $this->dieWithError(
'noname',
'invalidusername' );
110 $user = $this->userFactory->newFromName( $username );
111 if ( $user->getId() !== 0 ) {
112 $this->dieWithError(
'userexists',
'nonfreeusername' );
115 $password = $params[
'password'];
116 if ( !$user->isValidPassword( $password ) ) {
117 $this->dieWithError(
'apierror-translate-sandbox-invalidpassword',
'invalidpassword' );
120 $email = $params[
'email'];
121 if ( !Sanitizer::validateEmail( $email ) ) {
122 $this->dieWithError(
'invalidemailaddress',
'invalidemail' );
125 $user = TranslateSandbox::addUser( $username, $email, $password );
126 $output = [
'user' => [
127 'name' => $user->getName(),
128 'id' => $user->getId(),
131 $this->userOptionsManager->setOption( $user,
'language', $this->getContext()->getLanguage()->getCode() );
132 $this->userOptionsManager->saveOptions( $user );
134 $this->getResult()->addValue(
null, $this->getModuleName(), $output );
137 private function doDelete():
void {
138 $this->checkUserRightsAny(
'translate-sandboxmanage' );
140 $params = $this->extractRequestParams();
142 foreach ( $params[
'userid'] as $userId ) {
143 $user = $this->userFactory->newFromId( $userId );
144 $userpage = $user->getUserPage();
146 TranslateSandbox::sendEmail( $this->getUser(), $user,
'rejection' );
149 TranslateSandbox::deleteUser( $user );
150 }
catch ( MWException $e ) {
152 [
'apierror-translate-sandbox-invalidparam', wfEscapeWikiText( $e->getMessage() ) ],
157 $logEntry =
new ManualLogEntry(
'translatorsandbox',
'rejected' );
158 $logEntry->setPerformer( $this->getUser() );
159 $logEntry->setTarget( $userpage );
160 $logid = $logEntry->insert();
161 $logEntry->publish( $logid );
165 private function doPromote():
void {
166 $this->checkUserRightsAny(
'translate-sandboxmanage' );
168 $params = $this->extractRequestParams();
170 foreach ( $params[
'userid'] as $userId ) {
171 $user = $this->userFactory->newFromId( $userId );
174 TranslateSandbox::promoteUser( $user );
175 }
catch ( MWException $e ) {
177 [
'apierror-translate-sandbox-invalidparam', wfEscapeWikiText( $e->getMessage() ) ],
182 TranslateSandbox::sendEmail( $this->getUser(), $user,
'promotion' );
184 $logEntry =
new ManualLogEntry(
'translatorsandbox',
'promoted' );
185 $logEntry->setPerformer( $this->getUser() );
186 $logEntry->setTarget( $user->getUserPage() );
187 $logEntry->setParameters( [
188 '4::userid' => $user->getId(),
190 $logid = $logEntry->insert();
191 $logEntry->publish( $logid );
193 $this->createUserPage( $user );
197 private function doRemind():
void {
198 $params = $this->extractRequestParams();
200 foreach ( $params[
'userid'] as $userId ) {
201 $target = $this->userFactory->newFromId( $userId );
204 TranslateSandbox::sendEmail( $this->getUser(), $target,
'reminder' );
205 }
catch ( MWException $e ) {
207 [
'apierror-translate-sandbox-invalidparam', wfEscapeWikiText( $e->getMessage() ) ],
215 private function createUserPage( User $user ):
void {
216 $userpage = $user->getUserPage();
218 if ( $userpage->exists() ) {
222 $languagePrefs = FormatJson::decode(
223 $this->userOptionsLookup->getOption( $user,
'translate-sandbox' ),
226 $languages = implode(
'|', $languagePrefs[
'languages' ] ?? [] );
227 $babeltext =
"{{#babel:$languages}}";
228 $summary = $this->msg(
'tsb-create-user-page' )->inContentLanguage()->text();
230 $page = $this->wikiPageFactory->newFromTitle( $userpage );
231 $content = ContentHandler::makeContent( $babeltext, $userpage );
233 $page->newPageUpdater( $user )
234 ->setContent( SlotRecord::MAIN, $content )
235 ->saveRevision( CommentStoreComment::newUnsavedComment( trim( $summary ) ), EDIT_NEW );
238 public function isWriteMode():
bool {
242 public function needsToken():
string {
246 protected function getAllowedParams(): array {
249 ParamValidator::PARAM_TYPE => [
'create',
'delete',
'promote',
'remind' ],
250 ParamValidator::PARAM_REQUIRED =>
true,
253 ParamValidator::PARAM_TYPE =>
'integer',
254 ParamValidator::PARAM_DEFAULT => 0,
255 ParamValidator::PARAM_ISMULTI =>
true,
258 ParamValidator::PARAM_TYPE =>
'string',
259 ParamValidator::PARAM_REQUIRED =>
true,
261 'username' => [ ParamValidator::PARAM_TYPE =>
'string' ],
262 'password' => [ ParamValidator::PARAM_TYPE =>
'string' ],
263 'email' => [ ParamValidator::PARAM_TYPE =>
'string' ],