| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Logger\LoggerFactory; |
| 22 |
use MediaWiki\Message\Message; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class HttpError extends MWException { |
| 34 |
|
| 35 |
private $httpCode; |
| 36 |
|
| 37 |
private $header; |
| 38 |
|
| 39 |
private $content; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function __construct( $httpCode, $content, $header = null ) { |
| 50 |
parent::__construct( (string)$content ); |
| 51 |
$this->httpCode = (int)$httpCode; |
| 52 |
$this->header = $header; |
| 53 |
$this->content = $content; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function isLoggable() { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function getStatusCode() { |
| 75 |
return $this->httpCode; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function report() { |
| 84 |
$this->doLog(); |
| 85 |
|
| 86 |
HttpStatus::header( $this->httpCode ); |
| 87 |
header( 'Content-type: text/html; charset=utf-8' ); |
| 88 |
|
| 89 |
print $this->getHTML(); |
| 90 |
} |
| 91 |
|
| 92 |
private function doLog() { |
| 93 |
$logger = LoggerFactory::getInstance( 'HttpError' ); |
| 94 |
$content = $this->content; |
| 95 |
|
| 96 |
if ( $content instanceof Message ) { |
| 97 |
$content = $content->text(); |
| 98 |
} |
| 99 |
|
| 100 |
$context = [ |
| 101 |
'file' => $this->getFile(), |
| 102 |
'line' => $this->getLine(), |
| 103 |
'http_code' => $this->httpCode, |
| 104 |
]; |
| 105 |
|
| 106 |
$logMsg = "$content ({http_code}) from {file}:{line}"; |
| 107 |
|
| 108 |
if ( $this->getStatusCode() < 500 ) { |
| 109 |
$logger->info( $logMsg, $context ); |
| 110 |
} else { |
| 111 |
$logger->error( $logMsg, $context ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function getHTML() { |
| 122 |
if ( $this->header === null ) { |
| 123 |
$titleHtml = htmlspecialchars( HttpStatus::getMessage( $this->httpCode ) ); |
| 124 |
} elseif ( $this->header instanceof Message ) { |
| 125 |
$titleHtml = $this->header->escaped(); |
| 126 |
} else { |
| 127 |
$titleHtml = htmlspecialchars( $this->header ); |
| 128 |
} |
| 129 |
|
| 130 |
if ( $this->content instanceof Message ) { |
| 131 |
$contentHtml = $this->content->escaped(); |
| 132 |
} else { |
| 133 |
$contentHtml = nl2br( htmlspecialchars( $this->content ) ); |
| 134 |
} |
| 135 |
|
| 136 |
return "<!DOCTYPE html>\n" . |
| 137 |
"<html><head><title>$titleHtml</title><meta name=\"color-scheme\" content=\"light dark\" /></head>\n" . |
| 138 |
"<body><h1>$titleHtml</h1><p>$contentHtml</p></body></html>\n"; |
| 139 |
} |
| 140 |
} |