7f70c4fbaf3ea2111a28431b90e3360ba7cef0f5
[lhc/web/wiklou.git] / includes / exception / MWException.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 /**
22 * MediaWiki exception
23 *
24 * @ingroup Exception
25 */
26 class MWException extends Exception {
27 /**
28 * Should the exception use $wgOut to output the error?
29 *
30 * @return bool
31 */
32 public function useOutputPage() {
33 return $this->useMessageCache() &&
34 !empty( $GLOBALS['wgFullyInitialised'] ) &&
35 !empty( $GLOBALS['wgOut'] ) &&
36 !defined( 'MEDIAWIKI_INSTALL' );
37 }
38
39 /**
40 * Whether to log this exception in the exception debug log.
41 *
42 * @since 1.23
43 * @return bool
44 */
45 public function isLoggable() {
46 return true;
47 }
48
49 /**
50 * Can the extension use the Message class/wfMessage to get i18n-ed messages?
51 *
52 * @return bool
53 */
54 public function useMessageCache() {
55 global $wgLang;
56
57 foreach ( $this->getTrace() as $frame ) {
58 if ( isset( $frame['class'] ) && $frame['class'] === LocalisationCache::class ) {
59 return false;
60 }
61 }
62
63 return $wgLang instanceof Language;
64 }
65
66 /**
67 * Get a message from i18n
68 *
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
74 */
75 public function msg( $key, $fallback /*[, params...] */ ) {
76 global $wgSitename;
77 $args = array_slice( func_get_args(), 2 );
78
79 // FIXME: Keep logic in sync with MWExceptionRenderer::msg.
80 $res = false;
81 if ( $this->useMessageCache() ) {
82 try {
83 $res = wfMessage( $key, $args )->text();
84 } catch ( Exception $e ) {
85 }
86 }
87 if ( $res === false ) {
88 $res = wfMsgReplaceArgs( $fallback, $args );
89 // If an exception happens inside message rendering,
90 // {{SITENAME}} sometimes won't be replaced.
91 $res = strtr( $res, [
92 '{{SITENAME}}' => $wgSitename,
93 ] );
94 }
95 return $res;
96 }
97
98 /**
99 * If $wgShowExceptionDetails is true, return a HTML message with a
100 * backtrace to the error, otherwise show a message to ask to set it to true
101 * to show that information.
102 *
103 * @return string Html to output
104 */
105 public function getHTML() {
106 global $wgShowExceptionDetails;
107
108 if ( $wgShowExceptionDetails ) {
109 return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $this ) ) ) .
110 '</p><p>Backtrace:</p><p>' .
111 nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $this ) ) ) .
112 "</p>\n";
113 } else {
114 $logId = WebRequest::getRequestId();
115 $type = static::class;
116 return Html::errorBox(
117 htmlspecialchars(
118 '[' . $logId . '] ' .
119 gmdate( 'Y-m-d H:i:s' ) . ": " .
120 $this->msg( "internalerror-fatal-exception",
121 "Fatal exception of type $1",
122 $type,
123 $logId,
124 MWExceptionHandler::getURL()
125 )
126 ) ) .
127 "<!-- Set \$wgShowExceptionDetails = true; " .
128 "at the bottom of LocalSettings.php to show detailed " .
129 "debugging information. -->";
130 }
131 }
132
133 /**
134 * Get the text to display when reporting the error on the command line.
135 * If $wgShowExceptionDetails is true, return a text message with a
136 * backtrace to the error.
137 *
138 * @return string
139 */
140 public function getText() {
141 global $wgShowExceptionDetails;
142
143 if ( $wgShowExceptionDetails ) {
144 return MWExceptionHandler::getLogMessage( $this ) .
145 "\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $this ) . "\n";
146 } else {
147 return "Set \$wgShowExceptionDetails = true; " .
148 "in LocalSettings.php to show detailed debugging information.\n";
149 }
150 }
151
152 /**
153 * Return the title of the page when reporting this error in a HTTP response.
154 *
155 * @return string
156 */
157 public function getPageTitle() {
158 return $this->msg( 'internalerror', 'Internal error' );
159 }
160
161 /**
162 * Output the exception report using HTML.
163 */
164 public function reportHTML() {
165 global $wgOut, $wgSitename;
166 if ( $this->useOutputPage() ) {
167 $wgOut->prepareErrorPage( $this->getPageTitle() );
168 // Manually set the html title, since sometimes
169 // {{SITENAME}} does not get replaced for exceptions
170 // happening inside message rendering.
171 $wgOut->setHTMLTitle(
172 $this->msg(
173 'pagetitle',
174 "$1 - $wgSitename",
175 $this->getPageTitle()
176 )
177 );
178
179 $wgOut->addHTML( $this->getHTML() );
180
181 $wgOut->output();
182 } else {
183 self::header( 'Content-Type: text/html; charset=utf-8' );
184 echo "<!DOCTYPE html>\n" .
185 '<html><head>' .
186 // Mimick OutputPage::setPageTitle behaviour
187 '<title>' .
188 htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) .
189 '</title>' .
190 '<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
191 "</head><body>\n";
192
193 echo $this->getHTML();
194
195 echo "</body></html>\n";
196 }
197 }
198
199 /**
200 * Output a report about the exception and takes care of formatting.
201 * It will be either HTML or plain text based on isCommandLine().
202 */
203 public function report() {
204 global $wgMimeType;
205
206 if ( defined( 'MW_API' ) ) {
207 // Unhandled API exception, we can't be sure that format printer is alive
208 self::header( 'MediaWiki-API-Error: internal_api_error_' . static::class );
209 wfHttpError( 500, 'Internal Server Error', $this->getText() );
210 } elseif ( self::isCommandLine() ) {
211 $message = $this->getText();
212 // T17602: STDERR may not be available
213 if ( !defined( 'MW_PHPUNIT_TEST' ) && defined( 'STDERR' ) ) {
214 fwrite( STDERR, $message );
215 } else {
216 echo $message;
217 }
218 } else {
219 self::statusHeader( 500 );
220 self::header( "Content-Type: $wgMimeType; charset=utf-8" );
221
222 $this->reportHTML();
223 }
224 }
225
226 /**
227 * Check whether we are in command line mode or not to report the exception
228 * in the correct format.
229 *
230 * @return bool
231 */
232 public static function isCommandLine() {
233 return !empty( $GLOBALS['wgCommandLineMode'] );
234 }
235
236 /**
237 * Send a header, if we haven't already sent them. We shouldn't,
238 * but sometimes we might in a weird case like Export
239 * @param string $header
240 */
241 private static function header( $header ) {
242 if ( !headers_sent() ) {
243 header( $header );
244 }
245 }
246 private static function statusHeader( $code ) {
247 if ( !headers_sent() ) {
248 HttpStatus::header( $code );
249 }
250 }
251 }