2006-06-07 06:40:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
2007-04-21 12:42:27 +00:00
|
|
|
* MediaWiki exception
|
2007-04-20 08:55:14 +00:00
|
|
|
* @addtogroup Exception
|
|
|
|
|
*/
|
2006-06-07 06:40:24 +00:00
|
|
|
class MWException extends Exception
|
|
|
|
|
{
|
|
|
|
|
function useOutputPage() {
|
2006-07-26 07:15:39 +00:00
|
|
|
return !empty( $GLOBALS['wgFullyInitialised'] ) &&
|
|
|
|
|
!empty( $GLOBALS['wgArticle'] ) && !empty( $GLOBALS['wgTitle'] );
|
2006-06-07 06:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function useMessageCache() {
|
|
|
|
|
global $wgLang;
|
|
|
|
|
return is_object( $wgLang );
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-08 18:34:06 +00:00
|
|
|
function runHooks( $name, $args = array() ) {
|
|
|
|
|
global $wgExceptionHooks;
|
|
|
|
|
if( !isset( $wgExceptionHooks ) || !is_array( $wgExceptionHooks ) )
|
|
|
|
|
return; // Just silently ignore
|
|
|
|
|
if( !array_key_exists( $name, $wgExceptionHooks ) || !is_array( $wgExceptionHooks[ $name ] ) )
|
|
|
|
|
return;
|
|
|
|
|
$hooks = $wgExceptionHooks[ $name ];
|
|
|
|
|
$callargs = array_merge( array( $this ), $args );
|
|
|
|
|
|
|
|
|
|
foreach( $hooks as $hook ) {
|
|
|
|
|
if( is_string( $hook ) || ( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) ) ) { //'function' or array( 'class', hook' )
|
|
|
|
|
$result = call_user_func_array( $hook, $callargs );
|
|
|
|
|
} else {
|
|
|
|
|
$result = null;
|
|
|
|
|
}
|
|
|
|
|
if( is_string( $result ) )
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-21 12:42:27 +00:00
|
|
|
/** Get a message from i18n */
|
2006-06-07 06:40:24 +00:00
|
|
|
function msg( $key, $fallback /*[, params...] */ ) {
|
|
|
|
|
$args = array_slice( func_get_args(), 2 );
|
|
|
|
|
if ( $this->useMessageCache() ) {
|
|
|
|
|
return wfMsgReal( $key, $args );
|
|
|
|
|
} else {
|
|
|
|
|
return wfMsgReplaceArgs( $fallback, $args );
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-10-11 18:57:49 +00:00
|
|
|
|
2007-04-21 12:42:27 +00:00
|
|
|
/* If wgShowExceptionDetails, return a HTML message with a backtrace to the error. */
|
2006-06-07 06:40:24 +00:00
|
|
|
function getHTML() {
|
2006-10-11 18:57:49 +00:00
|
|
|
global $wgShowExceptionDetails;
|
|
|
|
|
if( $wgShowExceptionDetails ) {
|
|
|
|
|
return '<p>' . htmlspecialchars( $this->getMessage() ) .
|
|
|
|
|
'</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
|
|
|
|
|
"</p>\n";
|
|
|
|
|
} else {
|
|
|
|
|
return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
|
2007-09-30 00:19:43 +00:00
|
|
|
"at the bottom of LocalSettings.php to show detailed " .
|
|
|
|
|
"debugging information.</p>";
|
2006-10-11 18:57:49 +00:00
|
|
|
}
|
2006-06-07 06:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
2007-04-21 12:42:27 +00:00
|
|
|
/* If wgShowExceptionDetails, return a text message with a backtrace to the error */
|
2006-06-07 06:40:24 +00:00
|
|
|
function getText() {
|
2006-10-11 18:57:49 +00:00
|
|
|
global $wgShowExceptionDetails;
|
|
|
|
|
if( $wgShowExceptionDetails ) {
|
|
|
|
|
return $this->getMessage() .
|
|
|
|
|
"\nBacktrace:\n" . $this->getTraceAsString() . "\n";
|
|
|
|
|
} else {
|
|
|
|
|
return "<p>Set <tt>\$wgShowExceptionDetails = true;</tt> " .
|
|
|
|
|
"in LocalSettings.php to show detailed debugging information.</p>";
|
|
|
|
|
}
|
2006-06-07 06:40:24 +00:00
|
|
|
}
|
2007-04-21 12:42:27 +00:00
|
|
|
|
|
|
|
|
/* Return titles of this error page */
|
2006-06-07 06:40:24 +00:00
|
|
|
function getPageTitle() {
|
|
|
|
|
if ( $this->useMessageCache() ) {
|
|
|
|
|
return wfMsg( 'internalerror' );
|
|
|
|
|
} else {
|
|
|
|
|
global $wgSitename;
|
|
|
|
|
return "$wgSitename error";
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-04-21 12:42:27 +00:00
|
|
|
|
|
|
|
|
/** Return the requested URL and point to file and line number from which the
|
|
|
|
|
* exception occured
|
|
|
|
|
*/
|
2006-08-02 17:40:09 +00:00
|
|
|
function getLogMessage() {
|
2007-01-16 01:45:51 +00:00
|
|
|
global $wgRequest;
|
2006-08-02 17:40:09 +00:00
|
|
|
$file = $this->getFile();
|
|
|
|
|
$line = $this->getLine();
|
|
|
|
|
$message = $this->getMessage();
|
2007-01-16 01:45:51 +00:00
|
|
|
return $wgRequest->getRequestURL() . " Exception from line $line of $file: $message";
|
2006-08-02 17:40:09 +00:00
|
|
|
}
|
2007-04-21 12:42:27 +00:00
|
|
|
|
|
|
|
|
/** Output the exception report using HTML */
|
2006-06-07 06:40:24 +00:00
|
|
|
function reportHTML() {
|
|
|
|
|
global $wgOut;
|
|
|
|
|
if ( $this->useOutputPage() ) {
|
|
|
|
|
$wgOut->setPageTitle( $this->getPageTitle() );
|
|
|
|
|
$wgOut->setRobotpolicy( "noindex,nofollow" );
|
|
|
|
|
$wgOut->setArticleRelated( false );
|
|
|
|
|
$wgOut->enableClientCache( false );
|
|
|
|
|
$wgOut->redirect( '' );
|
|
|
|
|
$wgOut->clearHTML();
|
2008-01-08 18:34:06 +00:00
|
|
|
if( $hookResult = $this->runHooks( get_class( $this ) ) ) {
|
|
|
|
|
$wgOut->addHTML( $hookResult );
|
|
|
|
|
} else {
|
|
|
|
|
$wgOut->addHTML( $this->getHTML() );
|
|
|
|
|
}
|
2006-06-07 06:40:24 +00:00
|
|
|
$wgOut->output();
|
|
|
|
|
} else {
|
2008-01-08 18:34:06 +00:00
|
|
|
if( $hookResult = $this->runHooks( get_class( $this ) . "Raw" ) ) {
|
|
|
|
|
die( $hookResult );
|
|
|
|
|
}
|
2006-06-07 06:40:24 +00:00
|
|
|
echo $this->htmlHeader();
|
|
|
|
|
echo $this->getHTML();
|
|
|
|
|
echo $this->htmlFooter();
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-04-21 12:42:27 +00:00
|
|
|
|
|
|
|
|
/* Output a report about the exception and takes care of formatting.
|
|
|
|
|
* It will be either HTML or plain text based on $wgCommandLineMode.
|
|
|
|
|
*/
|
2006-06-07 06:40:24 +00:00
|
|
|
function report() {
|
|
|
|
|
global $wgCommandLineMode;
|
|
|
|
|
if ( $wgCommandLineMode ) {
|
2007-12-07 04:21:35 +00:00
|
|
|
fwrite( STDERR, $this->getText() );
|
2006-06-07 06:40:24 +00:00
|
|
|
} else {
|
2006-08-02 17:40:09 +00:00
|
|
|
$log = $this->getLogMessage();
|
|
|
|
|
if ( $log ) {
|
|
|
|
|
wfDebugLog( 'exception', $log );
|
|
|
|
|
}
|
2006-06-07 06:40:24 +00:00
|
|
|
$this->reportHTML();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function htmlHeader() {
|
|
|
|
|
global $wgLogo, $wgSitename, $wgOutputEncoding;
|
|
|
|
|
|
|
|
|
|
if ( !headers_sent() ) {
|
|
|
|
|
header( 'HTTP/1.0 500 Internal Server Error' );
|
|
|
|
|
header( 'Content-type: text/html; charset='.$wgOutputEncoding );
|
|
|
|
|
/* Don't cache error pages! They cause no end of trouble... */
|
|
|
|
|
header( 'Cache-control: none' );
|
|
|
|
|
header( 'Pragma: nocache' );
|
|
|
|
|
}
|
|
|
|
|
$title = $this->getPageTitle();
|
|
|
|
|
echo "<html>
|
|
|
|
|
<head>
|
|
|
|
|
<title>$title</title>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
|
|
|
|
|
";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function htmlFooter() {
|
|
|
|
|
echo "</body></html>";
|
2006-07-11 17:40:11 +00:00
|
|
|
}
|
2006-06-07 06:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2006-07-11 17:40:11 +00:00
|
|
|
* Exception class which takes an HTML error message, and does not
|
2006-06-07 06:40:24 +00:00
|
|
|
* produce a backtrace. Replacement for OutputPage::fatalError().
|
2007-04-20 08:55:14 +00:00
|
|
|
* @addtogroup Exception
|
2006-06-07 06:40:24 +00:00
|
|
|
*/
|
|
|
|
|
class FatalError extends MWException {
|
|
|
|
|
function getHTML() {
|
|
|
|
|
return $this->getMessage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getText() {
|
|
|
|
|
return $this->getMessage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
|
|
|
|
* @addtogroup Exception
|
|
|
|
|
*/
|
2006-06-07 06:40:24 +00:00
|
|
|
class ErrorPageError extends MWException {
|
|
|
|
|
public $title, $msg;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Note: these arguments are keys into wfMsg(), not text!
|
|
|
|
|
*/
|
|
|
|
|
function __construct( $title, $msg ) {
|
|
|
|
|
$this->title = $title;
|
|
|
|
|
$this->msg = $msg;
|
|
|
|
|
parent::__construct( wfMsg( $msg ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function report() {
|
|
|
|
|
global $wgOut;
|
|
|
|
|
$wgOut->showErrorPage( $this->title, $this->msg );
|
|
|
|
|
$wgOut->output();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Install an exception handler for MediaWiki exception types.
|
|
|
|
|
*/
|
|
|
|
|
function wfInstallExceptionHandler() {
|
|
|
|
|
set_exception_handler( 'wfExceptionHandler' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Report an exception to the user
|
|
|
|
|
*/
|
|
|
|
|
function wfReportException( Exception $e ) {
|
2006-11-29 05:45:03 +00:00
|
|
|
if ( $e instanceof MWException ) {
|
2006-06-07 06:40:24 +00:00
|
|
|
try {
|
|
|
|
|
$e->report();
|
|
|
|
|
} catch ( Exception $e2 ) {
|
|
|
|
|
// Exception occurred from within exception handler
|
2006-07-11 17:40:11 +00:00
|
|
|
// Show a simpler error message for the original exception,
|
2006-06-07 06:40:24 +00:00
|
|
|
// don't try to invoke report()
|
|
|
|
|
$message = "MediaWiki internal error.\n\n" .
|
2006-07-11 17:40:11 +00:00
|
|
|
"Original exception: " . $e->__toString() .
|
|
|
|
|
"\n\nException caught inside exception handler: " .
|
2006-06-07 06:40:24 +00:00
|
|
|
$e2->__toString() . "\n";
|
|
|
|
|
|
|
|
|
|
if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
|
2007-12-06 21:07:49 +00:00
|
|
|
fwrite( STDERR, $message );
|
2006-06-07 06:40:24 +00:00
|
|
|
} else {
|
|
|
|
|
echo nl2br( htmlspecialchars( $message ) ). "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
echo $e->__toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Exception handler which simulates the appropriate catch() handling:
|
2006-07-11 17:40:11 +00:00
|
|
|
*
|
2006-06-07 06:40:24 +00:00
|
|
|
* try {
|
|
|
|
|
* ...
|
|
|
|
|
* } catch ( MWException $e ) {
|
|
|
|
|
* $e->report();
|
|
|
|
|
* } catch ( Exception $e ) {
|
|
|
|
|
* echo $e->__toString();
|
|
|
|
|
* }
|
|
|
|
|
*/
|
|
|
|
|
function wfExceptionHandler( $e ) {
|
|
|
|
|
global $wgFullyInitialised;
|
|
|
|
|
wfReportException( $e );
|
2007-04-21 12:42:27 +00:00
|
|
|
|
2006-06-07 06:40:24 +00:00
|
|
|
// Final cleanup, similar to wfErrorExit()
|
|
|
|
|
if ( $wgFullyInitialised ) {
|
|
|
|
|
try {
|
2006-07-14 05:35:31 +00:00
|
|
|
wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
|
2006-06-07 06:40:24 +00:00
|
|
|
} catch ( Exception $e ) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Exit value should be nonzero for the benefit of shell jobs
|
|
|
|
|
exit( 1 );
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-29 01:19:14 +00:00
|
|
|
|