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