Merge "Support {{#FORMAL:}} syntax in jqueryMsg"
[mediawiki.git] / includes / exception / HttpError.php
blob8f6dfd135f730d7fdb8d70d5cbade365bb3cde7b
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\Logger\LoggerFactory;
22 use MediaWiki\Message\Message;
24 /**
25 * Show an error that looks like an HTTP server error.
26 * Replacement for wfHttpError().
28 * @newable
29 * @stable to extend
30 * @since 1.19
31 * @ingroup Exception
33 class HttpError extends MWException {
34 /** @var int */
35 private $httpCode;
36 /** @var string|Message|null */
37 private $header;
38 /** @var string|Message */
39 private $content;
41 /**
42 * @stable to call
43 * @param int $httpCode HTTP status code to send to the client
44 * @param string|Message $content Content of the message
45 * @param string|Message|null $header Content of the header (\<title\> and \<h1\>)
46 * @param-taint $content tainted
47 * @param-taint $header tainted
49 public function __construct( $httpCode, $content, $header = null ) {
50 parent::__construct( $content );
51 $this->httpCode = (int)$httpCode;
52 $this->header = $header;
53 $this->content = $content;
56 /**
57 * We don't want the default exception logging as we got our own logging set
58 * up in self::report.
60 * @see MWException::isLoggable
62 * @since 1.24
63 * @return bool
65 public function isLoggable() {
66 return false;
69 /**
70 * Returns the HTTP status code supplied to the constructor.
72 * @return int
74 public function getStatusCode() {
75 return $this->httpCode;
78 /**
79 * Report and log the HTTP error.
80 * Sends the appropriate HTTP status code and outputs an
81 * HTML page with an error message.
83 public function report() {
84 $this->doLog();
86 HttpStatus::header( $this->httpCode );
87 header( 'Content-type: text/html; charset=utf-8' );
89 print $this->getHTML();
92 private function doLog() {
93 $logger = LoggerFactory::getInstance( 'HttpError' );
94 $content = $this->content;
96 if ( $content instanceof Message ) {
97 $content = $content->text();
100 $context = [
101 'file' => $this->getFile(),
102 'line' => $this->getLine(),
103 'http_code' => $this->httpCode,
106 $logMsg = "$content ({http_code}) from {file}:{line}";
108 if ( $this->getStatusCode() < 500 ) {
109 $logger->info( $logMsg, $context );
110 } else {
111 $logger->error( $logMsg, $context );
116 * Returns HTML for reporting the HTTP error.
117 * This will be a minimal but complete HTML document.
119 * @return string HTML
121 public function getHTML() {
122 if ( $this->header === null ) {
123 $titleHtml = htmlspecialchars( HttpStatus::getMessage( $this->httpCode ) );
124 } elseif ( $this->header instanceof Message ) {
125 $titleHtml = $this->header->escaped();
126 } else {
127 $titleHtml = htmlspecialchars( $this->header );
130 if ( $this->content instanceof Message ) {
131 $contentHtml = $this->content->escaped();
132 } else {
133 $contentHtml = nl2br( htmlspecialchars( $this->content ) );
136 return "<!DOCTYPE html>\n" .
137 "<html><head><title>$titleHtml</title><meta name=\"color-scheme\" content=\"light dark\" /></head>\n" .
138 "<body><h1>$titleHtml</h1><p>$contentHtml</p></body></html>\n";