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