Merge "Perform a permission check on the title when changing the page language"
[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' ) {
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 $args = array_slice( func_get_args(), 2 );
77
78 if ( $this->useMessageCache() ) {
79 try {
80 return wfMessage( $key, $args )->text();
81 } catch ( Exception $e ) {
82 }
83 }
84 return wfMsgReplaceArgs( $fallback, $args );
85 }
86
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.
91 *
92 * @return string Html to output
93 */
94 public function getHTML() {
95 global $wgShowExceptionDetails;
96
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. -->";
117 }
118 }
119
120 /**
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.
124 *
125 * @return string
126 */
127 public function getText() {
128 global $wgShowExceptionDetails;
129
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";
136 }
137 }
138
139 /**
140 * Return the title of the page when reporting this error in a HTTP response.
141 *
142 * @return string
143 */
144 public function getPageTitle() {
145 return $this->msg( 'internalerror', 'Internal error' );
146 }
147
148 /**
149 * Output the exception report using HTML.
150 */
151 public function reportHTML() {
152 global $wgOut, $wgSitename;
153 if ( $this->useOutputPage() ) {
154 $wgOut->prepareErrorPage( $this->getPageTitle() );
155
156 $wgOut->addHTML( $this->getHTML() );
157
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";
169
170 echo $this->getHTML();
171
172 echo "</body></html>\n";
173 }
174 }
175
176 /**
177 * Output a report about the exception and takes care of formatting.
178 * It will be either HTML or plain text based on isCommandLine().
179 */
180 public function report() {
181 global $wgMimeType;
182
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;
194 }
195 } else {
196 self::statusHeader( 500 );
197 self::header( "Content-Type: $wgMimeType; charset=utf-8" );
198
199 $this->reportHTML();
200 }
201 }
202
203 /**
204 * Check whether we are in command line mode or not to report the exception
205 * in the correct format.
206 *
207 * @return bool
208 */
209 public static function isCommandLine() {
210 return !empty( $GLOBALS['wgCommandLineMode'] );
211 }
212
213 /**
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
217 */
218 private static function header( $header ) {
219 if ( !headers_sent() ) {
220 header( $header );
221 }
222 }
223 private static function statusHeader( $code ) {
224 if ( !headers_sent() ) {
225 HttpStatus::header( $code );
226 }
227 }
228 }