* (bug 8437) Make Title::loadRestrictions() initialise $mRestrictions properly
[mediawiki.git] / includes / Exception.php
blobac9c8a215134c4d33dcbfd48f0f5d9c43cacd85d
1 <?php
3 class MWException extends Exception
5 function useOutputPage() {
6 return !empty( $GLOBALS['wgFullyInitialised'] ) &&
7 !empty( $GLOBALS['wgArticle'] ) && !empty( $GLOBALS['wgTitle'] );
10 function useMessageCache() {
11 global $wgLang;
12 return is_object( $wgLang );
15 function msg( $key, $fallback /*[, params...] */ ) {
16 $args = array_slice( func_get_args(), 2 );
17 if ( $this->useMessageCache() ) {
18 return wfMsgReal( $key, $args );
19 } else {
20 return wfMsgReplaceArgs( $fallback, $args );
24 function getHTML() {
25 global $wgShowExceptionDetails;
26 if( $wgShowExceptionDetails ) {
27 return '<p>' . htmlspecialchars( $this->getMessage() ) .
28 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
29 "</p>\n";
30 } else {
31 return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
32 "in LocalSettings.php to show detailed debugging information.</p>";
36 function getText() {
37 global $wgShowExceptionDetails;
38 if( $wgShowExceptionDetails ) {
39 return $this->getMessage() .
40 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
41 } else {
42 return "<p>Set <tt>\$wgShowExceptionDetails = true;</tt> " .
43 "in LocalSettings.php to show detailed debugging information.</p>";
47 function getPageTitle() {
48 if ( $this->useMessageCache() ) {
49 return wfMsg( 'internalerror' );
50 } else {
51 global $wgSitename;
52 return "$wgSitename error";
56 function getLogMessage() {
57 $file = $this->getFile();
58 $line = $this->getLine();
59 $message = $this->getMessage();
60 return "{$_SERVER['REQUEST_URI']} Exception from line $line of $file: $message";
63 function reportHTML() {
64 global $wgOut;
65 if ( $this->useOutputPage() ) {
66 $wgOut->setPageTitle( $this->getPageTitle() );
67 $wgOut->setRobotpolicy( "noindex,nofollow" );
68 $wgOut->setArticleRelated( false );
69 $wgOut->enableClientCache( false );
70 $wgOut->redirect( '' );
71 $wgOut->clearHTML();
72 $wgOut->addHTML( $this->getHTML() );
73 $wgOut->output();
74 } else {
75 echo $this->htmlHeader();
76 echo $this->getHTML();
77 echo $this->htmlFooter();
81 function reportText() {
82 echo $this->getText();
85 function report() {
86 global $wgCommandLineMode;
87 if ( $wgCommandLineMode ) {
88 $this->reportText();
89 } else {
90 $log = $this->getLogMessage();
91 if ( $log ) {
92 wfDebugLog( 'exception', $log );
94 $this->reportHTML();
98 function htmlHeader() {
99 global $wgLogo, $wgSitename, $wgOutputEncoding;
101 if ( !headers_sent() ) {
102 header( 'HTTP/1.0 500 Internal Server Error' );
103 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
104 /* Don't cache error pages! They cause no end of trouble... */
105 header( 'Cache-control: none' );
106 header( 'Pragma: nocache' );
108 $title = $this->getPageTitle();
109 echo "<html>
110 <head>
111 <title>$title</title>
112 </head>
113 <body>
114 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
118 function htmlFooter() {
119 echo "</body></html>";
125 * Exception class which takes an HTML error message, and does not
126 * produce a backtrace. Replacement for OutputPage::fatalError().
128 class FatalError extends MWException {
129 function getHTML() {
130 return $this->getMessage();
133 function getText() {
134 return $this->getMessage();
138 class ErrorPageError extends MWException {
139 public $title, $msg;
142 * Note: these arguments are keys into wfMsg(), not text!
144 function __construct( $title, $msg ) {
145 $this->title = $title;
146 $this->msg = $msg;
147 parent::__construct( wfMsg( $msg ) );
150 function report() {
151 global $wgOut;
152 $wgOut->showErrorPage( $this->title, $this->msg );
153 $wgOut->output();
158 * Install an exception handler for MediaWiki exception types.
160 function wfInstallExceptionHandler() {
161 set_exception_handler( 'wfExceptionHandler' );
165 * Report an exception to the user
167 function wfReportException( Exception $e ) {
168 if ( $e instanceof MWException ) {
169 try {
170 $e->report();
171 } catch ( Exception $e2 ) {
172 // Exception occurred from within exception handler
173 // Show a simpler error message for the original exception,
174 // don't try to invoke report()
175 $message = "MediaWiki internal error.\n\n" .
176 "Original exception: " . $e->__toString() .
177 "\n\nException caught inside exception handler: " .
178 $e2->__toString() . "\n";
180 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
181 echo $message;
182 } else {
183 echo nl2br( htmlspecialchars( $message ) ). "\n";
186 } else {
187 echo $e->__toString();
192 * Exception handler which simulates the appropriate catch() handling:
194 * try {
195 * ...
196 * } catch ( MWException $e ) {
197 * $e->report();
198 * } catch ( Exception $e ) {
199 * echo $e->__toString();
202 function wfExceptionHandler( $e ) {
203 global $wgFullyInitialised;
204 wfReportException( $e );
206 // Final cleanup, similar to wfErrorExit()
207 if ( $wgFullyInitialised ) {
208 try {
209 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
210 } catch ( Exception $e ) {}
213 // Exit value should be nonzero for the benefit of shell jobs
214 exit( 1 );