* PageHistory::diffButtons
[lhc/web/wiklou.git] / includes / Exception.php
1 <?php
2
3 /**
4 * MediaWiki exception
5 * @addtogroup Exception
6 */
7 class MWException extends Exception
8 {
9 function useOutputPage() {
10 return !empty( $GLOBALS['wgFullyInitialised'] ) &&
11 !empty( $GLOBALS['wgArticle'] ) && !empty( $GLOBALS['wgTitle'] );
12 }
13
14 function useMessageCache() {
15 global $wgLang;
16 return is_object( $wgLang );
17 }
18
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 );
26 }
27 }
28
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>";
40 }
41 }
42
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>";
52 }
53 }
54
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";
62 }
63 }
64
65 /** Return the requested URL and point to file and line number from which the
66 * exception occured
67 */
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";
74 }
75
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();
92 }
93 }
94
95 /** Print the exception report using text */
96 function reportText() {
97 echo $this->getText();
98 }
99
100 /* Output a report about the exception and takes care of formatting.
101 * It will be either HTML or plain text based on $wgCommandLineMode.
102 */
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 );
111 }
112 $this->reportHTML();
113 }
114 }
115
116 function htmlHeader() {
117 global $wgLogo, $wgSitename, $wgOutputEncoding;
118
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' );
125 }
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>
133 ";
134 }
135
136 function htmlFooter() {
137 echo "</body></html>";
138 }
139
140 }
141
142 /**
143 * Exception class which takes an HTML error message, and does not
144 * produce a backtrace. Replacement for OutputPage::fatalError().
145 * @addtogroup Exception
146 */
147 class FatalError extends MWException {
148 function getHTML() {
149 return $this->getMessage();
150 }
151
152 function getText() {
153 return $this->getMessage();
154 }
155 }
156
157 /**
158 * @addtogroup Exception
159 */
160 class ErrorPageError extends MWException {
161 public $title, $msg;
162
163 /**
164 * Note: these arguments are keys into wfMsg(), not text!
165 */
166 function __construct( $title, $msg ) {
167 $this->title = $title;
168 $this->msg = $msg;
169 parent::__construct( wfMsg( $msg ) );
170 }
171
172 function report() {
173 global $wgOut;
174 $wgOut->showErrorPage( $this->title, $this->msg );
175 $wgOut->output();
176 }
177 }
178
179 /**
180 * Install an exception handler for MediaWiki exception types.
181 */
182 function wfInstallExceptionHandler() {
183 set_exception_handler( 'wfExceptionHandler' );
184 }
185
186 /**
187 * Report an exception to the user
188 */
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";
201
202 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
203 echo $message;
204 } else {
205 echo nl2br( htmlspecialchars( $message ) ). "\n";
206 }
207 }
208 } else {
209 echo $e->__toString();
210 }
211 }
212
213 /**
214 * Exception handler which simulates the appropriate catch() handling:
215 *
216 * try {
217 * ...
218 * } catch ( MWException $e ) {
219 * $e->report();
220 * } catch ( Exception $e ) {
221 * echo $e->__toString();
222 * }
223 */
224 function wfExceptionHandler( $e ) {
225 global $wgFullyInitialised;
226 wfReportException( $e );
227
228 // Final cleanup, similar to wfErrorExit()
229 if ( $wgFullyInitialised ) {
230 try {
231 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
232 } catch ( Exception $e ) {}
233 }
234
235 // Exit value should be nonzero for the benefit of shell jobs
236 exit( 1 );
237 }
238
239