* (bug 6627) Fix regression in Special:Ipblocklist with table prefix
[mediawiki.git] / includes / Exception.php
blob1e24515ba67dc4efe9cb73ffb8feaaeedb7ba7d8
1 <?php
3 class MWException extends Exception
5 function useOutputPage() {
6 return !empty( $GLOBALS['wgFullyInitialised'] );
9 function useMessageCache() {
10 global $wgLang;
11 return is_object( $wgLang );
14 function msg( $key, $fallback /*[, params...] */ ) {
15 $args = array_slice( func_get_args(), 2 );
16 if ( $this->useMessageCache() ) {
17 return wfMsgReal( $key, $args );
18 } else {
19 return wfMsgReplaceArgs( $fallback, $args );
23 function getHTML() {
24 return '<p>' . htmlspecialchars( $this->getMessage() ) .
25 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
26 "</p>\n";
29 function getText() {
30 return $this->getMessage() .
31 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
34 function getPageTitle() {
35 if ( $this->useMessageCache() ) {
36 return wfMsg( 'internalerror' );
37 } else {
38 global $wgSitename;
39 return "$wgSitename error";
43 function reportHTML() {
44 global $wgOut;
45 if ( $this->useOutputPage() ) {
46 $wgOut->setPageTitle( $this->getPageTitle() );
47 $wgOut->setRobotpolicy( "noindex,nofollow" );
48 $wgOut->setArticleRelated( false );
49 $wgOut->enableClientCache( false );
50 $wgOut->redirect( '' );
51 $wgOut->clearHTML();
52 $wgOut->addHTML( $this->getHTML() );
53 $wgOut->output();
54 } else {
55 echo $this->htmlHeader();
56 echo $this->getHTML();
57 echo $this->htmlFooter();
61 function reportText() {
62 echo $this->getText();
65 function report() {
66 global $wgCommandLineMode;
67 if ( $wgCommandLineMode ) {
68 $this->reportText();
69 } else {
70 $this->reportHTML();
74 function htmlHeader() {
75 global $wgLogo, $wgSitename, $wgOutputEncoding;
77 if ( !headers_sent() ) {
78 header( 'HTTP/1.0 500 Internal Server Error' );
79 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
80 /* Don't cache error pages! They cause no end of trouble... */
81 header( 'Cache-control: none' );
82 header( 'Pragma: nocache' );
84 $title = $this->getPageTitle();
85 echo "<html>
86 <head>
87 <title>$title</title>
88 </head>
89 <body>
90 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
94 function htmlFooter() {
95 echo "</body></html>";
99 /**
100 * Exception class which takes an HTML error message, and does not
101 * produce a backtrace. Replacement for OutputPage::fatalError().
103 class FatalError extends MWException {
104 function getHTML() {
105 return $this->getMessage();
108 function getText() {
109 return $this->getMessage();
113 class ErrorPageError extends MWException {
114 public $title, $msg;
117 * Note: these arguments are keys into wfMsg(), not text!
119 function __construct( $title, $msg ) {
120 $this->title = $title;
121 $this->msg = $msg;
122 parent::__construct( wfMsg( $msg ) );
125 function report() {
126 global $wgOut;
127 $wgOut->showErrorPage( $this->title, $this->msg );
128 $wgOut->output();
133 * Install an exception handler for MediaWiki exception types.
135 function wfInstallExceptionHandler() {
136 set_exception_handler( 'wfExceptionHandler' );
140 * Report an exception to the user
142 function wfReportException( Exception $e ) {
143 if ( is_a( $e, 'MWException' ) ) {
144 try {
145 $e->report();
146 } catch ( Exception $e2 ) {
147 // Exception occurred from within exception handler
148 // Show a simpler error message for the original exception,
149 // don't try to invoke report()
150 $message = "MediaWiki internal error.\n\n" .
151 "Original exception: " . $e->__toString() .
152 "\n\nException caught inside exception handler: " .
153 $e2->__toString() . "\n";
155 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
156 echo $message;
157 } else {
158 echo nl2br( htmlspecialchars( $message ) ). "\n";
161 } else {
162 echo $e->__toString();
167 * Exception handler which simulates the appropriate catch() handling:
169 * try {
170 * ...
171 * } catch ( MWException $e ) {
172 * $e->report();
173 * } catch ( Exception $e ) {
174 * echo $e->__toString();
177 function wfExceptionHandler( $e ) {
178 global $wgFullyInitialised;
179 wfReportException( $e );
181 // Final cleanup, similar to wfErrorExit()
182 if ( $wgFullyInitialised ) {
183 try {
184 wfProfileClose();
185 logProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
186 } catch ( Exception $e ) {}
189 // Exit value should be nonzero for the benefit of shell jobs
190 exit( 1 );