MediaWiki master
HttpError.php
Go to the documentation of this file.
1<?php
23
33class HttpError extends MWException {
35 private $httpCode;
37 private $header;
39 private $content;
40
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
65 public function isLoggable() {
66 return false;
67 }
68
74 public function getStatusCode() {
75 return $this->httpCode;
76 }
77
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
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}
getFile()
Get the file for this page, if one exists.
Show an error that looks like an HTTP server error.
Definition HttpError.php:33
report()
Report and log the HTTP error.
Definition HttpError.php:83
getStatusCode()
Returns the HTTP status code supplied to the constructor.
Definition HttpError.php:74
__construct( $httpCode, $content, $header=null)
Definition HttpError.php:49
isLoggable()
We don't want the default exception logging as we got our own logging set up in self::report.
Definition HttpError.php:65
getHTML()
Returns HTML for reporting the HTTP error.
MediaWiki exception.
Create PSR-3 logger objects.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:155
text()
Returns the message text.
Definition Message.php:1099
$header