* Don't create a WebRequest obhject in CLI mode but a FauxRequest; avoids some useles...
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Tue, 5 Jul 2011 15:05:14 +0000 (15:05 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Tue, 5 Jul 2011 15:05:14 +0000 (15:05 +0000)
* Added HTTP response code parsing (sending a "HTTP/1.x code" header was throwing a NOTICE about undefined index on the result of the explode() call) and storage; added FauxResponse::getStatusCode() to retrieve it

includes/Setup.php
includes/WebResponse.php

index 4f975f1..82bf442 100644 (file)
@@ -364,14 +364,16 @@ if( is_null( $wgLocalTZoffset ) ) {
        $wgLocalTZoffset = date( 'Z' ) / 60;
 }
 
-# Can't stub this one, it sets up $_GET and $_REQUEST in its constructor
-$wgRequest = new WebRequest;
-
 # Useful debug output
 global $wgCommandLineMode;
 if ( $wgCommandLineMode ) {
+       $wgRequest = new FauxRequest( array() );
+
        wfDebug( "\n\nStart command line script $self\n" );
 } else {
+       # Can't stub this one, it sets up $_GET and $_REQUEST in its constructor
+       $wgRequest = new WebRequest;
+
        $debug = "Start request\n\n{$_SERVER['REQUEST_METHOD']} {$wgRequest->getRequestURL()}";
 
        if ( $wgDebugPrintHttpHeaders ) {
index 6241041..a2d257c 100644 (file)
@@ -77,6 +77,7 @@ class WebResponse {
 class FauxResponse extends WebResponse {
        private $headers;
        private $cookies;
+       private $code;
 
        /**
         * Stores a HTTP header
@@ -85,10 +86,19 @@ class FauxResponse extends WebResponse {
         * @param $http_response_code null|int Forces the HTTP response code to the specified value.
         */
        public function header( $string, $replace = true, $http_response_code = null ) {
-               list( $key, $val ) = explode( ":", $string, 2 );
+               $match = array();
+               if ( preg_match( '~^HTTP/1.\d (\d+)\D*$~', $string, $match ) ) {
+                       $this->code = intval( $match[1] );
+               } else {
+                       list( $key, $val ) = explode( ":", $string, 2 );
 
-               if( $replace || !isset( $this->headers[$key] ) ) {
-                       $this->headers[$key] = $val;
+                       if( $replace || !isset( $this->headers[$key] ) ) {
+                               $this->headers[$key] = $val;
+                       }
+               }
+
+               if ( $http_response_code !== null ) {
+                       $this->code = intval( $http_response_code );
                }
        }
 
@@ -100,6 +110,15 @@ class FauxResponse extends WebResponse {
                return $this->headers[$key];
        }
 
+       /**
+        * Get the HTTP response code, null if not set
+        *
+        * @return Int or null
+        */
+       public function getStatusCode() {
+               return $this->code;
+       }
+
        /**
         * @param $name String: name of cookie
         * @param $value String: value to give cookie