Merge "Moved addAutopromoteOnceGroups() call to a deferred update"
[lhc/web/wiklou.git] / includes / WebRequest.php
index df88b35..a5fd9d8 100644 (file)
 class WebRequest {
        protected $data, $headers = array();
 
+       /**
+        * Flag to make WebRequest::getHeader return an array of values.
+        * @since 1.26
+        */
+       const GETHEADER_LIST = 1;
+
        /**
         * Lazy-init response object
         * @var WebResponse
@@ -50,6 +56,12 @@ class WebRequest {
         */
        private $ip;
 
+       /**
+        * The timestamp of the start of the request, with microsecond precision.
+        * @var float
+        */
+       protected $requestTime;
+
        /**
         * Cached URL protocol
         * @var string
@@ -57,9 +69,8 @@ class WebRequest {
        protected $protocol;
 
        public function __construct() {
-               if ( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc() ) {
-                       throw new MWException( "MediaWiki does not function when magic quotes are enabled." );
-               }
+               $this->requestTime = isset( $_SERVER['REQUEST_TIME_FLOAT'] )
+                       ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime( true );
 
                // POST overrides GET data
                // We don't use $_REQUEST here to avoid interference from cookies...
@@ -216,6 +227,17 @@ class WebRequest {
                }
        }
 
+       /**
+        * Get the number of seconds to have elapsed since request start,
+        * in fractional seconds, with microsecond resolution.
+        *
+        * @return float
+        * @since 1.25
+        */
+       public function getElapsedTime() {
+               return microtime( true ) - $this->requestTime;
+       }
+
        /**
         * Get the current URL protocol (http or https)
         * @return string
@@ -878,19 +900,28 @@ class WebRequest {
        }
 
        /**
-        * Get a request header, or false if it isn't set
-        * @param string $name Case-insensitive header name
+        * Get a request header, or false if it isn't set.
         *
-        * @return string|bool False on failure
-        */
-       public function getHeader( $name ) {
+        * @param string $name Case-insensitive header name
+        * @param int $flags Bitwise combination of:
+        *   WebRequest::GETHEADER_LIST  Treat the header as a comma-separated list
+        *                               of values, as described in RFC 2616 ยง 4.2.
+        *                               (since 1.26).
+        * @return string|array|bool False if header is unset; otherwise the
+        *  header value(s) as either a string (the default) or an array, if
+        *  WebRequest::GETHEADER_LIST flag was set.
+        */
+       public function getHeader( $name, $flags = 0 ) {
                $this->initHeaders();
                $name = strtoupper( $name );
-               if ( isset( $this->headers[$name] ) ) {
-                       return $this->headers[$name];
-               } else {
+               if ( !isset( $this->headers[$name] ) ) {
                        return false;
                }
+               $value = $this->headers[$name];
+               if ( $flags & self::GETHEADER_LIST ) {
+                       $value = array_map( 'trim', explode( ',', $value ) );
+               }
+               return $value;
        }
 
        /**
@@ -1274,6 +1305,8 @@ class FauxRequest extends WebRequest {
        public function __construct( $data = array(), $wasPosted = false,
                $session = null, $protocol = 'http'
        ) {
+               $this->requestTime = microtime( true );
+
                if ( is_array( $data ) ) {
                        $this->data = $data;
                } else {
@@ -1356,13 +1389,8 @@ class FauxRequest extends WebRequest {
                return $this->protocol;
        }
 
-       /**
-        * @param string $name The name of the header to get (case insensitive).
-        * @return bool|string
-        */
-       public function getHeader( $name ) {
-               $name = strtoupper( $name );
-               return isset( $this->headers[$name] ) ? $this->headers[$name] : false;
+       private function initHeaders() {
+               return;
        }
 
        /**
@@ -1470,8 +1498,8 @@ class DerivativeRequest extends FauxRequest {
                return $this->base->checkSessionCookie();
        }
 
-       public function getHeader( $name ) {
-               return $this->base->getHeader( $name );
+       public function getHeader( $name, $flags = 0 ) {
+               return $this->base->getHeader( $name, $flags );
        }
 
        public function getAllHeaders() {
@@ -1497,4 +1525,8 @@ class DerivativeRequest extends FauxRequest {
        public function getProtocol() {
                return $this->base->getProtocol();
        }
+
+       public function getElapsedTime() {
+               return $this->base->getElapsedTime();
+       }
 }