*Tweak rev_deleted message
[mediawiki.git] / includes / Exception.php
blob06cadc0df2cebad6164f60e7f22871782bdf5f61
1 <?php
3 /**
4 * MediaWiki exception
5 * @addtogroup Exception
6 */
7 class MWException extends Exception
9 function useOutputPage() {
10 return !empty( $GLOBALS['wgFullyInitialised'] ) &&
11 !empty( $GLOBALS['wgArticle'] ) && !empty( $GLOBALS['wgTitle'] );
14 function useMessageCache() {
15 global $wgLang;
16 return is_object( $wgLang );
19 /** Get a message from i18n */
20 function msg( $key, $fallback /*[, params...] */ ) {
21 $args = array_slice( func_get_args(), 2 );
22 if ( $this->useMessageCache() ) {
23 return wfMsgReal( $key, $args );
24 } else {
25 return wfMsgReplaceArgs( $fallback, $args );
29 /* If wgShowExceptionDetails, return a HTML message with a backtrace to the error. */
30 function getHTML() {
31 global $wgShowExceptionDetails;
32 if( $wgShowExceptionDetails ) {
33 return '<p>' . htmlspecialchars( $this->getMessage() ) .
34 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
35 "</p>\n";
36 } else {
37 return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
38 "at the bottom of LocalSettings.php to show detailed " .
39 "debugging information.</p>";
43 /* If wgShowExceptionDetails, return a text message with a backtrace to the error */
44 function getText() {
45 global $wgShowExceptionDetails;
46 if( $wgShowExceptionDetails ) {
47 return $this->getMessage() .
48 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
49 } else {
50 return "<p>Set <tt>\$wgShowExceptionDetails = true;</tt> " .
51 "in LocalSettings.php to show detailed debugging information.</p>";
55 /* Return titles of this error page */
56 function getPageTitle() {
57 if ( $this->useMessageCache() ) {
58 return wfMsg( 'internalerror' );
59 } else {
60 global $wgSitename;
61 return "$wgSitename error";
65 /** Return the requested URL and point to file and line number from which the
66 * exception occured
68 function getLogMessage() {
69 global $wgRequest;
70 $file = $this->getFile();
71 $line = $this->getLine();
72 $message = $this->getMessage();
73 return $wgRequest->getRequestURL() . " Exception from line $line of $file: $message";
76 /** Output the exception report using HTML */
77 function reportHTML() {
78 global $wgOut;
79 if ( $this->useOutputPage() ) {
80 $wgOut->setPageTitle( $this->getPageTitle() );
81 $wgOut->setRobotpolicy( "noindex,nofollow" );
82 $wgOut->setArticleRelated( false );
83 $wgOut->enableClientCache( false );
84 $wgOut->redirect( '' );
85 $wgOut->clearHTML();
86 $wgOut->addHTML( $this->getHTML() );
87 $wgOut->output();
88 } else {
89 echo $this->htmlHeader();
90 echo $this->getHTML();
91 echo $this->htmlFooter();
95 /** Print the exception report using text */
96 function reportText() {
97 echo $this->getText();
100 /* Output a report about the exception and takes care of formatting.
101 * It will be either HTML or plain text based on $wgCommandLineMode.
103 function report() {
104 global $wgCommandLineMode;
105 if ( $wgCommandLineMode ) {
106 $this->reportText();
107 } else {
108 $log = $this->getLogMessage();
109 if ( $log ) {
110 wfDebugLog( 'exception', $log );
112 $this->reportHTML();
116 function htmlHeader() {
117 global $wgLogo, $wgSitename, $wgOutputEncoding;
119 if ( !headers_sent() ) {
120 header( 'HTTP/1.0 500 Internal Server Error' );
121 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
122 /* Don't cache error pages! They cause no end of trouble... */
123 header( 'Cache-control: none' );
124 header( 'Pragma: nocache' );
126 $title = $this->getPageTitle();
127 echo "<html>
128 <head>
129 <title>$title</title>
130 </head>
131 <body>
132 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
136 function htmlFooter() {
137 echo "</body></html>";
143 * Exception class which takes an HTML error message, and does not
144 * produce a backtrace. Replacement for OutputPage::fatalError().
145 * @addtogroup Exception
147 class FatalError extends MWException {
148 function getHTML() {
149 return $this->getMessage();
152 function getText() {
153 return $this->getMessage();
158 * @addtogroup Exception
160 class ErrorPageError extends MWException {
161 public $title, $msg;
164 * Note: these arguments are keys into wfMsg(), not text!
166 function __construct( $title, $msg ) {
167 $this->title = $title;
168 $this->msg = $msg;
169 parent::__construct( wfMsg( $msg ) );
172 function report() {
173 global $wgOut;
174 $wgOut->showErrorPage( $this->title, $this->msg );
175 $wgOut->output();
180 * Install an exception handler for MediaWiki exception types.
182 function wfInstallExceptionHandler() {
183 set_exception_handler( 'wfExceptionHandler' );
187 * Report an exception to the user
189 function wfReportException( Exception $e ) {
190 if ( $e instanceof MWException ) {
191 try {
192 $e->report();
193 } catch ( Exception $e2 ) {
194 // Exception occurred from within exception handler
195 // Show a simpler error message for the original exception,
196 // don't try to invoke report()
197 $message = "MediaWiki internal error.\n\n" .
198 "Original exception: " . $e->__toString() .
199 "\n\nException caught inside exception handler: " .
200 $e2->__toString() . "\n";
202 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
203 echo $message;
204 } else {
205 echo nl2br( htmlspecialchars( $message ) ). "\n";
208 } else {
209 echo $e->__toString();
214 * Exception handler which simulates the appropriate catch() handling:
216 * try {
217 * ...
218 * } catch ( MWException $e ) {
219 * $e->report();
220 * } catch ( Exception $e ) {
221 * echo $e->__toString();
224 function wfExceptionHandler( $e ) {
225 global $wgFullyInitialised;
226 wfReportException( $e );
228 // Final cleanup, similar to wfErrorExit()
229 if ( $wgFullyInitialised ) {
230 try {
231 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
232 } catch ( Exception $e ) {}
235 // Exit value should be nonzero for the benefit of shell jobs
236 exit( 1 );