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 $passwordValidityStatus = $user->checkPasswordValidity( $password );
117 if ( !$passwordValidityStatus->isGood() ) {
118 $this->dieStatus( $passwordValidityStatus );
121 $email = $params[
'email'];
122 if ( !Sanitizer::validateEmail( $email ) ) {
123 $this->dieWithError(
'invalidemailaddress',
'invalidemail' );
128 }
catch ( RuntimeException $e ) {
131 $this->dieWithError(
'apierror-translate-sandbox-user-add' );
137 $output = [
'user' => [
138 'name' => $user->getName(),
139 'id' => $user->getId(),
142 $this->userOptionsManager->setOption( $user,
'language', $this->getContext()->getLanguage()->getCode() );
143 $this->userOptionsManager->saveOptions( $user );
145 $this->getResult()->addValue(
null, $this->getModuleName(), $output );
148 private function doDelete():
void {
149 $this->checkUserRightsAny(
'translate-sandboxmanage' );
151 $params = $this->extractRequestParams();
153 foreach ( $params[
'userid'] as $userId ) {
154 $user = $this->userFactory->newFromId( $userId );
155 $userpage = $user->getUserPage();
163 [
'apierror-translate-sandbox-invalidparam', wfEscapeWikiText( $e->getMessage() ) ],
168 $logEntry =
new ManualLogEntry(
'translatorsandbox',
'rejected' );
169 $logEntry->setPerformer( $this->getUser() );
170 $logEntry->setTarget( $userpage );
171 $logid = $logEntry->insert();
172 $logEntry->publish( $logid );
176 private function doPromote():
void {
177 $this->checkUserRightsAny(
'translate-sandboxmanage' );
179 $params = $this->extractRequestParams();
181 foreach ( $params[
'userid'] as $userId ) {
182 $user = $this->userFactory->newFromId( $userId );
188 [
'apierror-translate-sandbox-invalidparam', wfEscapeWikiText( $e->getMessage() ) ],
195 $logEntry =
new ManualLogEntry(
'translatorsandbox',
'promoted' );
196 $logEntry->setPerformer( $this->getUser() );
197 $logEntry->setTarget( $user->getUserPage() );
198 $logEntry->setParameters( [
199 '4::userid' => $user->getId(),
201 $logid = $logEntry->insert();
202 $logEntry->publish( $logid );
204 $this->createUserPage( $user );
208 private function doRemind():
void {
209 $params = $this->extractRequestParams();
211 foreach ( $params[
'userid'] as $userId ) {
212 $target = $this->userFactory->newFromId( $userId );
218 [
'apierror-translate-sandbox-invalidparam', wfEscapeWikiText( $e->getMessage() ) ],
226 private function createUserPage( User $user ):
void {
227 $userpage = $user->getUserPage();
229 if ( $userpage->exists() ) {
233 $languagePrefs = FormatJson::decode(
234 $this->userOptionsLookup->getOption( $user,
'translate-sandbox' ),
237 $languages = implode(
'|', $languagePrefs[
'languages' ] ?? [] );
238 $babeltext =
"{{#babel:$languages}}";
239 $summary = $this->msg(
'tsb-create-user-page' )->inContentLanguage()->text();
241 $page = $this->wikiPageFactory->newFromTitle( $userpage );
242 $content = ContentHandler::makeContent( $babeltext, $userpage );
244 $page->newPageUpdater( $user )
245 ->setContent( SlotRecord::MAIN, $content )
246 ->saveRevision( CommentStoreComment::newUnsavedComment( trim( $summary ) ), EDIT_NEW );
249 public function isWriteMode():
bool {
253 public function needsToken():
string {
257 protected function getAllowedParams(): array {
260 ParamValidator::PARAM_TYPE => [
'create',
'delete',
'promote',
'remind' ],
261 ParamValidator::PARAM_REQUIRED =>
true,
264 ParamValidator::PARAM_TYPE =>
'integer',
265 ParamValidator::PARAM_DEFAULT => 0,
266 ParamValidator::PARAM_ISMULTI =>
true,
269 ParamValidator::PARAM_TYPE =>
'string',
270 ParamValidator::PARAM_REQUIRED =>
true,
272 'username' => [ ParamValidator::PARAM_TYPE =>
'string' ],
273 'password' => [ ParamValidator::PARAM_TYPE =>
'string' ],
274 'email' => [ ParamValidator::PARAM_TYPE =>
'string' ],