getTrace() as $frame ) { if ( isset( $frame['class'] ) && $frame['class'] === LocalisationCache::class ) { return false; } } // Don't even bother with OutputPage if there's no Title context set, // (e.g. we're in RL code on load.php) - the Skin system (and probably // most of MediaWiki) won't work. return ( !empty( $GLOBALS['wgFullyInitialised'] ) && !empty( $GLOBALS['wgOut'] ) && RequestContext::getMain()->getTitle() && !defined( 'MEDIAWIKI_INSTALL' ) && // Don't send a skinned HTTP 500 page to API clients. !defined( 'MW_API' ) ); } /** * Output the throwable report using HTML * * @param Throwable $e */ private static function reportHTML( Throwable $e ) { 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' ) ); } // Show any custom GUI message before the details if ( $e instanceof MessageSpecifier ) { $wgOut->addHTML( Html::element( 'p', [], Message::newFromSpecifier( $e )->text() ) ); } $wgOut->addHTML( self::getHTML( $e ) ); $wgOut->output(); } else { self::header( 'Content-Type: text/html; charset=utf-8' ); $pageTitle = self::msg( 'internalerror', 'Internal error' ); echo "\n" . '' . // Mimick OutputPage::setPageTitle behaviour '' . htmlspecialchars( self::msg( 'pagetitle', "$1 - $wgSitename", $pageTitle ) ) . '' . '' . "\n"; echo self::getHTML( $e ); echo "\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. * * @param Throwable $e * @return string Html to output */ public static function getHTML( Throwable $e ) { global $wgShowExceptionDetails; if ( $wgShowExceptionDetails ) { $html = "

" . nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $e ) ) ) . '

Backtrace:

' . nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $e ) ) ) . "

\n"; } else { $logId = WebRequest::getRequestId(); $html = "
" . htmlspecialchars( '[' . $logId . '] ' . gmdate( 'Y-m-d H:i:s' ) . ": " . self::msg( "internalerror-fatal-exception", "Fatal exception of type $1", get_class( $e ), $logId, MWExceptionHandler::getURL() ) ) . "
\n" . ""; } return $html; } /** * 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 * @param mixed ...$params To pass to wfMessage() * @return string Message with arguments replaced */ private static function msg( $key, $fallback, ...$params ) { global $wgSitename; // FIXME: Keep logic in sync with MWException::msg. try { $res = wfMessage( $key, ...$params )->text(); } catch ( Exception $e ) { $res = wfMsgReplaceArgs( $fallback, $params ); // If an exception happens inside message rendering, // {{SITENAME}} sometimes won't be replaced. $res = strtr( $res, [ '{{SITENAME}}' => $wgSitename, ] ); } return $res; } /** * @param Throwable $e * @return string */ private static function getText( Throwable $e ) { global $wgShowExceptionDetails; if ( $wgShowExceptionDetails ) { return MWExceptionHandler::getLogMessage( $e ) . "\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $e ) . "\n"; } else { return self::getShowBacktraceError( $e ) . "\n"; } } /** * @param Throwable $e * @return string */ private static function getShowBacktraceError( Throwable $e ) { $var = '$wgShowExceptionDetails = true;'; return "Set $var at the bottom of LocalSettings.php to show detailed debugging information."; } /** * @return bool */ private static function isCommandLine() { return !empty( $GLOBALS['wgCommandLineMode'] ); } /** * @param string $header */ private static function header( $header ) { if ( !headers_sent() ) { header( $header ); } } /** * @param int $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) * * @suppress SecurityCheck-XSS * @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 (T17602). Try to produce meaningful output anyway. Using // echo may corrupt output to STDOUT though. if ( defined( 'STDERR' ) ) { fwrite( STDERR, $message ); } else { echo $message; } } /** * @param Throwable $e */ private static function reportOutageHTML( Throwable $e ) { global $wgShowExceptionDetails, $wgShowHostnames, $wgSitename; $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 ) { $info = str_replace( '$1', Html::element( 'span', [ 'dir' => 'ltr' ], $e->getMessage() ), htmlspecialchars( self::msg( 'dberr-info', '($1)' ) ) ); } else { $info = htmlspecialchars( self::msg( 'dberr-info-hidden', '(Cannot access the database)' ) ); } MediaWikiServices::getInstance()->getMessageCache()->disable(); // no DB access $html = "\n" . '' . '' . htmlspecialchars( $wgSitename ) . '' . '' . "

$sorry

$again

$info

"; if ( $wgShowExceptionDetails ) { $html .= '

Backtrace:

' .
				htmlspecialchars( $e->getTraceAsString() ) . '
'; } $html .= ''; echo $html; } }