* Added HttpError exception as replacement for wfHttpError(); changed alls core calls...
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Fri, 16 Sep 2011 18:50:13 +0000 (18:50 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Fri, 16 Sep 2011 18:50:13 +0000 (18:50 +0000)
* Changed FeedUtils' call to it to be similar than feeds are completely disabled
* Use local context instead of global variables in Special:Userlogout

includes/Exception.php
includes/FeedUtils.php
includes/Metadata.php
includes/WebRequest.php
includes/Wiki.php
includes/specials/SpecialUploadStash.php
includes/specials/SpecialUserlogout.php

index fb5f0ff..432b768 100644 (file)
@@ -366,6 +366,55 @@ class UserBlockedError extends ErrorPageError {
        }
 }
 
+/**
+ * Show an error that looks like an HTTP server error.
+ * Replacement for wfHttpError().
+ *
+ * @ingroup Exception
+ */
+class HttpError extends MWException {
+       private $httpCode, $header, $content;
+
+       /**
+        * Constructor
+        *
+        * @param $httpCode Integer: HTTP status code to send to the client
+        * @param $content String|Message: content of the message
+        * @param $header String|Message: content of the header (\<title\> and \<h1\>)
+        */
+       public function __construct( $httpCode, $content, $header = null ){
+               parent::__construct( $content );
+               $this->httpCode = (int)$httpCode;
+               $this->header = $header;
+               $this->content = $content;
+       }
+
+       public function reportHTML() {
+               $httpMessage = HttpStatus::getMessage( $this->httpCode );
+
+               header( "Status: {$this->httpCode} {$httpMessage}" );
+               header( 'Content-type: text/html; charset=utf-8' );
+
+               if ( $this->header === null ) {
+                       $header = $httpMessage;
+               } elseif ( $this->header instanceof Message ) {
+                       $header = $this->header->escaped();
+               } else {
+                       $header = htmlspecialchars( $this->header );
+               }
+
+               if ( $this->content instanceof Message ) {
+                       $content = $this->content->escaped();
+               } else {
+                       $content = htmlspecialchars( $this->content );
+               }
+
+               print "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n".
+                       "<html><head><title>$header</title></head>\n" .
+                       "<body><h1>$header</h1><p>$content</p></body></html>\n";
+       }
+}
+
 /**
  * Handler class for MWExceptions
  * @ingroup Exception
index e923a28..b6df0c6 100644 (file)
@@ -31,16 +31,15 @@ class FeedUtils {
         * @return Boolean
         */
        public static function checkFeedOutput( $type ) {
-               global $wgFeed, $wgFeedClasses;
+               global $wgOut, $wgFeed, $wgFeedClasses;
 
                if ( !$wgFeed ) {
-                       global $wgOut;
                        $wgOut->addWikiMsg( 'feed-unavailable' );
                        return false;
                }
 
                if( !isset( $wgFeedClasses[$type] ) ) {
-                       wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
+                       $wgOut->addWikiMsg( 'feed-invalid' );
                        return false;
                }
 
index 2e4ab94..e5e3296 100644 (file)
@@ -41,14 +41,13 @@ abstract class RdfMetaData {
                $rdftype = wfNegotiateType( wfAcceptToPrefs( $httpaccept ), wfAcceptToPrefs( self::RDF_TYPE_PREFS ) );
 
                if( !$rdftype ){
-                       wfHttpError( 406, 'Not Acceptable', wfMsg( 'notacceptable' ) );
-                       return false;
-               } else {
-                       $wgOut->disable();
-                       $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" );
-                       $wgOut->sendCacheControl();
-                       return true;
+                       throw new HttpError( 406, wfMessage( 'notacceptable' ) );
                }
+
+               $wgOut->disable();
+               $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" );
+               $wgOut->sendCacheControl();
+               return true;
        }
 
        protected function reallyFullUrl() {
index 98b5eb6..0c3a996 100644 (file)
@@ -874,7 +874,7 @@ class WebRequest {
                                        return false;
                                }
                        }
-                       wfHttpError( 403, 'Forbidden',
+                       throw new HttpError( 403,
                                'Invalid file extension found in the path info or query string.' );
 
                        return false;
index 985d951..a0d7751 100644 (file)
@@ -210,7 +210,7 @@ class MediaWiki {
                                                "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
                                                "to true.";
                                }
-                               wfHttpError( 500, "Internal error", $message );
+                               throw new HttpError( 500, $message );
                        } else {
                                $output->setSquidMaxage( 1200 );
                                $output->redirect( $targetUrl, '301' );
index d620385..e72c81e 100644 (file)
@@ -95,8 +95,7 @@ class SpecialUploadStash extends UnlistedSpecialPage {
                        $message = $e->getMessage();
                }
 
-               wfHttpError( $code, HttpStatus::getMessage( $code ), $message );
-               return false;
+               throw new HttpError( $code, $message );
        }
 
        /**
index 2e67eef..d747448 100644 (file)
@@ -33,31 +33,30 @@ class SpecialUserlogout extends UnlistedSpecialPage {
        }
 
        function execute( $par ) {
-               global $wgUser, $wgOut;
-
                /**
                 * Some satellite ISPs use broken precaching schemes that log people out straight after
                 * they're logged in (bug 17790). Luckily, there's a way to detect such requests.
                 */
                if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], '&amp;' ) !== false ) {
                        wfDebug( "Special:Userlogout request {$_SERVER['REQUEST_URI']} looks suspicious, denying.\n" );
-                       wfHttpError( 400, wfMsg( 'loginerror' ), wfMsg( 'suspicious-userlogout' ) );
-                       return;
+                       throw new HttpError( 400, wfMessage( 'suspicious-userlogout' ), wfMessage( 'loginerror' ) );
                }
 
                $this->setHeaders();
                $this->outputHeader();
 
-               $oldName = $wgUser->getName();
-               $wgUser->logout();
+               $user = $this->getUser();
+               $oldName = $user->getName();
+               $user->logout();
 
-               $wgOut->addWikiMsg( 'logouttext' );
+               $out = $this->getOutput();
+               $out->addWikiMsg( 'logouttext' );
 
                // Hook.
                $injected_html = '';
-               wfRunHooks( 'UserLogoutComplete', array( &$wgUser, &$injected_html, $oldName ) );
-               $wgOut->addHTML( $injected_html );
+               wfRunHooks( 'UserLogoutComplete', array( &$user, &$injected_html, $oldName ) );
+               $out->addHTML( $injected_html );
 
-               $wgOut->returnToMain();
+               $out->returnToMain();
        }
 }