Merge "Remove extra unneeded whitespace"
[lhc/web/wiklou.git] / includes / Exception.php
index 8c4e0a7..a1e3f12 100644 (file)
@@ -15,6 +15,8 @@
  * @ingroup Exception
  */
 class MWException extends Exception {
+       var $logId;
+
        /**
         * Should the exception use $wgOut to output the error ?
         * @return bool
@@ -53,11 +55,11 @@ class MWException extends Exception {
                global $wgExceptionHooks;
 
                if ( !isset( $wgExceptionHooks ) || !is_array( $wgExceptionHooks ) ) {
-                       return; // Just silently ignore
+                       return null; // Just silently ignore
                }
 
                if ( !array_key_exists( $name, $wgExceptionHooks ) || !is_array( $wgExceptionHooks[ $name ] ) ) {
-                       return;
+                       return null;
                }
 
                $hooks = $wgExceptionHooks[ $name ];
@@ -74,6 +76,7 @@ class MWException extends Exception {
                                return $result;
                        }
                }
+               return null;
        }
 
        /**
@@ -110,9 +113,14 @@ class MWException extends Exception {
                                '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
                                "</p>\n";
                } else {
-                       return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
+                       return 
+                               "<div class=\"errorbox\">" .
+                               '[' . $this->getLogId() . '] ' .
+                               gmdate( 'Y-m-d H:i:s' ) . 
+                               ": Fatal exception of type " . get_class( $this ) . "</div>\n" .
+                               "<!-- Set \$wgShowExceptionDetails = true; " .
                                "at the bottom of LocalSettings.php to show detailed " .
-                               "debugging information.</p>";
+                               "debugging information. -->";
                }
        }
 
@@ -141,6 +149,13 @@ class MWException extends Exception {
                return $this->msg( 'internalerror', "Internal error" );
        }
 
+       function getLogId() {
+               if ( $this->logId === null ) {
+                       $this->logId = wfRandomString( 8 );
+               }
+               return $this->logId;
+       }
+
        /**
         * Return the requested URL and point to file and line number from which the
         * exception occured
@@ -150,6 +165,7 @@ class MWException extends Exception {
        function getLogMessage() {
                global $wgRequest;
 
+               $id = $this->getLogId();
                $file = $this->getFile();
                $line = $this->getLine();
                $message = $this->getMessage();
@@ -163,7 +179,7 @@ class MWException extends Exception {
                        $url = '[no req]';
                }
 
-               return "$url   Exception from line $line of $file: $message";
+               return "[$id] $url   Exception from line $line of $file: $message";
        }
 
        /** Output the exception report using HTML */
@@ -181,6 +197,7 @@ class MWException extends Exception {
 
                        $wgOut->output();
                } else {
+                       header( "Content-Type: text/html; charset=utf-8" );
                        $hookResult = $this->runHooks( get_class( $this ) . "Raw" );
                        if ( $hookResult ) {
                                die( $hookResult );
@@ -196,13 +213,22 @@ class MWException extends Exception {
         * It will be either HTML or plain text based on isCommandLine().
         */
        function report() {
+               global $wgLogExceptionBacktrace;
                $log = $this->getLogMessage();
 
                if ( $log ) {
-                       wfDebugLog( 'exception', $log );
+                       if ( $wgLogExceptionBacktrace ) {
+                               wfDebugLog( 'exception', $log . "\n" . $this->getTraceAsString() . "\n" );
+                       } else {
+                               wfDebugLog( 'exception', $log );
+                       }
                }
 
-               if ( self::isCommandLine() ) {
+               if ( defined( 'MW_API' ) ) {
+                       // Unhandled API exception, we can't be sure that format printer is alive
+                       header( 'MediaWiki-API-Error: internal_api_error_' . get_class( $this ) );
+                       wfHttpError(500, 'Internal Server Error', $this->getText() );
+               } elseif ( self::isCommandLine() ) {
                        MWExceptionHandler::printError( $this->getText() );
                } else {
                        $this->reportHTML();
@@ -270,6 +296,36 @@ class ErrorPageError extends MWException {
        }
 }
 
+/**
+ * Show an error page on a badtitle.
+ * Similar to ErrorPage, but emit a 400 HTTP error code to let mobile
+ * browser it is not really a valid content.
+ */
+class BadTitleError extends ErrorPageError {
+
+       /**
+        * @param $msg string A message key (default: 'badtitletext')
+        * @param $params Array parameter to wfMsg()
+        */
+       function __construct( $msg = 'badtitletext', $params = null ) {
+               parent::__construct( 'badtitle', $msg, $params );
+       }
+
+       /**
+        * Just like ErrorPageError::report() but additionally set
+        * a 400 HTTP status code (bug 33646).
+        */
+       function report() {
+               global $wgOut;
+
+               // bug 33646: a badtitle error page need to return an error code
+               // to let mobile browser now that it is not a normal page.
+               $wgOut->setStatusCode( 400 );
+               parent::report();
+       }
+
+}
+
 /**
  * Show an error when a user tries to do something they do not have the necessary
  * permissions for.
@@ -337,7 +393,7 @@ class ThrottledError extends ErrorPageError {
        public function report(){
                global $wgOut;
                $wgOut->setStatusCode( 503 );
-               return parent::report();
+               parent::report();
        }
 }
 
@@ -406,7 +462,7 @@ class HttpError extends MWException {
                $this->content = $content;
        }
 
-       public function reportHTML() {
+       public function report() {
                $httpMessage = HttpStatus::getMessage( $this->httpCode );
 
                header( "Status: {$this->httpCode} {$httpMessage}" );
@@ -426,7 +482,7 @@ class HttpError extends MWException {
                        $content = htmlspecialchars( $this->content );
                }
 
-               print "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n".
+               print "<!DOCTYPE html>\n".
                        "<html><head><title>$header</title></head>\n" .
                        "<body><h1>$header</h1><p>$content</p></body></html>\n";
        }