r82686 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82685‎ | r82686 | r82687 >
Date:17:54, 23 February 2011
Author:ialex
Status:resolved (Comments)
Tags:
Comment:
Use $wgRequest to get and set session items instead of $_SESSION (as for cookies)
Modified paths:
  • /trunk/phase3/includes/User.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/User.php
@@ -873,32 +873,30 @@
874874 }
875875 }
876876
877 - if ( $wgRequest->getCookie( 'UserID' ) !== null ) {
878 - $sId = intval( $wgRequest->getCookie( 'UserID' ) );
879 - if( isset( $_SESSION['wsUserID'] ) && $sId != $_SESSION['wsUserID'] ) {
 877+ $cookieId = $wgRequest->getCookie( 'UserID' );
 878+ $sessId = $wgRequest->getSessionData( 'wsUserID' );
 879+
 880+ if ( $cookieId !== null ) {
 881+ $sId = intval( $cookieId );
 882+ if( $sessId !== null && $cookieId != $sessId ) {
880883 $this->loadDefaults(); // Possible collision!
881 - wfDebugLog( 'loginSessions', "Session user ID ({$_SESSION['wsUserID']}) and
 884+ wfDebugLog( 'loginSessions', "Session user ID ($sessId) and
882885 cookie user ID ($sId) don't match!" );
883886 return false;
884887 }
885 - $_SESSION['wsUserID'] = $sId;
886 - } else if ( isset( $_SESSION['wsUserID'] ) ) {
887 - if ( $_SESSION['wsUserID'] != 0 ) {
888 - $sId = $_SESSION['wsUserID'];
889 - } else {
890 - $this->loadDefaults();
891 - return false;
892 - }
 888+ $wgRequest->setSessionData( 'wsUserID', $sId );
 889+ } else if ( $sessId !== null && $sessId != 0 ) {
 890+ $sId = $sessId;
893891 } else {
894892 $this->loadDefaults();
895893 return false;
896894 }
897895
898 - if ( isset( $_SESSION['wsUserName'] ) ) {
899 - $sName = $_SESSION['wsUserName'];
900 - } else if ( $wgRequest->getCookie('UserName') !== null ) {
901 - $sName = $wgRequest->getCookie('UserName');
902 - $_SESSION['wsUserName'] = $sName;
 896+ if ( $wgRequest->getSessionData( 'wsUserName' ) !== null ) {
 897+ $sName = $wgRequest->getSessionData( 'wsUserName' );
 898+ } else if ( $wgRequest->getCookie( 'UserName' ) !== null ) {
 899+ $sName = $wgRequest->getCookie( 'UserName' );
 900+ $wgRequest->setSessionData( 'wsUserName', $sName );
903901 } else {
904902 $this->loadDefaults();
905903 return false;
@@ -917,8 +915,8 @@
918916 return false;
919917 }
920918
921 - if ( isset( $_SESSION['wsToken'] ) ) {
922 - $passwordCorrect = $_SESSION['wsToken'] == $this->mToken;
 919+ if ( $wgRequest->getSessionData( 'wsToken' ) !== null ) {
 920+ $passwordCorrect = $this->mToken == $wgRequest->getSessionData( 'wsToken' );
923921 $from = 'session';
924922 } else if ( $wgRequest->getCookie( 'Token' ) !== null ) {
925923 $passwordCorrect = $this->mToken == $wgRequest->getCookie( 'Token' );
@@ -930,7 +928,7 @@
931929 }
932930
933931 if ( ( $sName == $this->mName ) && $passwordCorrect ) {
934 - $_SESSION['wsToken'] = $this->mToken;
 932+ $wgRequest->setSessionData( 'wsToken', $this->mToken );
935933 wfDebug( "User: logged in from $from\n" );
936934 return true;
937935 } else {
@@ -2453,6 +2451,8 @@
24542452 * Set the default cookies for this session on the user's client.
24552453 */
24562454 function setCookies() {
 2455+ global $wgRequest;
 2456+
24572457 $this->load();
24582458 if ( 0 == $this->mId ) return;
24592459 $session = array(
@@ -2471,9 +2471,9 @@
24722472 }
24732473
24742474 wfRunHooks( 'UserSetCookies', array( $this, &$session, &$cookies ) );
2475 - #check for null, since the hook could cause a null value
2476 - if ( !is_null( $session ) && isset( $_SESSION ) ){
2477 - $_SESSION = $session + $_SESSION;
 2475+
 2476+ foreach ( $session as $name => $value ) {
 2477+ $wgRequest->setSessionData( $name, $value );
24782478 }
24792479 foreach ( $cookies as $name => $value ) {
24802480 if ( $value === false ) {
@@ -2499,9 +2499,11 @@
25002500 * @see logout()
25012501 */
25022502 function doLogout() {
 2503+ global $wgRequest;
 2504+
25032505 $this->clearInstanceCache( 'defaults' );
25042506
2505 - $_SESSION['wsUserID'] = 0;
 2507+ $wgRequest->setSessionData( 'wsUserID', 0 );
25062508
25072509 $this->clearCookie( 'UserID' );
25082510 $this->clearCookie( 'Token' );
@@ -2856,14 +2858,15 @@
28572859 * @return String The new edit token
28582860 */
28592861 function editToken( $salt = '' ) {
 2862+ global $wgRequest;
 2863+
28602864 if ( $this->isAnon() ) {
28612865 return EDIT_TOKEN_SUFFIX;
28622866 } else {
2863 - if( !isset( $_SESSION['wsEditToken'] ) ) {
 2867+ $token = $wgRequest->getSessionData( 'wsEditToken' );
 2868+ if ( $token === null ) {
28642869 $token = self::generateToken();
2865 - $_SESSION['wsEditToken'] = $token;
2866 - } else {
2867 - $token = $_SESSION['wsEditToken'];
 2870+ $wgRequest->setSessionData( 'wsEditToken', $token );
28682871 }
28692872 if( is_array( $salt ) ) {
28702873 $salt = implode( '|', $salt );

Follow-up revisions

RevisionCommit summaryAuthorDate
r83080Per Platonides, fix for r82686: make ApiUploadTest work again...ialex12:52, 2 March 2011
r84007Fix bug 27887, caused by r82686: inject session data into FauxRequest s used ...werdna11:38, 15 March 2011

Comments

#Comment by Platonides (talk | contribs)   21:38, 1 March 2011

Breaks tests:

  • ApiUploadTest::testUploadMissingParams
  • ApiUploadTest::testUploadZeroLength
#Comment by IAlex (talk | contribs)   13:04, 2 March 2011

Fixed in r83080.

#Comment by Siebrand (talk | contribs)   10:32, 14 March 2011

According to Tim in comment 5 of bug 27887, this causes a serious issue in LiquidThreads we severely suffer from at translatewiki.net.

Status & tagging log