Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / exception / MWException.php
blobbebd91580f59aad788b5795bd9ebecc49b08a5ee
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 * Run hook to allow extensions to modify the text of the exception
69 * @param string $name Class name of the exception
70 * @param array $args Arguments to pass to the callback functions
71 * @return string|null String to output or null if any hook has been called
73 public function runHooks( $name, $args = [] ) {
74 global $wgExceptionHooks;
76 if ( !isset( $wgExceptionHooks ) || !is_array( $wgExceptionHooks ) ) {
77 return null; // Just silently ignore
80 if ( !array_key_exists( $name, $wgExceptionHooks ) ||
81 !is_array( $wgExceptionHooks[$name] )
82 ) {
83 return null;
86 $hooks = $wgExceptionHooks[$name];
87 $callargs = array_merge( [ $this ], $args );
89 foreach ( $hooks as $hook ) {
90 if (
91 is_string( $hook ) ||
92 ( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) )
93 ) {
94 // 'function' or array( 'class', hook' )
95 $result = call_user_func_array( $hook, $callargs );
96 } else {
97 $result = null;
100 if ( is_string( $result ) ) {
101 return $result;
104 return null;
108 * Get a message from i18n
110 * @param string $key Message name
111 * @param string $fallback Default message if the message cache can't be
112 * called by the exception
113 * The function also has other parameters that are arguments for the message
114 * @return string Message with arguments replaced
116 public function msg( $key, $fallback /*[, params...] */ ) {
117 $args = array_slice( func_get_args(), 2 );
119 if ( $this->useMessageCache() ) {
120 try {
121 return wfMessage( $key, $args )->text();
122 } catch ( Exception $e ) {
125 return wfMsgReplaceArgs( $fallback, $args );
129 * If $wgShowExceptionDetails is true, return a HTML message with a
130 * backtrace to the error, otherwise show a message to ask to set it to true
131 * to show that information.
133 * @return string Html to output
135 public function getHTML() {
136 global $wgShowExceptionDetails;
138 if ( $wgShowExceptionDetails ) {
139 return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $this ) ) ) .
140 '</p><p>Backtrace:</p><p>' .
141 nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $this ) ) ) .
142 "</p>\n";
143 } else {
144 $logId = WebRequest::getRequestId();
145 $type = get_class( $this );
146 return "<div class=\"errorbox\">" .
147 '[' . $logId . '] ' .
148 gmdate( 'Y-m-d H:i:s' ) . ": " .
149 $this->msg( "internalerror-fatal-exception",
150 "Fatal exception of type $1",
151 $type,
152 $logId,
153 MWExceptionHandler::getURL( $this )
154 ) . "</div>\n" .
155 "<!-- Set \$wgShowExceptionDetails = true; " .
156 "at the bottom of LocalSettings.php to show detailed " .
157 "debugging information. -->";
162 * Get the text to display when reporting the error on the command line.
163 * If $wgShowExceptionDetails is true, return a text message with a
164 * backtrace to the error.
166 * @return string
168 public function getText() {
169 global $wgShowExceptionDetails;
171 if ( $wgShowExceptionDetails ) {
172 return MWExceptionHandler::getLogMessage( $this ) .
173 "\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $this ) . "\n";
174 } else {
175 return "Set \$wgShowExceptionDetails = true; " .
176 "in LocalSettings.php to show detailed debugging information.\n";
181 * Return the title of the page when reporting this error in a HTTP response.
183 * @return string
185 public function getPageTitle() {
186 return $this->msg( 'internalerror', 'Internal error' );
190 * Output the exception report using HTML.
192 public function reportHTML() {
193 global $wgOut, $wgSitename;
194 if ( $this->useOutputPage() ) {
195 $wgOut->prepareErrorPage( $this->getPageTitle() );
197 $hookResult = $this->runHooks( get_class( $this ) );
198 if ( $hookResult ) {
199 $wgOut->addHTML( $hookResult );
200 } else {
201 $wgOut->addHTML( $this->getHTML() );
204 $wgOut->output();
205 } else {
206 self::header( 'Content-Type: text/html; charset=utf-8' );
207 echo "<!DOCTYPE html>\n" .
208 '<html><head>' .
209 // Mimick OutputPage::setPageTitle behaviour
210 '<title>' .
211 htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) .
212 '</title>' .
213 '<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
214 "</head><body>\n";
216 $hookResult = $this->runHooks( get_class( $this ) . 'Raw' );
217 if ( $hookResult ) {
218 echo $hookResult;
219 } else {
220 echo $this->getHTML();
223 echo "</body></html>\n";
228 * Output a report about the exception and takes care of formatting.
229 * It will be either HTML or plain text based on isCommandLine().
231 public function report() {
232 global $wgMimeType;
234 if ( defined( 'MW_API' ) ) {
235 // Unhandled API exception, we can't be sure that format printer is alive
236 self::header( 'MediaWiki-API-Error: internal_api_error_' . get_class( $this ) );
237 wfHttpError( 500, 'Internal Server Error', $this->getText() );
238 } elseif ( self::isCommandLine() ) {
239 MWExceptionHandler::printError( $this->getText() );
240 } else {
241 self::statusHeader( 500 );
242 self::header( "Content-Type: $wgMimeType; charset=utf-8" );
244 $this->reportHTML();
249 * Check whether we are in command line mode or not to report the exception
250 * in the correct format.
252 * @return bool
254 public static function isCommandLine() {
255 return !empty( $GLOBALS['wgCommandLineMode'] );
259 * Send a header, if we haven't already sent them. We shouldn't,
260 * but sometimes we might in a weird case like Export
261 * @param string $header
263 private static function header( $header ) {
264 if ( !headers_sent() ) {
265 header( $header );
268 private static function statusHeader( $code ) {
269 if ( !headers_sent() ) {
270 HttpStatus::header( $code );