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