It's not guaranteed that loadSessionFromStore() will succeed after
whatever alterations the SessionProvider might have made later in the
request.
So instead, let's make a new global object that stores the SessionId
of the persistent session that was loaded during Setup.php, if any. Then
we can check that when we need to know whether the session was
persisted.
Bug: T124468
Change-Id: I1e8e616c83b16aadd86b0a0a40826d40f6e8abe4
// Set up the session
$ps_session = Profiler::instance()->scopedProfileIn( $fname . '-session' );
// Set up the session
$ps_session = Profiler::instance()->scopedProfileIn( $fname . '-session' );
+/**
+ * @var MediaWiki\\Session\\SessionId|null $wgInitialSessionId The persistent
+ * session ID (if any) loaded at startup
+ */
+$wgInitialSessionId = null;
if ( !defined( 'MW_NO_SESSION' ) && !$wgCommandLineMode ) {
// If session.auto_start is there, we can't touch session name
if ( $wgPHPSessionHandling !== 'disable' && !wfIniGetBool( 'session.auto_start' ) ) {
if ( !defined( 'MW_NO_SESSION' ) && !$wgCommandLineMode ) {
// If session.auto_start is there, we can't touch session name
if ( $wgPHPSessionHandling !== 'disable' && !wfIniGetBool( 'session.auto_start' ) ) {
+ if ( $session->isPersistent() ) {
+ $wgInitialSessionId = $session->getSessionId();
+ }
+
$session->renew();
if ( MediaWiki\Session\PHPSessionHandler::isEnabled() &&
( $session->isPersistent() || $session->shouldRememberUser() )
$session->renew();
if ( MediaWiki\Session\PHPSessionHandler::isEnabled() &&
( $session->isPersistent() || $session->shouldRememberUser() )
* @return bool
*/
public function checkSessionCookie() {
* @return bool
*/
public function checkSessionCookie() {
+ global $wgInitialSessionId;
wfDeprecated( __METHOD__, '1.27' );
wfDeprecated( __METHOD__, '1.27' );
- return SessionManager::singleton()->getPersistedSessionId( $this ) !== null;
+ return $wgInitialSessionId !== null &&
+ $this->getSession()->getId() === (string)$wgInitialSessionId;
$this->logger = $logger;
}
$this->logger = $logger;
}
- public function getPersistedSessionId( WebRequest $request ) {
- $info = $this->getSessionInfoForRequest( $request );
- if ( $info && $info->wasPersisted() ) {
- return $info->getId();
- } else {
- return null;
- }
- }
-
public function getSessionForRequest( WebRequest $request ) {
$info = $this->getSessionInfoForRequest( $request );
public function getSessionForRequest( WebRequest $request ) {
$info = $this->getSessionInfoForRequest( $request );
* @since 1.27
*/
interface SessionManagerInterface extends LoggerAwareInterface {
* @since 1.27
*/
interface SessionManagerInterface extends LoggerAwareInterface {
- /**
- * Fetch the persisted session ID in a request.
- *
- * Note this is not the same thing as whether the session associated with
- * the request is currently persistent, as the session might have been
- * first made persistent during this request.
- *
- * @param WebRequest $request
- * @return string|null
- * @throws \\OverflowException if there are multiple sessions tied for top
- * priority in the request. Exception has a property "sessionInfos"
- * holding the SessionInfo objects for the sessions involved.
- */
- public function getPersistedSessionId( WebRequest $request );
-
/**
* Fetch the session for a request
*
/**
* Fetch the session for a request
*
* @return bool
*/
function hasSessionCookie() {
* @return bool
*/
function hasSessionCookie() {
- global $wgDisableCookieCheck;
+ global $wgDisableCookieCheck, $wgInitialSessionId;
- return $wgDisableCookieCheck ||
- SessionManager::singleton()->getPersistedSessionId( $this->getRequest() ) !== null;
+ return $wgDisableCookieCheck || (
+ $wgInitialSessionId &&
+ $this->getRequest()->getSession()->getId() === (string)$wgInitialSessionId
+ );
$session = $manager->getSessionForRequest( $request );
$this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
$this->assertSame( $idEmpty, $session->getId() );
$session = $manager->getSessionForRequest( $request );
$this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
$this->assertSame( $idEmpty, $session->getId() );
- $this->assertNull( $manager->getPersistedSessionId( $request ) );
// Both providers return info, picks best one
$request->info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 1, array(
// Both providers return info, picks best one
$request->info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 1, array(
$session = $manager->getSessionForRequest( $request );
$this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
$this->assertSame( $id2, $session->getId() );
$session = $manager->getSessionForRequest( $request );
$this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
$this->assertSame( $id2, $session->getId() );
- $this->assertSame( $id2, $manager->getPersistedSessionId( $request ) );
$request->info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 2, array(
'provider' => $provider1,
$request->info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 2, array(
'provider' => $provider1,
$session = $manager->getSessionForRequest( $request );
$this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
$this->assertSame( $id1, $session->getId() );
$session = $manager->getSessionForRequest( $request );
$this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
$this->assertSame( $id1, $session->getId() );
- $this->assertSame( $id1, $manager->getPersistedSessionId( $request ) );
// Tied priorities
$request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, array(
// Tied priorities
$request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, array(
$this->assertContains( $request->info1, $ex->sessionInfos );
$this->assertContains( $request->info2, $ex->sessionInfos );
}
$this->assertContains( $request->info1, $ex->sessionInfos );
$this->assertContains( $request->info2, $ex->sessionInfos );
}
- try {
- $manager->getPersistedSessionId( $request );
- $this->fail( 'Expcected exception not thrown' );
- } catch ( \OverFlowException $ex ) {
- $this->assertStringStartsWith(
- 'Multiple sessions for this request tied for top priority: ',
- $ex->getMessage()
- );
- $this->assertCount( 2, $ex->sessionInfos );
- $this->assertContains( $request->info1, $ex->sessionInfos );
- $this->assertContains( $request->info2, $ex->sessionInfos );
- }
// Bad provider
$request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, array(
// Bad provider
$request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, array(
- try {
- $manager->getPersistedSessionId( $request );
- $this->fail( 'Expcected exception not thrown' );
- } catch ( \UnexpectedValueException $ex ) {
- $this->assertSame(
- 'Provider1 returned session info for a different provider: ' . $request->info1,
- $ex->getMessage()
- );
- }
// Unusable session info
$this->logger->setCollect( true );
// Unusable session info
$this->logger->setCollect( true );
$session = $manager->getSessionForRequest( $request );
$this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
$this->assertSame( $id2, $session->getId() );
$session = $manager->getSessionForRequest( $request );
$this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
$this->assertSame( $id2, $session->getId() );
- $this->assertSame( $id2, $manager->getPersistedSessionId( $request ) );
$this->logger->setCollect( false );
// Unpersisted session ID
$this->logger->setCollect( false );
// Unpersisted session ID
$this->assertSame( $id1, $session->getId() );
$session->persist();
$this->assertTrue( $session->isPersistent(), 'sanity check' );
$this->assertSame( $id1, $session->getId() );
$session->persist();
$this->assertTrue( $session->isPersistent(), 'sanity check' );
- $this->assertNull( $manager->getPersistedSessionId( $request ) );
}
public function testGetSessionById() {
}
public function testGetSessionById() {