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