MediaWiki REL1_37
HttpError.php
Go to the documentation of this file.
1<?php
22
32class HttpError extends MWException {
34 private $httpCode;
36 private $header;
38 private $content;
39
48 public function __construct( $httpCode, $content, $header = null ) {
49 parent::__construct( $content );
50 $this->httpCode = (int)$httpCode;
51 $this->header = $header;
52 $this->content = $content;
53 }
54
64 public function isLoggable() {
65 return false;
66 }
67
73 public function getStatusCode() {
74 return $this->httpCode;
75 }
76
82 public function report() {
83 $this->doLog();
84
85 HttpStatus::header( $this->httpCode );
86 header( 'Content-type: text/html; charset=utf-8' );
87
88 print $this->getHTML();
89 }
90
91 private function doLog() {
92 $logger = LoggerFactory::getInstance( 'HttpError' );
94
95 if ( $content instanceof Message ) {
97 }
98
99 $context = [
100 'file' => $this->getFile(),
101 'line' => $this->getLine(),
102 'http_code' => $this->httpCode,
103 ];
104
105 $logMsg = "$content ({http_code}) from {file}:{line}";
106
107 if ( $this->getStatusCode() < 500 ) {
108 $logger->info( $logMsg, $context );
109 } else {
110 $logger->error( $logMsg, $context );
111 }
112 }
113
120 public function getHTML() {
121 if ( $this->header === null ) {
122 $titleHtml = htmlspecialchars( HttpStatus::getMessage( $this->httpCode ) );
123 } elseif ( $this->header instanceof Message ) {
124 $titleHtml = $this->header->escaped();
125 } else {
126 $titleHtml = htmlspecialchars( $this->header );
127 }
128
129 if ( $this->content instanceof Message ) {
130 $contentHtml = $this->content->escaped();
131 } else {
132 $contentHtml = nl2br( htmlspecialchars( $this->content ) );
133 }
134
135 return "<!DOCTYPE html>\n" .
136 "<html><head><title>$titleHtml</title></head>\n" .
137 "<body><h1>$titleHtml</h1><p>$contentHtml</p></body></html>\n";
138 }
139}
getFile()
Get the file for this page, if one exists.
Show an error that looks like an HTTP server error.
Definition HttpError.php:32
string Message null $header
Definition HttpError.php:36
report()
Report and log the HTTP error.
Definition HttpError.php:82
string Message $content
Definition HttpError.php:38
getStatusCode()
Returns the HTTP status code supplied to the constructor.
Definition HttpError.php:73
int $httpCode
Definition HttpError.php:34
__construct( $httpCode, $content, $header=null)
Definition HttpError.php:48
isLoggable()
We don't want the default exception logging as we got our own logging set up in self::report.
Definition HttpError.php:64
getHTML()
Returns HTML for reporting the HTTP error.
MediaWiki exception.
static header( $header)
Send a header, if we haven't already sent them.
PSR-3 logger instance factory.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:138
text()
Returns the message text.
Definition Message.php:1008
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:69