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