Bring up reply and new thread forms with AJAX where available. Still submits with...
[lhc/web/wiklou.git] / includes / Exception.php
1 <?php
2 /**
3 * @defgroup Exception Exception
4 */
5
6 /**
7 * MediaWiki exception
8 * @ingroup Exception
9 */
10 class MWException extends Exception {
11 /**
12 * Should the exception use $wgOut to output the error ?
13 * @return bool
14 */
15 function useOutputPage() {
16 return $this->useMessageCache() &&
17 !empty( $GLOBALS['wgFullyInitialised'] ) &&
18 ( !empty( $GLOBALS['wgArticle'] ) || ( !empty( $GLOBALS['wgOut'] ) && !$GLOBALS['wgOut']->isArticle() ) ) &&
19 !empty( $GLOBALS['wgTitle'] );
20 }
21
22 /**
23 * Can the extension use wfMsg() to get i18n messages ?
24 * @return bool
25 */
26 function useMessageCache() {
27 global $wgLang;
28 foreach ( $this->getTrace() as $frame ) {
29 if ( isset( $frame['class'] ) &&
30 $frame['class'] == 'LocalisationCache' ) {
31 return false;
32 }
33 }
34 return is_object( $wgLang );
35 }
36
37 /**
38 * Run hook to allow extensions to modify the text of the exception
39 *
40 * @param String $name class name of the exception
41 * @param Array $args arguments to pass to the callback functions
42 * @return mixed string to output or null if any hook has been called
43 */
44 function runHooks( $name, $args = array() ) {
45 global $wgExceptionHooks;
46 if( !isset( $wgExceptionHooks ) || !is_array( $wgExceptionHooks ) )
47 return; // Just silently ignore
48 if( !array_key_exists( $name, $wgExceptionHooks ) || !is_array( $wgExceptionHooks[ $name ] ) )
49 return;
50 $hooks = $wgExceptionHooks[ $name ];
51 $callargs = array_merge( array( $this ), $args );
52
53 foreach( $hooks as $hook ) {
54 if( is_string( $hook ) || ( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) ) ) { //'function' or array( 'class', hook' )
55 $result = call_user_func_array( $hook, $callargs );
56 } else {
57 $result = null;
58 }
59 if( is_string( $result ) )
60 return $result;
61 }
62 }
63
64 /**
65 * Get a message from i18n
66 *
67 * @param String $key message name
68 * @param String $fallback default message if the message cache can't be
69 * called by the exception
70 * The function also has other parameters that are arguments for the message
71 * @return String message with arguments replaced
72 */
73 function msg( $key, $fallback /*[, params...] */ ) {
74 $args = array_slice( func_get_args(), 2 );
75 if ( $this->useMessageCache() ) {
76 return wfMsgReal( $key, $args );
77 } else {
78 return wfMsgReplaceArgs( $fallback, $args );
79 }
80 }
81
82 /**
83 * If $wgShowExceptionDetails is true, return a HTML message with a
84 * backtrace to the error, otherwise show a message to ask to set it to true
85 * to show that information.
86 *
87 * @return String html to output
88 */
89 function getHTML() {
90 global $wgShowExceptionDetails;
91 if( $wgShowExceptionDetails ) {
92 return '<p>' . nl2br( htmlspecialchars( $this->getMessage() ) ) .
93 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
94 "</p>\n";
95 } else {
96 return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
97 "at the bottom of LocalSettings.php to show detailed " .
98 "debugging information.</p>";
99 }
100 }
101
102 /**
103 * If $wgShowExceptionDetails is true, return a text message with a
104 * backtrace to the error.
105 */
106 function getText() {
107 global $wgShowExceptionDetails;
108 if( $wgShowExceptionDetails ) {
109 return $this->getMessage() .
110 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
111 } else {
112 return "Set \$wgShowExceptionDetails = true; " .
113 "in LocalSettings.php to show detailed debugging information.\n";
114 }
115 }
116
117 /* Return titles of this error page */
118 function getPageTitle() {
119 if ( $this->useMessageCache() ) {
120 return wfMsg( 'internalerror' );
121 } else {
122 global $wgSitename;
123 return "$wgSitename error";
124 }
125 }
126
127 /**
128 * Return the requested URL and point to file and line number from which the
129 * exception occured
130 *
131 * @return string
132 */
133 function getLogMessage() {
134 global $wgRequest;
135 $file = $this->getFile();
136 $line = $this->getLine();
137 $message = $this->getMessage();
138 if ( isset( $wgRequest ) ) {
139 $url = $wgRequest->getRequestURL();
140 if ( !$url ) {
141 $url = '[no URL]';
142 }
143 } else {
144 $url = '[no req]';
145 }
146
147 return "$url Exception from line $line of $file: $message";
148 }
149
150 /** Output the exception report using HTML */
151 function reportHTML() {
152 global $wgOut;
153 if ( $this->useOutputPage() ) {
154 $wgOut->setPageTitle( $this->getPageTitle() );
155 $wgOut->setRobotPolicy( "noindex,nofollow" );
156 $wgOut->setArticleRelated( false );
157 $wgOut->enableClientCache( false );
158 $wgOut->redirect( '' );
159 $wgOut->clearHTML();
160 if( $hookResult = $this->runHooks( get_class( $this ) ) ) {
161 $wgOut->addHTML( $hookResult );
162 } else {
163 $wgOut->addHTML( $this->getHTML() );
164 }
165 $wgOut->output();
166 } else {
167 if( $hookResult = $this->runHooks( get_class( $this ) . "Raw" ) ) {
168 die( $hookResult );
169 }
170 if ( defined( 'MEDIAWIKI_INSTALL' ) || $this->htmlBodyOnly() ) {
171 echo $this->getHTML();
172 } else {
173 echo $this->htmlHeader();
174 echo $this->getHTML();
175 echo $this->htmlFooter();
176 }
177 }
178 }
179
180 /**
181 * Output a report about the exception and takes care of formatting.
182 * It will be either HTML or plain text based on isCommandLine().
183 */
184 function report() {
185 $log = $this->getLogMessage();
186 if ( $log ) {
187 wfDebugLog( 'exception', $log );
188 }
189 if ( self::isCommandLine() ) {
190 wfPrintError( $this->getText() );
191 } else {
192 $this->reportHTML();
193 }
194 }
195
196 /**
197 * Send headers and output the beginning of the html page if not using
198 * $wgOut to output the exception.
199 */
200 function htmlHeader() {
201 global $wgLogo, $wgSitename, $wgOutputEncoding;
202
203 if ( !headers_sent() ) {
204 header( 'HTTP/1.0 500 Internal Server Error' );
205 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
206 /* Don't cache error pages! They cause no end of trouble... */
207 header( 'Cache-control: none' );
208 header( 'Pragma: nocache' );
209 }
210 $title = $this->getPageTitle();
211 return "<html>
212 <head>
213 <title>$title</title>
214 </head>
215 <body>
216 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''/>$title</h1>
217 ";
218 }
219
220 /**
221 * print the end of the html page if not using $wgOut.
222 */
223 function htmlFooter() {
224 return "</body></html>";
225 }
226
227 /**
228 * headers handled by subclass?
229 */
230 function htmlBodyOnly() {
231 return false;
232 }
233
234 static function isCommandLine() {
235 return !empty( $GLOBALS['wgCommandLineMode'] ) && !defined( 'MEDIAWIKI_INSTALL' );
236 }
237 }
238
239 /**
240 * Exception class which takes an HTML error message, and does not
241 * produce a backtrace. Replacement for OutputPage::fatalError().
242 * @ingroup Exception
243 */
244 class FatalError extends MWException {
245 function getHTML() {
246 return $this->getMessage();
247 }
248
249 function getText() {
250 return $this->getMessage();
251 }
252 }
253
254 /**
255 * @ingroup Exception
256 */
257 class ErrorPageError extends MWException {
258 public $title, $msg;
259
260 /**
261 * Note: these arguments are keys into wfMsg(), not text!
262 */
263 function __construct( $title, $msg ) {
264 $this->title = $title;
265 $this->msg = $msg;
266 parent::__construct( wfMsg( $msg ) );
267 }
268
269 function report() {
270 global $wgOut;
271 $wgOut->showErrorPage( $this->title, $this->msg );
272 $wgOut->output();
273 }
274 }
275
276 /**
277 * Install an exception handler for MediaWiki exception types.
278 */
279 function wfInstallExceptionHandler() {
280 set_exception_handler( 'wfExceptionHandler' );
281 }
282
283 /**
284 * Report an exception to the user
285 */
286 function wfReportException( Exception $e ) {
287 $cmdLine = MWException::isCommandLine();
288 if ( $e instanceof MWException ) {
289 try {
290 $e->report();
291 } catch ( Exception $e2 ) {
292 // Exception occurred from within exception handler
293 // Show a simpler error message for the original exception,
294 // don't try to invoke report()
295 $message = "MediaWiki internal error.\n\n";
296 if ( $GLOBALS['wgShowExceptionDetails'] )
297 $message .= "Original exception: " . $e->__toString();
298 $message .= "\n\nException caught inside exception handler";
299 if ( $GLOBALS['wgShowExceptionDetails'] )
300 $message .= ": " . $e2->__toString();
301 $message .= "\n";
302 if ( $cmdLine ) {
303 wfPrintError( $message );
304 } else {
305 echo nl2br( htmlspecialchars( $message ) ). "\n";
306 }
307 }
308 } else {
309 $message = "Unexpected non-MediaWiki exception encountered, of type \"" . get_class( $e ) . "\"\n" .
310 $e->__toString() . "\n";
311 if ( $GLOBALS['wgShowExceptionDetails'] ) {
312 $message .= "\n" . $e->getTraceAsString() ."\n";
313 }
314 if ( $cmdLine ) {
315 wfPrintError( $message );
316 } else {
317 echo nl2br( htmlspecialchars( $message ) ). "\n";
318 }
319 }
320 }
321
322 /**
323 * Print a message, if possible to STDERR.
324 * Use this in command line mode only (see isCommandLine)
325 */
326 function wfPrintError( $message ) {
327 #NOTE: STDERR may not be available, especially if php-cgi is used from the command line (bug #15602).
328 # Try to produce meaningful output anyway. Using echo may corrupt output to STDOUT though.
329 if ( defined( 'STDERR' ) ) {
330 fwrite( STDERR, $message );
331 } else {
332 echo( $message );
333 }
334 }
335
336 /**
337 * Exception handler which simulates the appropriate catch() handling:
338 *
339 * try {
340 * ...
341 * } catch ( MWException $e ) {
342 * $e->report();
343 * } catch ( Exception $e ) {
344 * echo $e->__toString();
345 * }
346 */
347 function wfExceptionHandler( $e ) {
348 global $wgFullyInitialised;
349 wfReportException( $e );
350
351 // Final cleanup, similar to wfErrorExit()
352 if ( $wgFullyInitialised ) {
353 try {
354 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
355 } catch ( Exception $e ) {}
356 }
357
358 // Exit value should be nonzero for the benefit of shell jobs
359 exit( 1 );
360 }