*stab stab stab*
[lhc/web/wiklou.git] / includes / Exception.php
1 <?php
2
3 class MWException extends Exception
4 {
5 function useOutputPage() {
6 return !empty( $GLOBALS['wgFullyInitialised'] ) &&
7 !empty( $GLOBALS['wgArticle'] ) && !empty( $GLOBALS['wgTitle'] );
8 }
9
10 function useMessageCache() {
11 global $wgLang;
12 return is_object( $wgLang );
13 }
14
15 function msg( $key, $fallback /*[, params...] */ ) {
16 $args = array_slice( func_get_args(), 2 );
17 if ( $this->useMessageCache() ) {
18 return wfMsgReal( $key, $args );
19 } else {
20 return wfMsgReplaceArgs( $fallback, $args );
21 }
22 }
23
24 function getHTML() {
25 global $wgShowExceptionDetails;
26 if( $wgShowExceptionDetails ) {
27 return '<p>' . htmlspecialchars( $this->getMessage() ) .
28 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
29 "</p>\n";
30 } else {
31 return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
32 "in LocalSettings.php to show detailed debugging information.</p>";
33 }
34 }
35
36 function getText() {
37 global $wgShowExceptionDetails;
38 if( $wgShowExceptionDetails ) {
39 return $this->getMessage() .
40 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
41 } else {
42 return "<p>Set <tt>\$wgShowExceptionDetails = true;</tt> " .
43 "in LocalSettings.php to show detailed debugging information.</p>";
44 }
45 }
46
47 function getPageTitle() {
48 if ( $this->useMessageCache() ) {
49 return wfMsg( 'internalerror' );
50 } else {
51 global $wgSitename;
52 return "$wgSitename error";
53 }
54 }
55
56 function getLogMessage() {
57 global $wgRequest;
58 $file = $this->getFile();
59 $line = $this->getLine();
60 $message = $this->getMessage();
61 return $wgRequest->getRequestURL() . " Exception from line $line of $file: $message";
62 }
63
64 function reportHTML() {
65 global $wgOut;
66 if ( $this->useOutputPage() ) {
67 $wgOut->setPageTitle( $this->getPageTitle() );
68 $wgOut->setRobotpolicy( "noindex,nofollow" );
69 $wgOut->setArticleRelated( false );
70 $wgOut->enableClientCache( false );
71 $wgOut->redirect( '' );
72 $wgOut->clearHTML();
73 $wgOut->addHTML( $this->getHTML() );
74 $wgOut->output();
75 } else {
76 echo $this->htmlHeader();
77 echo $this->getHTML();
78 echo $this->htmlFooter();
79 }
80 }
81
82 function reportText() {
83 echo $this->getText();
84 }
85
86 function report() {
87 global $wgCommandLineMode;
88 if ( $wgCommandLineMode ) {
89 $this->reportText();
90 } else {
91 $log = $this->getLogMessage();
92 if ( $log ) {
93 wfDebugLog( 'exception', $log );
94 }
95 $this->reportHTML();
96 }
97 }
98
99 function htmlHeader() {
100 global $wgLogo, $wgSitename, $wgOutputEncoding;
101
102 if ( !headers_sent() ) {
103 header( 'HTTP/1.0 500 Internal Server Error' );
104 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
105 /* Don't cache error pages! They cause no end of trouble... */
106 header( 'Cache-control: none' );
107 header( 'Pragma: nocache' );
108 }
109 $title = $this->getPageTitle();
110 echo "<html>
111 <head>
112 <title>$title</title>
113 </head>
114 <body>
115 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
116 ";
117 }
118
119 function htmlFooter() {
120 echo "</body></html>";
121 }
122
123 }
124
125 /**
126 * Exception class which takes an HTML error message, and does not
127 * produce a backtrace. Replacement for OutputPage::fatalError().
128 */
129 class FatalError extends MWException {
130 function getHTML() {
131 return $this->getMessage();
132 }
133
134 function getText() {
135 return $this->getMessage();
136 }
137 }
138
139 class ErrorPageError extends MWException {
140 public $title, $msg;
141
142 /**
143 * Note: these arguments are keys into wfMsg(), not text!
144 */
145 function __construct( $title, $msg ) {
146 $this->title = $title;
147 $this->msg = $msg;
148 parent::__construct( wfMsg( $msg ) );
149 }
150
151 function report() {
152 global $wgOut;
153 $wgOut->showErrorPage( $this->title, $this->msg );
154 $wgOut->output();
155 }
156 }
157
158 /**
159 * Install an exception handler for MediaWiki exception types.
160 */
161 function wfInstallExceptionHandler() {
162 set_exception_handler( 'wfExceptionHandler' );
163 }
164
165 /**
166 * Report an exception to the user
167 */
168 function wfReportException( Exception $e ) {
169 if ( $e instanceof MWException ) {
170 try {
171 $e->report();
172 } catch ( Exception $e2 ) {
173 // Exception occurred from within exception handler
174 // Show a simpler error message for the original exception,
175 // don't try to invoke report()
176 $message = "MediaWiki internal error.\n\n" .
177 "Original exception: " . $e->__toString() .
178 "\n\nException caught inside exception handler: " .
179 $e2->__toString() . "\n";
180
181 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
182 echo $message;
183 } else {
184 echo nl2br( htmlspecialchars( $message ) ). "\n";
185 }
186 }
187 } else {
188 echo $e->__toString();
189 }
190 }
191
192 /**
193 * Exception handler which simulates the appropriate catch() handling:
194 *
195 * try {
196 * ...
197 * } catch ( MWException $e ) {
198 * $e->report();
199 * } catch ( Exception $e ) {
200 * echo $e->__toString();
201 * }
202 */
203 function wfExceptionHandler( $e ) {
204 global $wgFullyInitialised;
205 wfReportException( $e );
206
207 // Final cleanup, similar to wfErrorExit()
208 if ( $wgFullyInitialised ) {
209 try {
210 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
211 } catch ( Exception $e ) {}
212 }
213
214 // Exit value should be nonzero for the benefit of shell jobs
215 exit( 1 );
216 }
217
218 ?>