MediaWiki  master
HttpError.php
Go to the documentation of this file.
1 <?php
22 
32 class 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 }
HttpError\$header
$header
Definition: HttpError.php:33
HttpError\report
report()
Report and log the HTTP error.
Definition: HttpError.php:75
HttpError
Show an error that looks like an HTTP server error.
Definition: HttpError.php:32
HttpError\$content
$content
Definition: HttpError.php:33
HttpError\getStatusCode
getStatusCode()
Returns the HTTP status code supplied to the constructor.
Definition: HttpError.php:66
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:57
MWException
MediaWiki exception.
Definition: MWException.php:29
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
HttpError\$httpCode
$httpCode
Definition: HttpError.php:33
HttpError\__construct
__construct( $httpCode, $content, $header=null)
Stable to call.
Definition: HttpError.php:41
HttpError\getHTML
getHTML()
Returns HTML for reporting the HTTP error.
Definition: HttpError.php:113
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:161
HttpError\doLog
doLog()
Definition: HttpError.php:84
getFile
getFile()
Get the file for this page, if one exists.
Definition: RevisionSearchResultTrait.php:89