Call Skin::setRelevantUser on Special:EmailUser
[lhc/web/wiklou.git] / includes / WebRequest.php
index 7b76592..812a320 100644 (file)
@@ -23,6 +23,8 @@
  * @file
  */
 
+use MediaWiki\Session\SessionManager;
+
 /**
  * The WebRequest class encapsulates getting at data passed in the
  * URL or via a POSTed form stripping illegal input characters and
@@ -31,7 +33,7 @@
  * @ingroup HTTP
  */
 class WebRequest {
-       protected $data, $headers = array();
+       protected $data, $headers = [];
 
        /**
         * Flag to make WebRequest::getHeader return an array of values.
@@ -63,6 +65,13 @@ class WebRequest {
         */
        protected $protocol;
 
+       /**
+        * @var \\MediaWiki\\Session\\SessionId|null Session ID to use for this
+        *  request. We can't save the session directly due to reference cycles not
+        *  working too well (slow GC in Zend and never collected in HHVM).
+        */
+       protected $sessionId = null;
+
        public function __construct() {
                $this->requestTime = isset( $_SERVER['REQUEST_TIME_FLOAT'] )
                        ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime( true );
@@ -92,7 +101,7 @@ class WebRequest {
                // PATH_INFO is mangled due to http://bugs.php.net/bug.php?id=31892
                // And also by Apache 2.x, double slashes are converted to single slashes.
                // So we will use REQUEST_URI if possible.
-               $matches = array();
+               $matches = [];
                if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
                        // Slurp out the path portion to examine...
                        $url = $_SERVER['REQUEST_URI'];
@@ -133,18 +142,18 @@ class WebRequest {
 
                                global $wgActionPaths;
                                if ( $wgActionPaths ) {
-                                       $router->add( $wgActionPaths, array( 'action' => '$key' ) );
+                                       $router->add( $wgActionPaths, [ 'action' => '$key' ] );
                                }
 
                                global $wgVariantArticlePath, $wgContLang;
                                if ( $wgVariantArticlePath ) {
                                        $router->add( $wgVariantArticlePath,
-                                               array( 'variant' => '$2' ),
-                                               array( '$2' => $wgContLang->getVariants() )
+                                               [ 'variant' => '$2' ],
+                                               [ '$2' => $wgContLang->getVariants() ]
                                        );
                                }
 
-                               Hooks::run( 'WebRequestPathInfoRouter', array( $router ) );
+                               Hooks::run( 'WebRequestPathInfoRouter', [ $router ] );
 
                                $matches = $router->parse( $path );
                        }
@@ -176,7 +185,7 @@ class WebRequest {
                $proto = self::detectProtocol();
                $stdPort = $proto === 'https' ? 443 : 80;
 
-               $varNames = array( 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' );
+               $varNames = [ 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' ];
                $host = 'localhost';
                $port = $stdPort;
                foreach ( $varNames as $varName ) {
@@ -285,7 +294,7 @@ class WebRequest {
                        if ( substr( $path, 0, $baseLen ) == $base ) {
                                $raw = substr( $path, $baseLen );
                                if ( $raw !== '' ) {
-                                       $matches = array( 'title' => rawurldecode( $raw ) );
+                                       $matches = [ 'title' => rawurldecode( $raw ) ];
                                        if ( $key ) {
                                                $matches[$key] = $keyValue;
                                        }
@@ -293,7 +302,7 @@ class WebRequest {
                                }
                        }
                }
-               return array();
+               return [];
        }
 
        /**
@@ -547,7 +556,7 @@ class WebRequest {
                        $names = array_keys( $this->data );
                }
 
-               $retVal = array();
+               $retVal = [];
                foreach ( $names as $name ) {
                        $value = $this->getGPCVal( $this->data, $name, null );
                        if ( !is_null( $value ) ) {
@@ -563,7 +572,7 @@ class WebRequest {
         * @param array $exclude
         * @return array
         */
-       public function getValueNames( $exclude = array() ) {
+       public function getValueNames( $exclude = [] ) {
                return array_diff( array_keys( $this->getValues() ), $exclude );
        }
 
@@ -638,18 +647,59 @@ class WebRequest {
        }
 
        /**
-        * Returns true if there is a session cookie set.
+        * Return the session for this request
+        * @since 1.27
+        * @note For performance, keep the session locally if you will be making
+        *  much use of it instead of calling this method repeatedly.
+        * @return MediaWiki\\Session\\Session
+        */
+       public function getSession() {
+               if ( $this->sessionId !== null ) {
+                       $session = SessionManager::singleton()->getSessionById( (string)$this->sessionId, true, $this );
+                       if ( $session ) {
+                               return $session;
+                       }
+               }
+
+               $session = SessionManager::singleton()->getSessionForRequest( $this );
+               $this->sessionId = $session->getSessionId();
+               return $session;
+       }
+
+       /**
+        * Set the session for this request
+        * @since 1.27
+        * @private For use by MediaWiki\\Session classes only
+        * @param MediaWiki\\Session\\SessionId $sessionId
+        */
+       public function setSessionId( MediaWiki\Session\SessionId $sessionId ) {
+               $this->sessionId = $sessionId;
+       }
+
+       /**
+        * Get the session id for this request, if any
+        * @since 1.27
+        * @private For use by MediaWiki\\Session classes only
+        * @return MediaWiki\\Session\\SessionId|null
+        */
+       public function getSessionId() {
+               return $this->sessionId;
+       }
+
+       /**
+        * Returns true if the request has a persistent session.
         * This does not necessarily mean that the user is logged in!
         *
-        * If you want to check for an open session, use session_id()
-        * instead; that will also tell you if the session was opened
-        * during the current request (in which case the cookie will
-        * be sent back to the client at the end of the script run).
-        *
+        * @deprecated since 1.27, use
+        *  \\MediaWiki\\Session\\SessionManager::singleton()->getPersistedSessionId()
+        *  instead.
         * @return bool
         */
        public function checkSessionCookie() {
-               return isset( $_COOKIE[session_name()] );
+               global $wgInitialSessionId;
+               wfDeprecated( __METHOD__, '1.27' );
+               return $wgInitialSessionId !== null &&
+                       $this->getSession()->getId() === (string)$wgInitialSessionId;
        }
 
        /**
@@ -669,13 +719,13 @@ class WebRequest {
        }
 
        /**
-        * Return the path and query string portion of the request URI.
+        * Return the path and query string portion of the main request URI.
         * This will be suitable for use as a relative link in HTML output.
         *
         * @throws MWException
         * @return string
         */
-       public function getRequestURL() {
+       public static function getGlobalRequestURL() {
                if ( isset( $_SERVER['REQUEST_URI'] ) && strlen( $_SERVER['REQUEST_URI'] ) ) {
                        $base = $_SERVER['REQUEST_URI'];
                } elseif ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] )
@@ -712,6 +762,17 @@ class WebRequest {
                }
        }
 
+       /**
+        * Return the path and query string portion of the request URI.
+        * This will be suitable for use as a relative link in HTML output.
+        *
+        * @throws MWException
+        * @return string
+        */
+       public function getRequestURL() {
+               return self::getGlobalRequestURL();
+       }
+
        /**
         * Return the request URI with the canonical service and hostname, path,
         * and query string. This will be suitable for use as an absolute link
@@ -732,7 +793,7 @@ class WebRequest {
         * @return string
         */
        public function appendQueryValue( $key, $value ) {
-               return $this->appendQueryArray( array( $key => $value ) );
+               return $this->appendQueryArray( [ $key => $value ] );
        }
 
        /**
@@ -780,7 +841,7 @@ class WebRequest {
                        $offset = 0;
                }
 
-               return array( $limit, $offset );
+               return [ $limit, $offset ];
        }
 
        /**
@@ -907,26 +968,25 @@ class WebRequest {
        }
 
        /**
-        * Get data from $_SESSION
+        * Get data from the session
         *
-        * @param string $key Name of key in $_SESSION
+        * @note Prefer $this->getSession() instead if making multiple calls.
+        * @param string $key Name of key in the session
         * @return mixed
         */
        public function getSessionData( $key ) {
-               if ( !isset( $_SESSION[$key] ) ) {
-                       return null;
-               }
-               return $_SESSION[$key];
+               return $this->getSession()->get( $key );
        }
 
        /**
         * Set session data
         *
-        * @param string $key Name of key in $_SESSION
+        * @note Prefer $this->getSession() instead if making multiple calls.
+        * @param string $key Name of key in the session
         * @param mixed $data
         */
        public function setSessionData( $key, $data ) {
-               $_SESSION[$key] = $data;
+               return $this->getSession()->set( $key, $data );
        }
 
        /**
@@ -939,7 +999,7 @@ class WebRequest {
         * @throws HttpError
         * @return bool
         */
-       public function checkUrlExtension( $extWhitelist = array() ) {
+       public function checkUrlExtension( $extWhitelist = [] ) {
                $extWhitelist[] = 'php';
                if ( IEUrlExtension::areServerVarsBad( $_SERVER, $extWhitelist ) ) {
                        if ( !$this->wasPosted() ) {
@@ -1003,7 +1063,7 @@ HTML;
                // http://www.thefutureoftheweb.com/blog/use-accept-language-header
                $acceptLang = $this->getHeader( 'Accept-Language' );
                if ( !$acceptLang ) {
-                       return array();
+                       return [];
                }
 
                // Return the language codes in lower case
@@ -1018,7 +1078,7 @@ HTML;
                );
 
                if ( !count( $lang_parse[1] ) ) {
-                       return array();
+                       return [];
                }
 
                $langcodes = $lang_parse[1];
@@ -1130,7 +1190,7 @@ HTML;
                }
 
                # Allow extensions to improve our guess
-               Hooks::run( 'GetIP', array( &$ip ) );
+               Hooks::run( 'GetIP', [ &$ip ] );
 
                if ( !$ip ) {
                        throw new MWException( "Unable to determine IP." );