MediaWiki  master
HttpError.php
Go to the documentation of this file.
1 <?php
22 
32 class 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 ) {
96  $content = $content->text();
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 }
HttpError\$content
string Message $content
Definition: HttpError.php:38
HttpError\report
report()
Report and log the HTTP error.
Definition: HttpError.php:82
Message\text
text()
Returns the message text.
Definition: Message.php:1006
HttpError
Show an error that looks like an HTTP server error.
Definition: HttpError.php:32
HttpError\getStatusCode
getStatusCode()
Returns the HTTP status code supplied to the constructor.
Definition: HttpError.php:73
HttpError\isLoggable
isLoggable()
We don't want the default exception logging as we got our own logging set up in self::report.
Definition: HttpError.php:64
MWException
MediaWiki exception.
Definition: MWException.php:29
HttpError\$header
string Message null $header
Definition: HttpError.php:36
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
HttpError\__construct
__construct( $httpCode, $content, $header=null)
Definition: HttpError.php:48
HttpError\getHTML
getHTML()
Returns HTML for reporting the HTTP error.
Definition: HttpError.php:120
MWException\header
static header( $header)
Send a header, if we haven't already sent them.
Definition: MWException.php:265
HttpStatus\getMessage
static getMessage( $code)
Get the message associated with an HTTP response status code.
Definition: HttpStatus.php:34
HttpStatus\header
static header( $code)
Output an HTTP status code header.
Definition: HttpStatus.php:96
Message
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:138
HttpError\doLog
doLog()
Definition: HttpError.php:91
HttpError\$httpCode
int $httpCode
Definition: HttpError.php:34
getFile
getFile()
Get the file for this page, if one exists.
Definition: RevisionSearchResultTrait.php:89