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