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