From: Alexandre Emsenhuber Date: Wed, 6 Jul 2011 16:42:16 +0000 (+0000) Subject: * Pass the WebRequest object to User::newFromSession() and use it instead of $wgReque... X-Git-Tag: 1.31.0-rc.0~29043 X-Git-Url: http://git.cyclocoop.org/%28?a=commitdiff_plain;h=efc5144e7a88922724f1ef5937c62caf11944804;p=lhc%2Fweb%2Fwiklou.git * Pass the WebRequest object to User::newFromSession() and use it instead of $wgRequest (will still fall back to $wgRequest if not passed or another method to get an User object) * Always call User::newFromSession(), not only when not in CLI mode * Removed User::$mSkin, unused and not set --- diff --git a/includes/RequestContext.php b/includes/RequestContext.php index be21c43fd5..255a175400 100644 --- a/includes/RequestContext.php +++ b/includes/RequestContext.php @@ -120,10 +120,7 @@ class RequestContext { */ public function getUser() { if ( !isset($this->user) ) { - global $wgCommandLineMode; - $this->user = $wgCommandLineMode - ? new User - : User::newFromSession( $this->getRequest() ); + $this->user = User::newFromSession( $this->getRequest() ); } return $this->user; } @@ -347,4 +344,4 @@ abstract class ContextSource implements IContextSource { public function getSkin() { return $this->context->getSkin(); } -} \ No newline at end of file +} diff --git a/includes/User.php b/includes/User.php index 8019079589..839269d28d 100644 --- a/includes/User.php +++ b/includes/User.php @@ -202,9 +202,9 @@ class User { $mLocked, $mHideName, $mOptions; /** - * @var Skin + * @var WebRequest */ - var $mSkin; + private $mRequest; /** * @var Block @@ -407,11 +407,14 @@ class User { * Create a new user object using data from session or cookies. If the * login credentials are invalid, the result is an anonymous user. * + * @param $request WebRequest object to use; $wgRequest will be used if + * ommited. * @return User */ - static function newFromSession() { + static function newFromSession( WebRequest $request = null ) { $user = new User; $user->mFrom = 'session'; + $user->mRequest = $request; return $user; } @@ -851,8 +854,6 @@ class User { function loadDefaults( $name = false ) { wfProfileIn( __METHOD__ ); - global $wgRequest; - $this->mId = 0; $this->mName = $name; $this->mRealName = ''; @@ -862,8 +863,9 @@ class User { $this->mOptionOverrides = null; $this->mOptionsLoaded = false; - if( $wgRequest->getCookie( 'LoggedOut' ) !== null ) { - $this->mTouched = wfTimestamp( TS_MW, $wgRequest->getCookie( 'LoggedOut' ) ); + $loggedOut = $this->getRequest()->getCookie( 'LoggedOut' ); + if( $loggedOut !== null ) { + $this->mTouched = wfTimestamp( TS_MW, $loggedOut ); } else { $this->mTouched = '0'; # Allow any pages to be cached } @@ -914,7 +916,7 @@ class User { * @return Bool True if the user is logged in, false otherwise. */ private function loadFromSession() { - global $wgRequest, $wgExternalAuthType, $wgAutocreatePolicy; + global $wgExternalAuthType, $wgAutocreatePolicy; $result = null; wfRunHooks( 'UserLoadFromSession', array( $this, &$result ) ); @@ -930,8 +932,10 @@ class User { } } - $cookieId = $wgRequest->getCookie( 'UserID' ); - $sessId = $wgRequest->getSessionData( 'wsUserID' ); + $request = $this->getRequest(); + + $cookieId = $request->getCookie( 'UserID' ); + $sessId = $request->getSessionData( 'wsUserID' ); if ( $cookieId !== null ) { $sId = intval( $cookieId ); @@ -941,7 +945,7 @@ class User { cookie user ID ($sId) don't match!" ); return false; } - $wgRequest->setSessionData( 'wsUserID', $sId ); + $request->setSessionData( 'wsUserID', $sId ); } elseif ( $sessId !== null && $sessId != 0 ) { $sId = $sessId; } else { @@ -949,11 +953,11 @@ class User { return false; } - if ( $wgRequest->getSessionData( 'wsUserName' ) !== null ) { - $sName = $wgRequest->getSessionData( 'wsUserName' ); - } elseif ( $wgRequest->getCookie( 'UserName' ) !== null ) { - $sName = $wgRequest->getCookie( 'UserName' ); - $wgRequest->setSessionData( 'wsUserName', $sName ); + if ( $request->getSessionData( 'wsUserName' ) !== null ) { + $sName = $request->getSessionData( 'wsUserName' ); + } elseif ( $request->getCookie( 'UserName' ) !== null ) { + $sName = $request->getCookie( 'UserName' ); + $request->setSessionData( 'wsUserName', $sName ); } else { $this->loadDefaults(); return false; @@ -973,11 +977,11 @@ class User { return false; } - if ( $wgRequest->getSessionData( 'wsToken' ) !== null ) { - $passwordCorrect = $proposedUser->getToken() === $wgRequest->getSessionData( 'wsToken' ); + if ( $request->getSessionData( 'wsToken' ) !== null ) { + $passwordCorrect = $proposedUser->getToken() === $request->getSessionData( 'wsToken' ); $from = 'session'; - } elseif ( $wgRequest->getCookie( 'Token' ) !== null ) { - $passwordCorrect = $proposedUser->getToken() === $wgRequest->getCookie( 'Token' ); + } elseif ( $request->getCookie( 'Token' ) !== null ) { + $passwordCorrect = $proposedUser->getToken() === $request->getCookie( 'Token' ); $from = 'cookie'; } else { # No session or persistent login cookie @@ -987,7 +991,7 @@ class User { if ( ( $sName === $proposedUser->getName() ) && $passwordCorrect ) { $this->loadFromUserObject( $proposedUser ); - $wgRequest->setSessionData( 'wsToken', $this->mToken ); + $request->setSessionData( 'wsToken', $this->mToken ); wfDebug( "User: logged in from $from\n" ); return true; } else { @@ -1166,7 +1170,6 @@ class User { $this->mDatePreference = null; $this->mBlockedby = -1; # Unset $this->mHash = false; - $this->mSkin = null; $this->mRights = null; $this->mEffectiveGroups = null; $this->mOptions = null; @@ -2183,11 +2186,6 @@ class User { $this->load(); $this->loadOptions(); - if ( $oname == 'skin' ) { - # Clear cached skin, so the new one displays immediately in Special:Preferences - $this->mSkin = null; - } - // Explicitly NULL values should refer to defaults global $wgDefaultUserOptions; if( is_null( $val ) && isset( $wgDefaultUserOptions[$oname] ) ) { @@ -2467,6 +2465,20 @@ class User { return( ( $wgUseRCPatrol || $wgUseNPPatrol ) && ( $this->isAllowedAny( 'patrol', 'patrolmarks' ) ) ); } + /** + * Get the WebRequest object to use with this object + * + * @return WebRequest + */ + public function getRequest() { + if ( $this->mRequest ) { + return $this->mRequest; + } else { + global $wgRequest; + return $wgRequest; + } + } + /** * Get the current skin, loading it if required * @return Skin The current skin @@ -2625,8 +2637,7 @@ class User { * if 0 or not specified, use the default $wgCookieExpiration */ protected function setCookie( $name, $value, $exp = 0 ) { - global $wgRequest; - $wgRequest->response()->setcookie( $name, $value, $exp ); + $this->getRequest()->response()->setcookie( $name, $value, $exp ); } /** @@ -2645,8 +2656,7 @@ class User { */ function setCookies( $request = null ) { if ( $request === null ) { - global $wgRequest; - $request = $wgRequest; + $request = $this->getRequest(); } $this->load(); @@ -2695,11 +2705,9 @@ class User { * @see logout() */ function doLogout() { - global $wgRequest; - $this->clearInstanceCache( 'defaults' ); - $wgRequest->setSessionData( 'wsUserID', 0 ); + $this->getRequest()->setSessionData( 'wsUserID', 0 ); $this->clearCookie( 'UserID' ); $this->clearCookie( 'Token' ); @@ -3056,8 +3064,7 @@ class User { */ function editToken( $salt = '', $request = null ) { if ( $request == null ) { - global $wgRequest; - $request = $wgRequest; + $request = $this->getRequest(); } if ( $this->isAnon() ) {