Merge "Add note that IP::isInRange() can return unexpected results for invalid args"
[lhc/web/wiklou.git] / includes / WebRequest.php
index ce5cb96..b18d59c 100644 (file)
@@ -23,6 +23,8 @@
  * @file
  */
 
+use MediaWiki\Session\Session;
+use MediaWiki\Session\SessionId;
 use MediaWiki\Session\SessionManager;
 
 /**
@@ -66,7 +68,7 @@ class WebRequest {
        protected $protocol;
 
        /**
-        * @var \\MediaWiki\\Session\\SessionId|null Session ID to use for this
+        * @var 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).
         */
@@ -246,6 +248,25 @@ class WebRequest {
                return microtime( true ) - $this->requestTime;
        }
 
+       /**
+        * Get the unique request ID.
+        * This is either the value of the UNIQUE_ID envvar (if present) or a
+        * randomly-generated 24-character string.
+        *
+        * @return string
+        * @since 1.27
+        */
+       public static function getRequestId() {
+               static $reqId;
+
+               if ( !$reqId ) {
+                       $reqId = isset( $_SERVER['UNIQUE_ID'] )
+                               ? $_SERVER['UNIQUE_ID'] : wfRandomString( 24 );
+               }
+
+               return $reqId;
+       }
+
        /**
         * Get the current URL protocol (http or https)
         * @return string
@@ -651,7 +672,7 @@ class WebRequest {
         * @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
+        * @return Session
         */
        public function getSession() {
                if ( $this->sessionId !== null ) {
@@ -669,18 +690,18 @@ class WebRequest {
        /**
         * Set the session for this request
         * @since 1.27
-        * @private For use by MediaWiki\\Session classes only
-        * @param MediaWiki\\Session\\SessionId $sessionId
+        * @private For use by MediaWiki\Session classes only
+        * @param SessionId $sessionId
         */
-       public function setSessionId( MediaWiki\Session\SessionId $sessionId ) {
+       public function setSessionId( 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
+        * @private For use by MediaWiki\Session classes only
+        * @return SessionId|null
         */
        public function getSessionId() {
                return $this->sessionId;
@@ -691,7 +712,7 @@ class WebRequest {
         * This does not necessarily mean that the user is logged in!
         *
         * @deprecated since 1.27, use
-        *  \\MediaWiki\\Session\\SessionManager::singleton()->getPersistedSessionId()
+        *  \MediaWiki\Session\SessionManager::singleton()->getPersistedSessionId()
         *  instead.
         * @return bool
         */
@@ -719,13 +740,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'] )
@@ -762,6 +783,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