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