Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
50.00% |
17 / 34 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
HttpError | |
50.00% |
17 / 34 |
|
66.67% |
4 / 6 |
26.12 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
isLoggable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getStatusCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
report | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
doLog | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
getHTML | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 |
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. |
7 | * |
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. |
12 | * |
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 |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | use MediaWiki\Logger\LoggerFactory; |
22 | use MediaWiki\Message\Message; |
23 | |
24 | /** |
25 | * Show an error that looks like an HTTP server error. |
26 | * Replacement for wfHttpError(). |
27 | * |
28 | * @newable |
29 | * @stable to extend |
30 | * @since 1.19 |
31 | * @ingroup Exception |
32 | */ |
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; |
40 | |
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 |
48 | */ |
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; |
54 | } |
55 | |
56 | /** |
57 | * We don't want the default exception logging as we got our own logging set |
58 | * up in self::report. |
59 | * |
60 | * @see MWException::isLoggable |
61 | * |
62 | * @since 1.24 |
63 | * @return bool |
64 | */ |
65 | public function isLoggable() { |
66 | return false; |
67 | } |
68 | |
69 | /** |
70 | * Returns the HTTP status code supplied to the constructor. |
71 | * |
72 | * @return int |
73 | */ |
74 | public function getStatusCode() { |
75 | return $this->httpCode; |
76 | } |
77 | |
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. |
82 | */ |
83 | public function report() { |
84 | $this->doLog(); |
85 | |
86 | HttpStatus::header( $this->httpCode ); |
87 | header( 'Content-type: text/html; charset=utf-8' ); |
88 | |
89 | print $this->getHTML(); |
90 | } |
91 | |
92 | private function doLog() { |
93 | $logger = LoggerFactory::getInstance( 'HttpError' ); |
94 | $content = $this->content; |
95 | |
96 | if ( $content instanceof Message ) { |
97 | $content = $content->text(); |
98 | } |
99 | |
100 | $context = [ |
101 | 'file' => $this->getFile(), |
102 | 'line' => $this->getLine(), |
103 | 'http_code' => $this->httpCode, |
104 | ]; |
105 | |
106 | $logMsg = "$content ({http_code}) from {file}:{line}"; |
107 | |
108 | if ( $this->getStatusCode() < 500 ) { |
109 | $logger->info( $logMsg, $context ); |
110 | } else { |
111 | $logger->error( $logMsg, $context ); |
112 | } |
113 | } |
114 | |
115 | /** |
116 | * Returns HTML for reporting the HTTP error. |
117 | * This will be a minimal but complete HTML document. |
118 | * |
119 | * @return string HTML |
120 | */ |
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 ); |
128 | } |
129 | |
130 | if ( $this->content instanceof Message ) { |
131 | $contentHtml = $this->content->escaped(); |
132 | } else { |
133 | $contentHtml = nl2br( htmlspecialchars( $this->content ) ); |
134 | } |
135 | |
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"; |
139 | } |
140 | } |