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