userOptionsManager = $userOptionsManager; $this->languageNameUtils = $languageNameUtils; } public function execute() { $request = $this->getRequest(); if ( !$request->wasPosted() ) { $this->dieWithError( [ 'apierror-mustbeposted', $request->getText( 'action' ) ] ); } $languageCode = $request->getRawVal( 'languagecode', '' ); if ( !$this->languageNameUtils->isSupportedLanguage( $languageCode ) ) { $this->dieWithError( [ 'apierror-invalidlang', $this->encodeParamName( 'languagecode' ) ] ); } $user = $this->getUser(); if ( !$user->isRegistered() ) { if ( $this->getConfig()->get( 'ULSAnonCanChangeLanguage' ) ) { // Anonymous users can change language. // Use a cookie that also can changed by JavaScript. $request->response()->setCookie( 'language', $languageCode, 0, [ 'httpOnly' => false ] ); return; } $this->dieWithError( [ 'apierror-ulssetlang-anon-notallowed' ] ); } $updateUser = $user->getInstanceForUpdate(); $this->userOptionsManager->setOption( $updateUser, 'language', $languageCode ); // Sync the DB on post-send DeferredUpdates::addCallableUpdate( static function () use ( $updateUser ) { $updateUser->saveSettings(); } ); } public function getAllowedParams() { return [ 'languagecode' => [ ParamValidator::PARAM_REQUIRED => true, ] ]; } public function isInternal() { // Try to scare people away from using this externally return true; } public function needsToken() { return 'csrf'; } }