2016-09-14 03:57:54 +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
|
|
|
|
|
* @author Aaron Schulz
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class to expose exceptions to the client (API bots, users, admins using CLI scripts)
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
class MWExceptionRenderer {
|
|
|
|
|
const AS_RAW = 1; // show as text
|
|
|
|
|
const AS_PRETTY = 2; // show as HTML
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-19 16:39:44 +00:00
|
|
|
* @param Exception|Throwable $e Original exception
|
2016-09-14 03:57:54 +00:00
|
|
|
* @param integer $mode MWExceptionExposer::AS_* constant
|
2016-09-19 16:39:44 +00:00
|
|
|
* @param Exception|Throwable|null $eNew New exception from attempting to show the first
|
2016-09-14 03:57:54 +00:00
|
|
|
*/
|
2016-09-19 16:39:44 +00:00
|
|
|
public static function output( $e, $mode, $eNew = null ) {
|
2016-09-14 03:57:54 +00:00
|
|
|
global $wgMimeType;
|
|
|
|
|
|
|
|
|
|
if ( defined( 'MW_API' ) ) {
|
|
|
|
|
// Unhandled API exception, we can't be sure that format printer is alive
|
|
|
|
|
self::header( 'MediaWiki-API-Error: internal_api_error_' . get_class( $e ) );
|
|
|
|
|
wfHttpError( 500, 'Internal Server Error', self::getText( $e ) );
|
|
|
|
|
} elseif ( self::isCommandLine() ) {
|
|
|
|
|
self::printError( self::getText( $e ) );
|
|
|
|
|
} elseif ( $mode === self::AS_PRETTY ) {
|
2016-09-30 23:57:38 +00:00
|
|
|
if ( $e instanceof DBConnectionError ) {
|
|
|
|
|
self::reportOutageHTML( $e );
|
|
|
|
|
} else {
|
|
|
|
|
self::statusHeader( 500 );
|
|
|
|
|
self::header( "Content-Type: $wgMimeType; charset=utf-8" );
|
|
|
|
|
self::reportHTML( $e );
|
|
|
|
|
}
|
2016-09-14 03:57:54 +00:00
|
|
|
} else {
|
|
|
|
|
if ( $eNew ) {
|
|
|
|
|
$message = "MediaWiki internal error.\n\n";
|
|
|
|
|
if ( self::showBackTrace( $e ) ) {
|
|
|
|
|
$message .= 'Original exception: ' .
|
|
|
|
|
MWExceptionHandler::getLogMessage( $e ) .
|
|
|
|
|
"\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $e ) .
|
|
|
|
|
"\n\nException caught inside exception handler: " .
|
|
|
|
|
MWExceptionHandler::getLogMessage( $eNew ) .
|
|
|
|
|
"\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $eNew );
|
|
|
|
|
} else {
|
|
|
|
|
$message .= "Exception caught inside exception handler.\n\n" .
|
|
|
|
|
"Set \$wgShowExceptionDetails = true; at the bottom of LocalSettings.php " .
|
|
|
|
|
"to show detailed debugging information.";
|
|
|
|
|
}
|
|
|
|
|
$message .= "\n";
|
|
|
|
|
} else {
|
|
|
|
|
if ( self::showBackTrace( $e ) ) {
|
|
|
|
|
$message = MWExceptionHandler::getLogMessage( $e ) .
|
|
|
|
|
"\nBacktrace:\n" .
|
|
|
|
|
MWExceptionHandler::getRedactedTraceAsString( $e ) . "\n";
|
|
|
|
|
} else {
|
|
|
|
|
$message = MWExceptionHandler::getPublicLogMessage( $e );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( self::isCommandLine() ) {
|
|
|
|
|
self::printError( $message );
|
|
|
|
|
} else {
|
|
|
|
|
echo nl2br( htmlspecialchars( $message ) ) . "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run hook to allow extensions to modify the text of the exception
|
|
|
|
|
*
|
|
|
|
|
* Called by MWException for b/c
|
|
|
|
|
*
|
2016-09-19 16:39:44 +00:00
|
|
|
* @param Exception|Throwable $e
|
2016-09-14 03:57:54 +00:00
|
|
|
* @param string $name Class name of the exception
|
|
|
|
|
* @param array $args Arguments to pass to the callback functions
|
|
|
|
|
* @return string|null String to output or null if any hook has been called
|
|
|
|
|
*/
|
2016-09-19 16:39:44 +00:00
|
|
|
public static function runHooks( $e, $name, $args = [] ) {
|
2016-09-14 03:57:54 +00:00
|
|
|
global $wgExceptionHooks;
|
|
|
|
|
|
|
|
|
|
if ( !isset( $wgExceptionHooks ) || !is_array( $wgExceptionHooks ) ) {
|
|
|
|
|
return null; // Just silently ignore
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !array_key_exists( $name, $wgExceptionHooks ) ||
|
|
|
|
|
!is_array( $wgExceptionHooks[$name] )
|
|
|
|
|
) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$hooks = $wgExceptionHooks[$name];
|
|
|
|
|
$callargs = array_merge( [ $e ], $args );
|
|
|
|
|
|
|
|
|
|
foreach ( $hooks as $hook ) {
|
|
|
|
|
if (
|
|
|
|
|
is_string( $hook ) ||
|
|
|
|
|
( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) )
|
|
|
|
|
) {
|
|
|
|
|
// 'function' or [ 'class', 'hook' ]
|
|
|
|
|
$result = call_user_func_array( $hook, $callargs );
|
|
|
|
|
} else {
|
|
|
|
|
$result = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_string( $result ) ) {
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-19 16:39:44 +00:00
|
|
|
* @param Exception|Throwable $e
|
2016-09-14 03:57:54 +00:00
|
|
|
* @return bool Should the exception use $wgOut to output the error?
|
|
|
|
|
*/
|
2016-09-19 16:39:44 +00:00
|
|
|
private static function useOutputPage( $e ) {
|
2016-09-14 03:57:54 +00:00
|
|
|
// Can the extension use the Message class/wfMessage to get i18n-ed messages?
|
|
|
|
|
foreach ( $e->getTrace() as $frame ) {
|
|
|
|
|
if ( isset( $frame['class'] ) && $frame['class'] === 'LocalisationCache' ) {
|
2016-09-14 09:28:18 +00:00
|
|
|
return false;
|
2016-09-14 03:57:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
!empty( $GLOBALS['wgFullyInitialised'] ) &&
|
|
|
|
|
!empty( $GLOBALS['wgOut'] ) &&
|
|
|
|
|
!defined( 'MEDIAWIKI_INSTALL' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Output the exception report using HTML
|
|
|
|
|
*
|
2016-09-19 16:39:44 +00:00
|
|
|
* @param Exception|Throwable $e
|
2016-09-14 03:57:54 +00:00
|
|
|
*/
|
2016-09-19 16:39:44 +00:00
|
|
|
private static function reportHTML( $e ) {
|
2016-09-14 03:57:54 +00:00
|
|
|
global $wgOut, $wgSitename;
|
|
|
|
|
|
|
|
|
|
if ( self::useOutputPage( $e ) ) {
|
|
|
|
|
if ( $e instanceof MWException ) {
|
|
|
|
|
$wgOut->prepareErrorPage( $e->getPageTitle() );
|
|
|
|
|
} elseif ( $e instanceof DBReadOnlyError ) {
|
|
|
|
|
$wgOut->prepareErrorPage( self::msg( 'readonly', 'Database is locked' ) );
|
|
|
|
|
} elseif ( $e instanceof DBExpectedError ) {
|
|
|
|
|
$wgOut->prepareErrorPage( self::msg( 'databaseerror', 'Database error' ) );
|
|
|
|
|
} else {
|
|
|
|
|
$wgOut->prepareErrorPage( self::msg( 'internalerror', 'Internal error' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$hookResult = self::runHooks( $e, get_class( $e ) );
|
|
|
|
|
if ( $hookResult ) {
|
|
|
|
|
$wgOut->addHTML( $hookResult );
|
|
|
|
|
} else {
|
2016-09-14 09:28:18 +00:00
|
|
|
// Show any custom GUI message before the details
|
|
|
|
|
if ( $e instanceof MessageSpecifier ) {
|
2016-09-26 22:32:54 +00:00
|
|
|
$wgOut->addHTML( Message::newFromSpecifier( $e )->escaped() );
|
2016-09-14 09:28:18 +00:00
|
|
|
}
|
2016-09-14 03:57:54 +00:00
|
|
|
$wgOut->addHTML( self::getHTML( $e ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$wgOut->output();
|
|
|
|
|
} else {
|
|
|
|
|
self::header( 'Content-Type: text/html; charset=utf-8' );
|
|
|
|
|
$pageTitle = self::msg( 'internalerror', 'Internal error' );
|
|
|
|
|
echo "<!DOCTYPE html>\n" .
|
|
|
|
|
'<html><head>' .
|
|
|
|
|
// Mimick OutputPage::setPageTitle behaviour
|
|
|
|
|
'<title>' .
|
|
|
|
|
htmlspecialchars( self::msg( 'pagetitle', "$1 - $wgSitename", $pageTitle ) ) .
|
|
|
|
|
'</title>' .
|
|
|
|
|
'<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
|
|
|
|
|
"</head><body>\n";
|
|
|
|
|
|
|
|
|
|
$hookResult = self::runHooks( $e, get_class( $e ) . 'Raw' );
|
|
|
|
|
if ( $hookResult ) {
|
|
|
|
|
echo $hookResult;
|
|
|
|
|
} else {
|
|
|
|
|
echo self::getHTML( $e );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo "</body></html>\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2016-09-19 16:39:44 +00:00
|
|
|
* @param Exception|Throwable $e
|
2016-09-14 03:57:54 +00:00
|
|
|
* @return string Html to output
|
|
|
|
|
*/
|
2016-09-19 16:39:44 +00:00
|
|
|
public static function getHTML( $e ) {
|
2016-09-14 03:57:54 +00:00
|
|
|
if ( self::showBackTrace( $e ) ) {
|
2016-09-14 09:28:18 +00:00
|
|
|
$html = "<div class=\"errorbox\"><p>" .
|
2016-09-14 03:57:54 +00:00
|
|
|
nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $e ) ) ) .
|
|
|
|
|
'</p><p>Backtrace:</p><p>' .
|
|
|
|
|
nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $e ) ) ) .
|
2016-09-14 09:28:18 +00:00
|
|
|
"</p></div>\n";
|
2016-09-14 03:57:54 +00:00
|
|
|
} else {
|
|
|
|
|
$logId = WebRequest::getRequestId();
|
2016-09-14 09:28:18 +00:00
|
|
|
$html = "<div class=\"errorbox\">" .
|
2016-09-14 03:57:54 +00:00
|
|
|
'[' . $logId . '] ' .
|
|
|
|
|
gmdate( 'Y-m-d H:i:s' ) . ": " .
|
|
|
|
|
self::msg( "internalerror-fatal-exception",
|
|
|
|
|
"Fatal exception of type $1",
|
|
|
|
|
get_class( $e ),
|
|
|
|
|
$logId,
|
|
|
|
|
MWExceptionHandler::getURL()
|
|
|
|
|
) . "</div>\n" .
|
|
|
|
|
"<!-- Set \$wgShowExceptionDetails = true; " .
|
|
|
|
|
"at the bottom of LocalSettings.php to show detailed " .
|
|
|
|
|
"debugging information. -->";
|
|
|
|
|
}
|
2016-09-14 09:28:18 +00:00
|
|
|
|
|
|
|
|
return $html;
|
2016-09-14 03:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a message from i18n
|
|
|
|
|
*
|
|
|
|
|
* @param string $key Message name
|
|
|
|
|
* @param string $fallback Default message if the message cache can't be
|
|
|
|
|
* called by the exception
|
|
|
|
|
* The function also has other parameters that are arguments for the message
|
|
|
|
|
* @return string Message with arguments replaced
|
|
|
|
|
*/
|
|
|
|
|
private static function msg( $key, $fallback /*[, params...] */ ) {
|
|
|
|
|
$args = array_slice( func_get_args(), 2 );
|
|
|
|
|
try {
|
|
|
|
|
return wfMessage( $key, $args )->text();
|
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
return wfMsgReplaceArgs( $fallback, $args );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-19 16:39:44 +00:00
|
|
|
* @param Exception|Throwable $e
|
2016-09-14 03:57:54 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2016-09-19 17:55:07 +00:00
|
|
|
private static function getText( $e ) {
|
2016-09-14 03:57:54 +00:00
|
|
|
if ( self::showBackTrace( $e ) ) {
|
|
|
|
|
return MWExceptionHandler::getLogMessage( $e ) .
|
|
|
|
|
"\nBacktrace:\n" .
|
|
|
|
|
MWExceptionHandler::getRedactedTraceAsString( $e ) . "\n";
|
|
|
|
|
} else {
|
|
|
|
|
return "Set \$wgShowExceptionDetails = true; " .
|
|
|
|
|
"in LocalSettings.php to show detailed debugging information.\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-19 16:39:44 +00:00
|
|
|
* @param Exception|Throwable $e
|
2016-09-14 03:57:54 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2016-09-19 16:39:44 +00:00
|
|
|
private static function showBackTrace( $e ) {
|
2016-09-14 03:57:54 +00:00
|
|
|
global $wgShowExceptionDetails, $wgShowDBErrorBacktrace;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
$wgShowExceptionDetails &&
|
|
|
|
|
( !( $e instanceof DBError ) || $wgShowDBErrorBacktrace )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private static function isCommandLine() {
|
|
|
|
|
return !empty( $GLOBALS['wgCommandLineMode'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $header
|
|
|
|
|
*/
|
|
|
|
|
private static function header( $header ) {
|
|
|
|
|
if ( !headers_sent() ) {
|
|
|
|
|
header( $header );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param integer $code
|
|
|
|
|
*/
|
|
|
|
|
private static function statusHeader( $code ) {
|
|
|
|
|
if ( !headers_sent() ) {
|
|
|
|
|
HttpStatus::header( $code );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Print a message, if possible to STDERR.
|
|
|
|
|
* Use this in command line mode only (see isCommandLine)
|
|
|
|
|
*
|
|
|
|
|
* @param string $message Failure text
|
|
|
|
|
*/
|
|
|
|
|
private static function printError( $message ) {
|
|
|
|
|
// NOTE: STDERR may not be available, especially if php-cgi is used from the
|
|
|
|
|
// command line (bug #15602). Try to produce meaningful output anyway. Using
|
|
|
|
|
// echo may corrupt output to STDOUT though.
|
|
|
|
|
if ( defined( 'STDERR' ) ) {
|
|
|
|
|
fwrite( STDERR, $message );
|
|
|
|
|
} else {
|
|
|
|
|
echo $message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-19 16:39:44 +00:00
|
|
|
* @param Exception|Throwable $e
|
2016-09-14 03:57:54 +00:00
|
|
|
*/
|
2016-09-19 16:39:44 +00:00
|
|
|
private static function reportOutageHTML( $e ) {
|
2016-09-14 03:57:54 +00:00
|
|
|
global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
|
|
|
|
|
|
|
|
|
|
$sorry = htmlspecialchars( self::msg(
|
|
|
|
|
'dberr-problems',
|
|
|
|
|
'Sorry! This site is experiencing technical difficulties.'
|
|
|
|
|
) );
|
|
|
|
|
$again = htmlspecialchars( self::msg(
|
|
|
|
|
'dberr-again',
|
|
|
|
|
'Try waiting a few minutes and reloading.'
|
|
|
|
|
) );
|
|
|
|
|
|
|
|
|
|
if ( $wgShowHostnames || $wgShowSQLErrors ) {
|
|
|
|
|
$info = str_replace(
|
|
|
|
|
'$1',
|
|
|
|
|
Html::element( 'span', [ 'dir' => 'ltr' ], htmlspecialchars( $e->getMessage() ) ),
|
|
|
|
|
htmlspecialchars( self::msg( 'dberr-info', '($1)' ) )
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$info = htmlspecialchars( self::msg(
|
|
|
|
|
'dberr-info-hidden',
|
|
|
|
|
'(Cannot access the database)'
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MessageCache::singleton()->disable(); // no DB access
|
|
|
|
|
|
|
|
|
|
$html = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
|
|
|
|
|
|
|
|
|
|
if ( $wgShowDBErrorBacktrace ) {
|
|
|
|
|
$html .= '<p>Backtrace:</p><pre>' .
|
|
|
|
|
htmlspecialchars( $e->getTraceAsString() ) . '</pre>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$html .= '<hr />';
|
|
|
|
|
$html .= self::googleSearchForm();
|
|
|
|
|
|
|
|
|
|
echo $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private static function googleSearchForm() {
|
|
|
|
|
global $wgSitename, $wgCanonicalServer, $wgRequest;
|
|
|
|
|
|
|
|
|
|
$usegoogle = htmlspecialchars( self::msg(
|
|
|
|
|
'dberr-usegoogle',
|
|
|
|
|
'You can try searching via Google in the meantime.'
|
|
|
|
|
) );
|
|
|
|
|
$outofdate = htmlspecialchars( self::msg(
|
|
|
|
|
'dberr-outofdate',
|
|
|
|
|
'Note that their indexes of our content may be out of date.'
|
|
|
|
|
) );
|
|
|
|
|
$googlesearch = htmlspecialchars( self::msg( 'searchbutton', 'Search' ) );
|
|
|
|
|
$search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
|
|
|
|
|
$server = htmlspecialchars( $wgCanonicalServer );
|
|
|
|
|
$sitename = htmlspecialchars( $wgSitename );
|
|
|
|
|
$trygoogle = <<<EOT
|
|
|
|
|
<div style="margin: 1.5em">$usegoogle<br />
|
|
|
|
|
<small>$outofdate</small>
|
|
|
|
|
</div>
|
|
|
|
|
<form method="get" action="//www.google.com/search" id="googlesearch">
|
|
|
|
|
<input type="hidden" name="domains" value="$server" />
|
|
|
|
|
<input type="hidden" name="num" value="50" />
|
|
|
|
|
<input type="hidden" name="ie" value="UTF-8" />
|
|
|
|
|
<input type="hidden" name="oe" value="UTF-8" />
|
|
|
|
|
<input type="text" name="q" size="31" maxlength="255" value="$search" />
|
|
|
|
|
<input type="submit" name="btnG" value="$googlesearch" />
|
|
|
|
|
<p>
|
|
|
|
|
<label><input type="radio" name="sitesearch" value="$server" checked="checked" />$sitename</label>
|
|
|
|
|
<label><input type="radio" name="sitesearch" value="" />WWW</label>
|
|
|
|
|
</p>
|
|
|
|
|
</form>
|
|
|
|
|
EOT;
|
|
|
|
|
return $trygoogle;
|
|
|
|
|
}
|
|
|
|
|
}
|