2014-02-24 20:13:49 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MediaWiki exception
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Exception
|
|
|
|
|
*/
|
|
|
|
|
class MWException extends Exception {
|
|
|
|
|
/**
|
|
|
|
|
* Should the exception use $wgOut to output the error?
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function useOutputPage() {
|
2014-02-24 20:13:49 +00:00
|
|
|
return $this->useMessageCache() &&
|
|
|
|
|
!empty( $GLOBALS['wgFullyInitialised'] ) &&
|
|
|
|
|
!empty( $GLOBALS['wgOut'] ) &&
|
|
|
|
|
!defined( 'MEDIAWIKI_INSTALL' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether to log this exception in the exception debug log.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.23
|
2014-04-14 19:43:18 +00:00
|
|
|
* @return bool
|
2014-02-24 20:13:49 +00:00
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function isLoggable() {
|
2014-02-24 20:13:49 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Can the extension use the Message class/wfMessage to get i18n-ed messages?
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function useMessageCache() {
|
2014-02-24 20:13:49 +00:00
|
|
|
global $wgLang;
|
|
|
|
|
|
|
|
|
|
foreach ( $this->getTrace() as $frame ) {
|
|
|
|
|
if ( isset( $frame['class'] ) && $frame['class'] === 'LocalisationCache' ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $wgLang instanceof Language;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a message from i18n
|
|
|
|
|
*
|
2014-04-14 19:43:18 +00:00
|
|
|
* @param string $key Message name
|
|
|
|
|
* @param string $fallback Default message if the message cache can't be
|
2014-02-24 20:13:49 +00:00
|
|
|
* called by the exception
|
|
|
|
|
* The function also has other parameters that are arguments for the message
|
2014-04-14 19:43:18 +00:00
|
|
|
* @return string Message with arguments replaced
|
2014-02-24 20:13:49 +00:00
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function msg( $key, $fallback /*[, params...] */ ) {
|
2014-02-24 20:13:49 +00:00
|
|
|
$args = array_slice( func_get_args(), 2 );
|
|
|
|
|
|
|
|
|
|
if ( $this->useMessageCache() ) {
|
2015-02-12 23:01:54 +00:00
|
|
|
try {
|
|
|
|
|
return wfMessage( $key, $args )->text();
|
2015-03-15 01:59:28 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
}
|
2014-02-24 20:13:49 +00:00
|
|
|
}
|
2015-02-12 23:01:54 +00:00
|
|
|
return wfMsgReplaceArgs( $fallback, $args );
|
2014-02-24 20:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If $wgShowExceptionDetails is true, return a HTML message with a
|
|
|
|
|
* backtrace to the error, otherwise show a message to ask to set it to true
|
|
|
|
|
* to show that information.
|
|
|
|
|
*
|
2014-04-14 19:43:18 +00:00
|
|
|
* @return string Html to output
|
2014-02-24 20:13:49 +00:00
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function getHTML() {
|
2014-02-24 20:13:49 +00:00
|
|
|
global $wgShowExceptionDetails;
|
|
|
|
|
|
|
|
|
|
if ( $wgShowExceptionDetails ) {
|
|
|
|
|
return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $this ) ) ) .
|
2014-04-24 19:50:01 +00:00
|
|
|
'</p><p>Backtrace:</p><p>' .
|
|
|
|
|
nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $this ) ) ) .
|
2014-02-24 20:13:49 +00:00
|
|
|
"</p>\n";
|
|
|
|
|
} else {
|
Provide a unique request identifier
When MediaWiki encounters an unhandled exception, the error message it produces
includes a randomly-generated token, which allows the exception details to be
looked up in the error logs. This is useful but narrow: would it not be useful
to have the ability to retrieve all log records associated with a particular
request, rather than just exception details? (Hint: yes.)
So: introduce the notion of a request-global unique ID, retrievable via
WebRequest::getRequestId(). When MediaWiki is behind Apache + mod_unique_id
(which provides the same facility) or some other software which sets a
UNIQUE_ID envvar, the value of that envvar is used as the request ID.
Otherwise, it is a randomly-generated 24-character string.
The request ID supplants exception-specific IDs; MWExceptionHandler::getLogId()
is deprecated, accordingly. The request ID is also added as an annotation to
all Monolog-processed log records, and is exposed client-side as 'wgRequestId'.
This allows developers to associate a page view with log records even when the
page view does not result in an unhandled exception. (For the WMF, I also
intend to add it as an annotation to profiling data).
The request ID is not a tracking token; it does not persist, and it is
associated with a backend request, not with a particular user or a particular
session. Like the data in the NewPP report, the request ID is designed to be
cacheable, so that if, for example, a developer notices something weird in the
HTML, s/he can associate the output with a backend request regardless of
whether the response was served from the cache or directly from the backend.
Some prior art:
* https://httpd.apache.org/docs/2.4/mod/mod_unique_id.html
* http://api.rubyonrails.org/classes/ActionDispatch/RequestId.html
* https://github.com/dabapps/django-log-request-id
* https://packagist.org/packages/php-middleware/request-id
* https://github.com/rhyselsmore/flask-request-id
Change-Id: Iaf90c20c330e0470b9b98627a0228cadefd301d1
2016-03-25 01:43:23 +00:00
|
|
|
$logId = WebRequest::getRequestId();
|
2017-03-07 02:14:14 +00:00
|
|
|
$type = static::class;
|
2014-02-24 20:13:49 +00:00
|
|
|
return "<div class=\"errorbox\">" .
|
2015-02-12 23:01:54 +00:00
|
|
|
'[' . $logId . '] ' .
|
|
|
|
|
gmdate( 'Y-m-d H:i:s' ) . ": " .
|
|
|
|
|
$this->msg( "internalerror-fatal-exception",
|
|
|
|
|
"Fatal exception of type $1",
|
|
|
|
|
$type,
|
|
|
|
|
$logId,
|
|
|
|
|
MWExceptionHandler::getURL( $this )
|
|
|
|
|
) . "</div>\n" .
|
2014-02-24 20:13:49 +00:00
|
|
|
"<!-- Set \$wgShowExceptionDetails = true; " .
|
|
|
|
|
"at the bottom of LocalSettings.php to show detailed " .
|
|
|
|
|
"debugging information. -->";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the text to display when reporting the error on the command line.
|
|
|
|
|
* If $wgShowExceptionDetails is true, return a text message with a
|
|
|
|
|
* backtrace to the error.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function getText() {
|
2014-02-24 20:13:49 +00:00
|
|
|
global $wgShowExceptionDetails;
|
|
|
|
|
|
|
|
|
|
if ( $wgShowExceptionDetails ) {
|
|
|
|
|
return MWExceptionHandler::getLogMessage( $this ) .
|
|
|
|
|
"\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $this ) . "\n";
|
|
|
|
|
} else {
|
|
|
|
|
return "Set \$wgShowExceptionDetails = true; " .
|
|
|
|
|
"in LocalSettings.php to show detailed debugging information.\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the title of the page when reporting this error in a HTTP response.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function getPageTitle() {
|
2014-03-14 05:02:45 +00:00
|
|
|
return $this->msg( 'internalerror', 'Internal error' );
|
2014-02-24 20:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Output the exception report using HTML.
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function reportHTML() {
|
2014-03-14 05:02:45 +00:00
|
|
|
global $wgOut, $wgSitename;
|
2014-02-24 20:13:49 +00:00
|
|
|
if ( $this->useOutputPage() ) {
|
|
|
|
|
$wgOut->prepareErrorPage( $this->getPageTitle() );
|
|
|
|
|
|
2017-05-21 08:29:41 +00:00
|
|
|
$wgOut->addHTML( $this->getHTML() );
|
2014-02-24 20:13:49 +00:00
|
|
|
|
|
|
|
|
$wgOut->output();
|
|
|
|
|
} else {
|
2014-05-13 19:15:06 +00:00
|
|
|
self::header( 'Content-Type: text/html; charset=utf-8' );
|
2014-02-24 20:13:49 +00:00
|
|
|
echo "<!DOCTYPE html>\n" .
|
|
|
|
|
'<html><head>' .
|
2014-03-14 05:02:45 +00:00
|
|
|
// Mimick OutputPage::setPageTitle behaviour
|
2014-04-24 19:50:01 +00:00
|
|
|
'<title>' .
|
|
|
|
|
htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) .
|
|
|
|
|
'</title>' .
|
2014-02-24 20:13:49 +00:00
|
|
|
'<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
|
|
|
|
|
"</head><body>\n";
|
|
|
|
|
|
2017-05-21 08:29:41 +00:00
|
|
|
echo $this->getHTML();
|
2014-02-24 20:13:49 +00:00
|
|
|
|
|
|
|
|
echo "</body></html>\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Output a report about the exception and takes care of formatting.
|
|
|
|
|
* It will be either HTML or plain text based on isCommandLine().
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function report() {
|
2016-10-02 00:02:12 +00:00
|
|
|
global $wgMimeType;
|
|
|
|
|
|
|
|
|
|
if ( defined( 'MW_API' ) ) {
|
|
|
|
|
// Unhandled API exception, we can't be sure that format printer is alive
|
2017-03-07 02:14:14 +00:00
|
|
|
self::header( 'MediaWiki-API-Error: internal_api_error_' . static::class );
|
2016-10-02 00:02:12 +00:00
|
|
|
wfHttpError( 500, 'Internal Server Error', $this->getText() );
|
|
|
|
|
} elseif ( self::isCommandLine() ) {
|
|
|
|
|
$message = $this->getText();
|
|
|
|
|
// T17602: STDERR may not be available
|
|
|
|
|
if ( defined( 'STDERR' ) ) {
|
|
|
|
|
fwrite( STDERR, $message );
|
|
|
|
|
} else {
|
|
|
|
|
echo $message;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
self::statusHeader( 500 );
|
|
|
|
|
self::header( "Content-Type: $wgMimeType; charset=utf-8" );
|
|
|
|
|
|
|
|
|
|
$this->reportHTML();
|
|
|
|
|
}
|
2014-02-24 20:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check whether we are in command line mode or not to report the exception
|
|
|
|
|
* in the correct format.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public static function isCommandLine() {
|
2014-02-24 20:13:49 +00:00
|
|
|
return !empty( $GLOBALS['wgCommandLineMode'] );
|
|
|
|
|
}
|
2014-05-13 19:15:06 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a header, if we haven't already sent them. We shouldn't,
|
|
|
|
|
* but sometimes we might in a weird case like Export
|
2014-08-14 18:22:52 +00:00
|
|
|
* @param string $header
|
2014-05-13 19:15:06 +00:00
|
|
|
*/
|
|
|
|
|
private static function header( $header ) {
|
|
|
|
|
if ( !headers_sent() ) {
|
|
|
|
|
header( $header );
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-01 14:31:52 +00:00
|
|
|
private static function statusHeader( $code ) {
|
|
|
|
|
if ( !headers_sent() ) {
|
|
|
|
|
HttpStatus::header( $code );
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-24 20:13:49 +00:00
|
|
|
}
|