Merge "resourceloader: Disable a flaky MessageBlobStoreTest case" into REL1_31
[lhc/web/wiklou.git] / includes / exception / HttpError.php
index 0515566..f464d8a 100644 (file)
@@ -18,6 +18,8 @@
  * @file
  */
 
+use MediaWiki\Logger\LoggerFactory;
+
 /**
  * Show an error that looks like an HTTP server error.
  * Replacement for wfHttpError().
@@ -29,11 +31,9 @@ class HttpError extends MWException {
        private $httpCode, $header, $content;
 
        /**
-        * Constructor
-        *
         * @param int $httpCode HTTP status code to send to the client
         * @param string|Message $content Content of the message
-        * @param string|Message $header Content of the header (\<title\> and \<h1\>)
+        * @param string|Message|null $header Content of the header (\<title\> and \<h1\>)
         */
        public function __construct( $httpCode, $content, $header = null ) {
                parent::__construct( $content );
@@ -72,27 +72,25 @@ class HttpError extends MWException {
        public function report() {
                $this->doLog();
 
-               $httpMessage = HttpStatus::getMessage( $this->httpCode );
-
-               header( "Status: {$this->httpCode} {$httpMessage}", true, $this->httpCode );
+               HttpStatus::header( $this->httpCode );
                header( 'Content-type: text/html; charset=utf-8' );
 
                print $this->getHTML();
        }
 
        private function doLog() {
-               $logger = MWLoggerFactory::getInstance( 'HttpError' );
+               $logger = LoggerFactory::getInstance( 'HttpError' );
                $content = $this->content;
 
                if ( $content instanceof Message ) {
                        $content = $content->text();
                }
 
-               $context = array(
+               $context = [
                        'file' => $this->getFile(),
                        'line' => $this->getLine(),
                        'http_code' => $this->httpCode,
-               );
+               ];
 
                $logMsg = "$content ({http_code}) from {file}:{line}";
 
@@ -111,21 +109,21 @@ class HttpError extends MWException {
         */
        public function getHTML() {
                if ( $this->header === null ) {
-                       $header = HttpStatus::getMessage( $this->httpCode );
+                       $titleHtml = htmlspecialchars( HttpStatus::getMessage( $this->httpCode ) );
                } elseif ( $this->header instanceof Message ) {
-                       $header = $this->header->escaped();
+                       $titleHtml = $this->header->escaped();
                } else {
-                       $header = htmlspecialchars( $this->header );
+                       $titleHtml = htmlspecialchars( $this->header );
                }
 
                if ( $this->content instanceof Message ) {
-                       $content = $this->content->escaped();
+                       $contentHtml = $this->content->escaped();
                } else {
-                       $content = htmlspecialchars( $this->content );
+                       $contentHtml = nl2br( htmlspecialchars( $this->content ) );
                }
 
                return "<!DOCTYPE html>\n" .
-               "<html><head><title>$header</title></head>\n" .
-               "<body><h1>$header</h1><p>$content</p></body></html>\n";
+               "<html><head><title>$titleHtml</title></head>\n" .
+               "<body><h1>$titleHtml</h1><p>$contentHtml</p></body></html>\n";
        }
 }