MediaWiki REL1_35
HttpError.php
Go to the documentation of this file.
1<?php
22
32class HttpError extends MWException {
34
41 public function __construct( $httpCode, $content, $header = null ) {
42 parent::__construct( $content );
43 $this->httpCode = (int)$httpCode;
44 $this->header = $header;
45 $this->content = $content;
46 }
47
57 public function isLoggable() {
58 return false;
59 }
60
66 public function getStatusCode() {
67 return $this->httpCode;
68 }
69
75 public function report() {
76 $this->doLog();
77
78 HttpStatus::header( $this->httpCode );
79 header( 'Content-type: text/html; charset=utf-8' );
80
81 print $this->getHTML();
82 }
83
84 private function doLog() {
85 $logger = LoggerFactory::getInstance( 'HttpError' );
87
88 if ( $content instanceof Message ) {
89 $content = $content->text();
90 }
91
92 $context = [
93 'file' => $this->getFile(),
94 'line' => $this->getLine(),
95 'http_code' => $this->httpCode,
96 ];
97
98 $logMsg = "$content ({http_code}) from {file}:{line}";
99
100 if ( $this->getStatusCode() < 500 ) {
101 $logger->info( $logMsg, $context );
102 } else {
103 $logger->error( $logMsg, $context );
104 }
105 }
106
113 public function getHTML() {
114 if ( $this->header === null ) {
115 $titleHtml = htmlspecialchars( HttpStatus::getMessage( $this->httpCode ) );
116 } elseif ( $this->header instanceof Message ) {
117 $titleHtml = $this->header->escaped();
118 } else {
119 $titleHtml = htmlspecialchars( $this->header );
120 }
121
122 if ( $this->content instanceof Message ) {
123 $contentHtml = $this->content->escaped();
124 } else {
125 $contentHtml = nl2br( htmlspecialchars( $this->content ) );
126 }
127
128 return "<!DOCTYPE html>\n" .
129 "<html><head><title>$titleHtml</title></head>\n" .
130 "<body><h1>$titleHtml</h1><p>$contentHtml</p></body></html>\n";
131 }
132}
getFile()
Get the file for this page, if one exists.
Show an error that looks like an HTTP server error.
Definition HttpError.php:32
report()
Report and log the HTTP error.
Definition HttpError.php:75
getStatusCode()
Returns the HTTP status code supplied to the constructor.
Definition HttpError.php:66
__construct( $httpCode, $content, $header=null)
Stable to call.
Definition HttpError.php:41
isLoggable()
We don't want the default exception logging as we got our own logging set up in self::report.
Definition HttpError.php:57
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:161
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:64