r91560 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91559‎ | r91560 | r91561 >
Date:16:42, 6 July 2011
Author:ialex
Status:ok
Tags:
Comment:
* 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
Modified paths:
  • /trunk/phase3/includes/RequestContext.php (modified) (history)
  • /trunk/phase3/includes/User.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/User.php
@@ -202,9 +202,9 @@
203203 $mLocked, $mHideName, $mOptions;
204204
205205 /**
206 - * @var Skin
 206+ * @var WebRequest
207207 */
208 - var $mSkin;
 208+ private $mRequest;
209209
210210 /**
211211 * @var Block
@@ -407,11 +407,14 @@
408408 * Create a new user object using data from session or cookies. If the
409409 * login credentials are invalid, the result is an anonymous user.
410410 *
 411+ * @param $request WebRequest object to use; $wgRequest will be used if
 412+ * ommited.
411413 * @return User
412414 */
413 - static function newFromSession() {
 415+ static function newFromSession( WebRequest $request = null ) {
414416 $user = new User;
415417 $user->mFrom = 'session';
 418+ $user->mRequest = $request;
416419 return $user;
417420 }
418421
@@ -851,8 +854,6 @@
852855 function loadDefaults( $name = false ) {
853856 wfProfileIn( __METHOD__ );
854857
855 - global $wgRequest;
856 -
857858 $this->mId = 0;
858859 $this->mName = $name;
859860 $this->mRealName = '';
@@ -862,8 +863,9 @@
863864 $this->mOptionOverrides = null;
864865 $this->mOptionsLoaded = false;
865866
866 - if( $wgRequest->getCookie( 'LoggedOut' ) !== null ) {
867 - $this->mTouched = wfTimestamp( TS_MW, $wgRequest->getCookie( 'LoggedOut' ) );
 867+ $loggedOut = $this->getRequest()->getCookie( 'LoggedOut' );
 868+ if( $loggedOut !== null ) {
 869+ $this->mTouched = wfTimestamp( TS_MW, $loggedOut );
868870 } else {
869871 $this->mTouched = '0'; # Allow any pages to be cached
870872 }
@@ -914,7 +916,7 @@
915917 * @return Bool True if the user is logged in, false otherwise.
916918 */
917919 private function loadFromSession() {
918 - global $wgRequest, $wgExternalAuthType, $wgAutocreatePolicy;
 920+ global $wgExternalAuthType, $wgAutocreatePolicy;
919921
920922 $result = null;
921923 wfRunHooks( 'UserLoadFromSession', array( $this, &$result ) );
@@ -930,9 +932,11 @@
931933 }
932934 }
933935
934 - $cookieId = $wgRequest->getCookie( 'UserID' );
935 - $sessId = $wgRequest->getSessionData( 'wsUserID' );
 936+ $request = $this->getRequest();
936937
 938+ $cookieId = $request->getCookie( 'UserID' );
 939+ $sessId = $request->getSessionData( 'wsUserID' );
 940+
937941 if ( $cookieId !== null ) {
938942 $sId = intval( $cookieId );
939943 if( $sessId !== null && $cookieId != $sessId ) {
@@ -941,7 +945,7 @@
942946 cookie user ID ($sId) don't match!" );
943947 return false;
944948 }
945 - $wgRequest->setSessionData( 'wsUserID', $sId );
 949+ $request->setSessionData( 'wsUserID', $sId );
946950 } elseif ( $sessId !== null && $sessId != 0 ) {
947951 $sId = $sessId;
948952 } else {
@@ -949,11 +953,11 @@
950954 return false;
951955 }
952956
953 - if ( $wgRequest->getSessionData( 'wsUserName' ) !== null ) {
954 - $sName = $wgRequest->getSessionData( 'wsUserName' );
955 - } elseif ( $wgRequest->getCookie( 'UserName' ) !== null ) {
956 - $sName = $wgRequest->getCookie( 'UserName' );
957 - $wgRequest->setSessionData( 'wsUserName', $sName );
 957+ if ( $request->getSessionData( 'wsUserName' ) !== null ) {
 958+ $sName = $request->getSessionData( 'wsUserName' );
 959+ } elseif ( $request->getCookie( 'UserName' ) !== null ) {
 960+ $sName = $request->getCookie( 'UserName' );
 961+ $request->setSessionData( 'wsUserName', $sName );
958962 } else {
959963 $this->loadDefaults();
960964 return false;
@@ -973,11 +977,11 @@
974978 return false;
975979 }
976980
977 - if ( $wgRequest->getSessionData( 'wsToken' ) !== null ) {
978 - $passwordCorrect = $proposedUser->getToken() === $wgRequest->getSessionData( 'wsToken' );
 981+ if ( $request->getSessionData( 'wsToken' ) !== null ) {
 982+ $passwordCorrect = $proposedUser->getToken() === $request->getSessionData( 'wsToken' );
979983 $from = 'session';
980 - } elseif ( $wgRequest->getCookie( 'Token' ) !== null ) {
981 - $passwordCorrect = $proposedUser->getToken() === $wgRequest->getCookie( 'Token' );
 984+ } elseif ( $request->getCookie( 'Token' ) !== null ) {
 985+ $passwordCorrect = $proposedUser->getToken() === $request->getCookie( 'Token' );
982986 $from = 'cookie';
983987 } else {
984988 # No session or persistent login cookie
@@ -987,7 +991,7 @@
988992
989993 if ( ( $sName === $proposedUser->getName() ) && $passwordCorrect ) {
990994 $this->loadFromUserObject( $proposedUser );
991 - $wgRequest->setSessionData( 'wsToken', $this->mToken );
 995+ $request->setSessionData( 'wsToken', $this->mToken );
992996 wfDebug( "User: logged in from $from\n" );
993997 return true;
994998 } else {
@@ -1166,7 +1170,6 @@
11671171 $this->mDatePreference = null;
11681172 $this->mBlockedby = -1; # Unset
11691173 $this->mHash = false;
1170 - $this->mSkin = null;
11711174 $this->mRights = null;
11721175 $this->mEffectiveGroups = null;
11731176 $this->mOptions = null;
@@ -2183,11 +2186,6 @@
21842187 $this->load();
21852188 $this->loadOptions();
21862189
2187 - if ( $oname == 'skin' ) {
2188 - # Clear cached skin, so the new one displays immediately in Special:Preferences
2189 - $this->mSkin = null;
2190 - }
2191 -
21922190 // Explicitly NULL values should refer to defaults
21932191 global $wgDefaultUserOptions;
21942192 if( is_null( $val ) && isset( $wgDefaultUserOptions[$oname] ) ) {
@@ -2468,6 +2466,20 @@
24692467 }
24702468
24712469 /**
 2470+ * Get the WebRequest object to use with this object
 2471+ *
 2472+ * @return WebRequest
 2473+ */
 2474+ public function getRequest() {
 2475+ if ( $this->mRequest ) {
 2476+ return $this->mRequest;
 2477+ } else {
 2478+ global $wgRequest;
 2479+ return $wgRequest;
 2480+ }
 2481+ }
 2482+
 2483+ /**
24722484 * Get the current skin, loading it if required
24732485 * @return Skin The current skin
24742486 * @todo FIXME: Need to check the old failback system [AV]
@@ -2625,8 +2637,7 @@
26262638 * if 0 or not specified, use the default $wgCookieExpiration
26272639 */
26282640 protected function setCookie( $name, $value, $exp = 0 ) {
2629 - global $wgRequest;
2630 - $wgRequest->response()->setcookie( $name, $value, $exp );
 2641+ $this->getRequest()->response()->setcookie( $name, $value, $exp );
26312642 }
26322643
26332644 /**
@@ -2645,8 +2656,7 @@
26462657 */
26472658 function setCookies( $request = null ) {
26482659 if ( $request === null ) {
2649 - global $wgRequest;
2650 - $request = $wgRequest;
 2660+ $request = $this->getRequest();
26512661 }
26522662
26532663 $this->load();
@@ -2695,11 +2705,9 @@
26962706 * @see logout()
26972707 */
26982708 function doLogout() {
2699 - global $wgRequest;
2700 -
27012709 $this->clearInstanceCache( 'defaults' );
27022710
2703 - $wgRequest->setSessionData( 'wsUserID', 0 );
 2711+ $this->getRequest()->setSessionData( 'wsUserID', 0 );
27042712
27052713 $this->clearCookie( 'UserID' );
27062714 $this->clearCookie( 'Token' );
@@ -3056,8 +3064,7 @@
30573065 */
30583066 function editToken( $salt = '', $request = null ) {
30593067 if ( $request == null ) {
3060 - global $wgRequest;
3061 - $request = $wgRequest;
 3068+ $request = $this->getRequest();
30623069 }
30633070
30643071 if ( $this->isAnon() ) {
Index: trunk/phase3/includes/RequestContext.php
@@ -120,10 +120,7 @@
121121 */
122122 public function getUser() {
123123 if ( !isset($this->user) ) {
124 - global $wgCommandLineMode;
125 - $this->user = $wgCommandLineMode
126 - ? new User
127 - : User::newFromSession( $this->getRequest() );
 124+ $this->user = User::newFromSession( $this->getRequest() );
128125 }
129126 return $this->user;
130127 }
@@ -347,4 +344,4 @@
348345 public function getSkin() {
349346 return $this->context->getSkin();
350347 }
351 -}
\ No newline at end of file
 348+}

Status & tagging log