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' );
93  $content = $this->content;
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 }
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:82
getStatusCode()
Returns the HTTP status code supplied to the constructor.
Definition: HttpError.php:73
__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.
Definition: HttpError.php:120
static header( $code)
Output an HTTP status code header.
Definition: HttpStatus.php:96
static getMessage( $code)
Get the message associated with an HTTP response status code.
Definition: HttpStatus.php:34
MediaWiki exception.
Definition: MWException.php:32
PSR-3 logger instance factory.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:144
$content
Definition: router.php:76