Merge "Move counting of watchers to WatchedItemStore"
[lhc/web/wiklou.git] / includes / api / ApiCreateAccount.php
index b3a543a..5552a85 100644 (file)
@@ -21,6 +21,7 @@
  *
  * @file
  */
+use MediaWiki\Logger\LoggerFactory;
 
 /**
  * Unit to authenticate account registration attempts to the current wiki.
@@ -52,16 +53,14 @@ class ApiCreateAccount extends ApiBase {
                                'You cannot create a new account because you are blocked',
                                'blocked',
                                0,
-                               array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $this->getUser()->getBlock() ) )
+                               [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $this->getUser()->getBlock() ) ]
                        );
                }
 
                $params = $this->extractRequestParams();
 
-               // Init session if necessary
-               if ( session_id() == '' ) {
-                       wfSetupSession();
-               }
+               // Make sure session is persisted
+               MediaWiki\Session\SessionManager::getGlobalSession()->persist();
 
                if ( $params['mailpassword'] && !$params['email'] ) {
                        $this->dieUsageMsg( 'noemail' );
@@ -74,7 +73,7 @@ class ApiCreateAccount extends ApiBase {
                $context = new DerivativeContext( $this->getContext() );
                $context->setRequest( new DerivativeRequest(
                        $this->getContext()->getRequest(),
-                       array(
+                       [
                                'type' => 'signup',
                                'uselang' => $params['language'],
                                'wpName' => $params['name'],
@@ -86,16 +85,20 @@ class ApiCreateAccount extends ApiBase {
                                'wpCreateaccountToken' => $params['token'],
                                'wpCreateaccount' => $params['mailpassword'] ? null : '1',
                                'wpCreateaccountMail' => $params['mailpassword'] ? '1' : null
-                       )
+                       ]
                ) );
 
                $loginForm = new LoginForm();
                $loginForm->setContext( $context );
-               Hooks::run( 'AddNewAccountApiForm', array( $this, $loginForm ) );
+               Hooks::run( 'AddNewAccountApiForm', [ $this, $loginForm ] );
                $loginForm->load();
 
-               $status = $loginForm->addNewaccountInternal();
-               $result = array();
+               $status = $loginForm->addNewAccountInternal();
+               LoggerFactory::getInstance( 'authmanager' )->info( 'Account creation attempt via API', [
+                       'event' => 'accountcreation',
+                       'status' => $status,
+               ] );
+               $result = [];
                if ( $status->isGood() ) {
                        // Success!
                        $user = $status->getValue();
@@ -113,7 +116,9 @@ class ApiCreateAccount extends ApiBase {
                                        'createaccount-title',
                                        'createaccount-text'
                                ) );
-                       } elseif ( $this->getConfig()->get( 'EmailAuthentication' ) && Sanitizer::validateEmail( $user->getEmail() ) ) {
+                       } elseif ( $this->getConfig()->get( 'EmailAuthentication' ) &&
+                               Sanitizer::validateEmail( $user->getEmail() )
+                       ) {
                                // Send out an email authentication message if needed
                                $status->merge( $user->sendConfirmationMail() );
                        }
@@ -121,7 +126,7 @@ class ApiCreateAccount extends ApiBase {
                        // Save settings (including confirmation token)
                        $user->saveSettings();
 
-                       Hooks::run( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
+                       Hooks::run( 'AddNewAccount', [ $user, $params['mailpassword'] ] );
 
                        if ( $params['mailpassword'] ) {
                                $logAction = 'byemail';
@@ -144,8 +149,11 @@ class ApiCreateAccount extends ApiBase {
                        // Token was incorrect, so add it to result, but don't throw an exception
                        // since not having the correct token is part of the normal
                        // flow of events.
-                       $result['token'] = LoginForm::getCreateaccountToken();
+                       $result['token'] = LoginForm::getCreateaccountToken()->toString();
                        $result['result'] = 'NeedToken';
+                       $this->setWarning( 'Fetching a token via action=createaccount is deprecated. ' .
+                               'Use action=query&meta=tokens&type=createaccount instead.' );
+                       $this->logFeatureUsage( 'action=createaccount&!token' );
                } elseif ( !$status->isOK() ) {
                        // There was an error. Die now.
                        $this->dieStatus( $status );
@@ -168,7 +176,7 @@ class ApiCreateAccount extends ApiBase {
                }
 
                // Give extensions a chance to modify the API result data
-               Hooks::run( 'AddNewAccountApiResult', array( $this, $loginForm, &$result ) );
+               Hooks::run( 'AddNewAccountApiResult', [ $this, $loginForm, &$result ] );
 
                $apiResult->addValue( null, 'createaccount', $result );
        }
@@ -186,37 +194,41 @@ class ApiCreateAccount extends ApiBase {
        }
 
        public function getAllowedParams() {
-               return array(
-                       'name' => array(
+               return [
+                       'name' => [
                                ApiBase::PARAM_TYPE => 'user',
                                ApiBase::PARAM_REQUIRED => true
-                       ),
-                       'password' => array(
+                       ],
+                       'password' => [
                                ApiBase::PARAM_TYPE => 'password',
-                       ),
+                       ],
                        'domain' => null,
-                       'token' => null,
-                       'email' => array(
+                       'token' => [
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => false, // for BC
+                               ApiBase::PARAM_HELP_MSG => [ 'api-help-param-token', 'createaccount' ],
+                       ],
+                       'email' => [
                                ApiBase::PARAM_TYPE => 'string',
                                ApiBase::PARAM_REQUIRED => $this->getConfig()->get( 'EmailConfirmToEdit' ),
-                       ),
+                       ],
                        'realname' => null,
-                       'mailpassword' => array(
+                       'mailpassword' => [
                                ApiBase::PARAM_TYPE => 'boolean',
                                ApiBase::PARAM_DFLT => false
-                       ),
+                       ],
                        'reason' => null,
                        'language' => null
-               );
+               ];
        }
 
        protected function getExamplesMessages() {
-               return array(
+               return [
                        'action=createaccount&name=testuser&password=test123'
                                => 'apihelp-createaccount-example-pass',
                        'action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason'
                                => 'apihelp-createaccount-example-mail',
-               );
+               ];
        }
 
        public function getHelpUrls() {