Easier to use addWikiText() in wrapWikiMsg()
[lhc/web/wiklou.git] / includes / HttpFunctions.php
index 0dccebb..2d3d8c3 100644 (file)
@@ -15,7 +15,7 @@ class Http {
         *
         * @param $method String: HTTP method. Usually GET/POST
         * @param $url String: full URL to act on
-        * @param $options Array: options to pass to HttpRequest object.
+        * @param $options Array: options to pass to MWHttpRequest object.
         *      Possible keys for the array:
         *    - timeout             Timeout length in seconds
         *    - postData            An array of key-value pairs or a url-encoded form data
@@ -40,7 +40,7 @@ class Http {
                        $options['timeout'] = 'default';
                }
 
-               $req = HttpRequest::factory( $url, $options );
+               $req = MWHttpRequest::factory( $url, $options );
                $status = $req->execute();
 
                if ( $status->isOK() ) {
@@ -133,8 +133,11 @@ class Http {
 /**
  * This wrapper class will call out to curl (if available) or fallback
  * to regular PHP if necessary for handling internal HTTP requests.
+ *
+ * Renamed from HttpRequest to MWHttpRequst to avoid conflict with
+ * php's HTTP extension.
  */
-class HttpRequest {
+class MWHttpRequest {
        protected $content;
        protected $timeout = 'default';
        protected $headersOnly = null;
@@ -195,7 +198,7 @@ class HttpRequest {
 
        /**
         * Generate a new request object
-        * @see HttpRequest::__construct
+        * @see MWHttpRequest::__construct
         */
        public static function factory( $url, $options = null ) {
                if ( !Http::$httpEngine ) {
@@ -386,8 +389,12 @@ class HttpRequest {
        }
 
        /**
-        * Sets the member variable status to a fatal status if the HTTP
-        * status code was not 200.
+        * Sets HTTPRequest status member to a fatal value with the error
+        * message if the returned integer value of the status code was
+        * not successful (< 300) or a redirect (>=300 and < 400).  (see
+        * RFC2616, section 10,
+        * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a
+        * list of status codes.)
         *
         * @return nothing
         */
@@ -396,12 +403,27 @@ class HttpRequest {
                        $this->parseHeader();
                }
 
-               if ( (int)$this->respStatus !== 200 ) {
+               if ( (int)$this->respStatus > 399 ) {
                        list( $code, $message ) = explode( " ", $this->respStatus, 2 );
                        $this->status->fatal( "http-bad-status", $code, $message );
                }
        }
 
+       /**
+        * Get the integer value of the HTTP status code (e.g. 200 for "200 Ok")
+        * (see RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
+        * for a list of status codes.)
+        *
+        * @return Integer
+        */
+       public function getStatus() {
+               if ( !$this->respHeaders ) {
+                       $this->parseHeader();
+               }
+
+               return (int)$this->respStatus;
+       }
+
 
        /**
         * Returns true if the last status code was a redirect.
@@ -458,7 +480,7 @@ class HttpRequest {
        }
 
        /**
-        * Tells the HttpRequest object to use this pre-loaded CookieJar.
+        * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
         *
         * @param $jar CookieJar
         */
@@ -773,9 +795,9 @@ class CookieJar {
 }
 
 /**
- * HttpRequest implemented using internal curl compiled into PHP
+ * MWHttpRequest implemented using internal curl compiled into PHP
  */
-class CurlHttpRequest extends HttpRequest {
+class CurlHttpRequest extends MWHttpRequest {
        static $curlMessageMap = array(
                6 => 'http-host-unreachable',
                28 => 'http-timed-out'
@@ -845,12 +867,14 @@ class CurlHttpRequest extends HttpRequest {
                }
 
                if ( $this->followRedirects && $this->canFollowRedirects() ) {
-                       if ( ! @curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
+                       wfSuppressWarnings();
+                       if ( ! curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
                                wfDebug( __METHOD__ . ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
                                        "Probably safe_mode or open_basedir is set.\n" );
                                // Continue the processing. If it were in curl_setopt_array,
                                // processing would have halted on its entry
                        }
+                       wfRestoreWarnings();
                }
 
                if ( false === curl_exec( $curlHandle ) ) {
@@ -888,7 +912,7 @@ class CurlHttpRequest extends HttpRequest {
        }
 }
 
-class PhpHttpRequest extends HttpRequest {
+class PhpHttpRequest extends MWHttpRequest {
        protected function urlToTcp( $url ) {
                $parsedUrl = parse_url( $url );