Prevent the following strict-standards warnings - i.e. when running with error_loggin...
[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 $file = $this->getFile();
58 $line = $this->getLine();
59 $message = $this->getMessage();
60 return "{$_SERVER['REQUEST_URI']} Exception from line $line of $file: $message";
61 }
62
63 function reportHTML() {
64 global $wgOut;
65 if ( $this->useOutputPage() ) {
66 $wgOut->setPageTitle( $this->getPageTitle() );
67 $wgOut->setRobotpolicy( "noindex,nofollow" );
68 $wgOut->setArticleRelated( false );
69 $wgOut->enableClientCache( false );
70 $wgOut->redirect( '' );
71 $wgOut->clearHTML();
72 $wgOut->addHTML( $this->getHTML() );
73 $wgOut->output();
74 } else {
75 echo $this->htmlHeader();
76 echo $this->getHTML();
77 echo $this->htmlFooter();
78 }
79 }
80
81 function reportText() {
82 echo $this->getText();
83 }
84
85 function report() {
86 global $wgCommandLineMode;
87 if ( $wgCommandLineMode ) {
88 $this->reportText();
89 } else {
90 $log = $this->getLogMessage();
91 if ( $log ) {
92 wfDebugLog( 'exception', $log );
93 }
94 $this->reportHTML();
95 }
96 }
97
98 function htmlHeader() {
99 global $wgLogo, $wgSitename, $wgOutputEncoding;
100
101 if ( !headers_sent() ) {
102 header( 'HTTP/1.0 500 Internal Server Error' );
103 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
104 /* Don't cache error pages! They cause no end of trouble... */
105 header( 'Cache-control: none' );
106 header( 'Pragma: nocache' );
107 }
108 $title = $this->getPageTitle();
109 echo "<html>
110 <head>
111 <title>$title</title>
112 </head>
113 <body>
114 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
115 ";
116 }
117
118 function htmlFooter() {
119 echo "</body></html>";
120 }
121
122 }
123
124 /**
125 * Exception class which takes an HTML error message, and does not
126 * produce a backtrace. Replacement for OutputPage::fatalError().
127 */
128 class FatalError extends MWException {
129 function getHTML() {
130 return $this->getMessage();
131 }
132
133 function getText() {
134 return $this->getMessage();
135 }
136 }
137
138 class ErrorPageError extends MWException {
139 public $title, $msg;
140
141 /**
142 * Note: these arguments are keys into wfMsg(), not text!
143 */
144 function __construct( $title, $msg ) {
145 $this->title = $title;
146 $this->msg = $msg;
147 parent::__construct( wfMsg( $msg ) );
148 }
149
150 function report() {
151 global $wgOut;
152 $wgOut->showErrorPage( $this->title, $this->msg );
153 $wgOut->output();
154 }
155 }
156
157 /**
158 * Install an exception handler for MediaWiki exception types.
159 */
160 function wfInstallExceptionHandler() {
161 set_exception_handler( 'wfExceptionHandler' );
162 }
163
164 /**
165 * Report an exception to the user
166 */
167 function wfReportException( Exception $e ) {
168 if ( $e instanceof MWException ) {
169 try {
170 $e->report();
171 } catch ( Exception $e2 ) {
172 // Exception occurred from within exception handler
173 // Show a simpler error message for the original exception,
174 // don't try to invoke report()
175 $message = "MediaWiki internal error.\n\n" .
176 "Original exception: " . $e->__toString() .
177 "\n\nException caught inside exception handler: " .
178 $e2->__toString() . "\n";
179
180 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
181 echo $message;
182 } else {
183 echo nl2br( htmlspecialchars( $message ) ). "\n";
184 }
185 }
186 } else {
187 echo $e->__toString();
188 }
189 }
190
191 /**
192 * Exception handler which simulates the appropriate catch() handling:
193 *
194 * try {
195 * ...
196 * } catch ( MWException $e ) {
197 * $e->report();
198 * } catch ( Exception $e ) {
199 * echo $e->__toString();
200 * }
201 */
202 function wfExceptionHandler( $e ) {
203 global $wgFullyInitialised;
204 wfReportException( $e );
205
206 // Final cleanup, similar to wfErrorExit()
207 if ( $wgFullyInitialised ) {
208 try {
209 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
210 } catch ( Exception $e ) {}
211 }
212
213 // Exit value should be nonzero for the benefit of shell jobs
214 exit( 1 );
215 }
216
217 ?>