Add sslCAFile option to DatabaseMysqli
[mediawiki.git] / includes / exception / MWException.php
blob8c1f8dc9683e4ce6f5ca96ff3e5d8693aaab4876
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 /**
22 * MediaWiki exception
24 * @ingroup Exception
26 class MWException extends Exception {
27 /**
28 * Should the exception use $wgOut to output the error?
30 * @return bool
32 public function useOutputPage() {
33 return $this->useMessageCache() &&
34 !empty( $GLOBALS['wgFullyInitialised'] ) &&
35 !empty( $GLOBALS['wgOut'] ) &&
36 !defined( 'MEDIAWIKI_INSTALL' );
39 /**
40 * Whether to log this exception in the exception debug log.
42 * @since 1.23
43 * @return bool
45 public function isLoggable() {
46 return true;
49 /**
50 * Can the extension use the Message class/wfMessage to get i18n-ed messages?
52 * @return bool
54 public function useMessageCache() {
55 global $wgLang;
57 foreach ( $this->getTrace() as $frame ) {
58 if ( isset( $frame['class'] ) && $frame['class'] === 'LocalisationCache' ) {
59 return false;
63 return $wgLang instanceof Language;
66 /**
67 * Get a message from i18n
69 * @param string $key Message name
70 * @param string $fallback Default message if the message cache can't be
71 * called by the exception
72 * The function also has other parameters that are arguments for the message
73 * @return string Message with arguments replaced
75 public function msg( $key, $fallback /*[, params...] */ ) {
76 $args = array_slice( func_get_args(), 2 );
78 if ( $this->useMessageCache() ) {
79 try {
80 return wfMessage( $key, $args )->text();
81 } catch ( Exception $e ) {
84 return wfMsgReplaceArgs( $fallback, $args );
87 /**
88 * If $wgShowExceptionDetails is true, return a HTML message with a
89 * backtrace to the error, otherwise show a message to ask to set it to true
90 * to show that information.
92 * @return string Html to output
94 public function getHTML() {
95 global $wgShowExceptionDetails;
97 if ( $wgShowExceptionDetails ) {
98 return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $this ) ) ) .
99 '</p><p>Backtrace:</p><p>' .
100 nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $this ) ) ) .
101 "</p>\n";
102 } else {
103 $logId = WebRequest::getRequestId();
104 $type = static::class;
105 return "<div class=\"errorbox\">" .
106 '[' . $logId . '] ' .
107 gmdate( 'Y-m-d H:i:s' ) . ": " .
108 $this->msg( "internalerror-fatal-exception",
109 "Fatal exception of type $1",
110 $type,
111 $logId,
112 MWExceptionHandler::getURL( $this )
113 ) . "</div>\n" .
114 "<!-- Set \$wgShowExceptionDetails = true; " .
115 "at the bottom of LocalSettings.php to show detailed " .
116 "debugging information. -->";
121 * Get the text to display when reporting the error on the command line.
122 * If $wgShowExceptionDetails is true, return a text message with a
123 * backtrace to the error.
125 * @return string
127 public function getText() {
128 global $wgShowExceptionDetails;
130 if ( $wgShowExceptionDetails ) {
131 return MWExceptionHandler::getLogMessage( $this ) .
132 "\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $this ) . "\n";
133 } else {
134 return "Set \$wgShowExceptionDetails = true; " .
135 "in LocalSettings.php to show detailed debugging information.\n";
140 * Return the title of the page when reporting this error in a HTTP response.
142 * @return string
144 public function getPageTitle() {
145 return $this->msg( 'internalerror', 'Internal error' );
149 * Output the exception report using HTML.
151 public function reportHTML() {
152 global $wgOut, $wgSitename;
153 if ( $this->useOutputPage() ) {
154 $wgOut->prepareErrorPage( $this->getPageTitle() );
156 $wgOut->addHTML( $this->getHTML() );
158 $wgOut->output();
159 } else {
160 self::header( 'Content-Type: text/html; charset=utf-8' );
161 echo "<!DOCTYPE html>\n" .
162 '<html><head>' .
163 // Mimick OutputPage::setPageTitle behaviour
164 '<title>' .
165 htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) .
166 '</title>' .
167 '<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
168 "</head><body>\n";
170 echo $this->getHTML();
172 echo "</body></html>\n";
177 * Output a report about the exception and takes care of formatting.
178 * It will be either HTML or plain text based on isCommandLine().
180 public function report() {
181 global $wgMimeType;
183 if ( defined( 'MW_API' ) ) {
184 // Unhandled API exception, we can't be sure that format printer is alive
185 self::header( 'MediaWiki-API-Error: internal_api_error_' . static::class );
186 wfHttpError( 500, 'Internal Server Error', $this->getText() );
187 } elseif ( self::isCommandLine() ) {
188 $message = $this->getText();
189 // T17602: STDERR may not be available
190 if ( defined( 'STDERR' ) ) {
191 fwrite( STDERR, $message );
192 } else {
193 echo $message;
195 } else {
196 self::statusHeader( 500 );
197 self::header( "Content-Type: $wgMimeType; charset=utf-8" );
199 $this->reportHTML();
204 * Check whether we are in command line mode or not to report the exception
205 * in the correct format.
207 * @return bool
209 public static function isCommandLine() {
210 return !empty( $GLOBALS['wgCommandLineMode'] );
214 * Send a header, if we haven't already sent them. We shouldn't,
215 * but sometimes we might in a weird case like Export
216 * @param string $header
218 private static function header( $header ) {
219 if ( !headers_sent() ) {
220 header( $header );
223 private static function statusHeader( $code ) {
224 if ( !headers_sent() ) {
225 HttpStatus::header( $code );