MediaWiki REL1_33
MWException.php
Go to the documentation of this file.
1<?php
26class MWException extends Exception {
32 public function useOutputPage() {
33 return $this->useMessageCache() &&
34 !empty( $GLOBALS['wgFullyInitialised'] ) &&
35 !empty( $GLOBALS['wgOut'] ) &&
36 !defined( 'MEDIAWIKI_INSTALL' );
37 }
38
45 public function isLoggable() {
46 return true;
47 }
48
54 public function useMessageCache() {
55 global $wgLang;
56
57 foreach ( $this->getTrace() as $frame ) {
58 if ( isset( $frame['class'] ) && $frame['class'] === LocalisationCache::class ) {
59 return false;
60 }
61 }
62
63 return $wgLang instanceof Language;
64 }
65
75 public function msg( $key, $fallback /*[, params...] */ ) {
76 global $wgSitename;
78
79 // FIXME: Keep logic in sync with MWExceptionRenderer::msg.
80 $res = false;
81 if ( $this->useMessageCache() ) {
82 try {
83 $res = wfMessage( $key, $args )->text();
84 } catch ( Exception $e ) {
85 }
86 }
87 if ( $res === false ) {
89 // If an exception happens inside message rendering,
90 // {{SITENAME}} sometimes won't be replaced.
91 $res = strtr( $res, [
92 '{{SITENAME}}' => $wgSitename,
93 ] );
94 }
95 return $res;
96 }
97
105 public function getHTML() {
107
109 return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $this ) ) ) .
110 '</p><p>Backtrace:</p><p>' .
111 nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $this ) ) ) .
112 "</p>\n";
113 } else {
114 $logId = WebRequest::getRequestId();
115 $type = static::class;
116 return Html::errorBox(
118 '[' . $logId . '] ' .
119 gmdate( 'Y-m-d H:i:s' ) . ": " .
120 $this->msg( "internalerror-fatal-exception",
121 "Fatal exception of type $1",
122 $type,
123 $logId,
124 MWExceptionHandler::getURL()
125 )
126 ) ) .
127 "<!-- Set \$wgShowExceptionDetails = true; " .
128 "at the bottom of LocalSettings.php to show detailed " .
129 "debugging information. -->";
130 }
131 }
132
140 public function getText() {
142
144 return MWExceptionHandler::getLogMessage( $this ) .
145 "\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $this ) . "\n";
146 } else {
147 return "Set \$wgShowExceptionDetails = true; " .
148 "in LocalSettings.php to show detailed debugging information.\n";
149 }
150 }
151
157 public function getPageTitle() {
158 return $this->msg( 'internalerror', 'Internal error' );
159 }
160
164 public function reportHTML() {
165 global $wgOut, $wgSitename;
166 if ( $this->useOutputPage() ) {
167 $wgOut->prepareErrorPage( $this->getPageTitle() );
168 // Manually set the html title, since sometimes
169 // {{SITENAME}} does not get replaced for exceptions
170 // happening inside message rendering.
171 $wgOut->setHTMLTitle(
172 $this->msg(
173 'pagetitle',
174 "$1 - $wgSitename",
175 $this->getPageTitle()
176 )
177 );
178
179 $wgOut->addHTML( $this->getHTML() );
180
181 $wgOut->output();
182 } else {
183 self::header( 'Content-Type: text/html; charset=utf-8' );
184 echo "<!DOCTYPE html>\n" .
185 '<html><head>' .
186 // Mimick OutputPage::setPageTitle behaviour
187 '<title>' .
188 htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) .
189 '</title>' .
190 '<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
191 "</head><body>\n";
192
193 echo $this->getHTML();
194
195 echo "</body></html>\n";
196 }
197 }
198
203 public function report() {
204 global $wgMimeType;
205
206 if ( defined( 'MW_API' ) ) {
207 // Unhandled API exception, we can't be sure that format printer is alive
208 self::header( 'MediaWiki-API-Error: internal_api_error_' . static::class );
209 wfHttpError( 500, 'Internal Server Error', $this->getText() );
210 } elseif ( self::isCommandLine() ) {
211 $message = $this->getText();
212 $this->writeToCommandLine( $message );
213 } else {
214 self::statusHeader( 500 );
215 self::header( "Content-Type: $wgMimeType; charset=utf-8" );
216
217 $this->reportHTML();
218 }
219 }
220
227 private function writeToCommandLine( $message ) {
228 // T17602: STDERR may not be available
229 if ( !defined( 'MW_PHPUNIT_TEST' ) && defined( 'STDERR' ) ) {
230 fwrite( STDERR, $message );
231 } else {
232 echo $message;
233 }
234 }
235
242 public static function isCommandLine() {
243 return !empty( $GLOBALS['wgCommandLineMode'] );
244 }
245
251 private static function header( $header ) {
252 if ( !headers_sent() ) {
253 header( $header );
254 }
255 }
256 private static function statusHeader( $code ) {
257 if ( !headers_sent() ) {
259 }
260 }
261}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
$GLOBALS['IP']
$wgMimeType
The default Content-Type header.
$wgSitename
Name of the site.
$wgShowExceptionDetails
If set to true, uncaught exceptions will print the exception message and a complete stack trace to ou...
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
wfMsgReplaceArgs( $message, $args)
Replace message parameter keys on the given formatted output.
$fallback
$wgOut
Definition Setup.php:880
$wgLang
Definition Setup.php:875
if( $line===false) $args
Definition cdb.php:64
static header( $code)
Output an HTTP status code header.
Internationalisation code.
Definition Language.php:36
MediaWiki exception.
writeToCommandLine( $message)
Write a message to stderr falling back to stdout if stderr unavailable.
static statusHeader( $code)
isLoggable()
Whether to log this exception in the exception debug log.
useMessageCache()
Can the extension use the Message class/wfMessage to get i18n-ed messages?
static header( $header)
Send a header, if we haven't already sent them.
getHTML()
If $wgShowExceptionDetails is true, return a HTML message with a backtrace to the error,...
static isCommandLine()
Check whether we are in command line mode or not to report the exception in the correct format.
useOutputPage()
Should the exception use $wgOut to output the error?
report()
Output a report about the exception and takes care of formatting.
getPageTitle()
Return the title of the page when reporting this error in a HTTP response.
reportHTML()
Output the exception report using HTML.
getText()
Get the text to display when reporting the error on the command line.
msg( $key, $fallback)
Get a message from i18n.
$res
Definition database.txt:21
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition hooks.txt:856
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
returning false will NOT prevent logging $e
Definition hooks.txt:2175
$header