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