Merge "Add SPARQL client to core"
[lhc/web/wiklou.git] / includes / exception / MWExceptionRenderer.php
index a9bd4da..dc8dfd0 100644 (file)
@@ -16,7 +16,6 @@
  * http://www.gnu.org/copyleft/gpl.html
  *
  * @file
- * @author Aaron Schulz
  */
 
 use Wikimedia\Rdbms\DBConnectionError;
@@ -34,7 +33,7 @@ class MWExceptionRenderer {
 
        /**
         * @param Exception|Throwable $e Original exception
-        * @param integer $mode MWExceptionExposer::AS_* constant
+        * @param int $mode MWExceptionExposer::AS_* constant
         * @param Exception|Throwable|null $eNew New exception from attempting to show the first
         */
        public static function output( $e, $mode, $eNew = null ) {
@@ -47,10 +46,10 @@ class MWExceptionRenderer {
                } elseif ( self::isCommandLine() ) {
                        self::printError( self::getText( $e ) );
                } elseif ( $mode === self::AS_PRETTY ) {
+                       self::statusHeader( 500 );
                        if ( $e instanceof DBConnectionError ) {
                                self::reportOutageHTML( $e );
                        } else {
-                               self::statusHeader( 500 );
                                self::header( "Content-Type: $wgMimeType; charset=utf-8" );
                                self::reportHTML( $e );
                        }
@@ -84,51 +83,6 @@ class MWExceptionRenderer {
                }
        }
 
-       /**
-        * Run hook to allow extensions to modify the text of the exception
-        *
-        * Called by MWException for b/c
-        *
-        * @param Exception|Throwable $e
-        * @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
-        */
-       public static function runHooks( $e, $name, $args = [] ) {
-               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;
-       }
-
        /**
         * @param Exception|Throwable $e
         * @return bool Should the exception use $wgOut to output the error?
@@ -136,14 +90,19 @@ class MWExceptionRenderer {
        private static function useOutputPage( $e ) {
                // 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' ) {
+                       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' )
                );
        }
@@ -167,16 +126,11 @@ class MWExceptionRenderer {
                                $wgOut->prepareErrorPage( self::msg( 'internalerror', 'Internal error' ) );
                        }
 
-                       $hookResult = self::runHooks( $e, get_class( $e ) );
-                       if ( $hookResult ) {
-                               $wgOut->addHTML( $hookResult );
-                       } else {
-                               // Show any custom GUI message before the details
-                               if ( $e instanceof MessageSpecifier ) {
-                                       $wgOut->addHTML( Message::newFromSpecifier( $e )->escaped() );
-                               }
-                               $wgOut->addHTML( self::getHTML( $e ) );
+                       // 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 {
@@ -191,12 +145,7 @@ class MWExceptionRenderer {
                                '<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 self::getHTML( $e );
 
                        echo "</body></html>\n";
                }
@@ -220,14 +169,15 @@ class MWExceptionRenderer {
                } else {
                        $logId = WebRequest::getRequestId();
                        $html = "<div class=\"errorbox mw-content-ltr\">" .
-                               '[' . $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" .
+                               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()
+                               ) ) . "</div>\n" .
                                "<!-- " . wordwrap( self::getShowBacktraceError( $e ), 50 ) . " -->";
                }
 
@@ -262,7 +212,7 @@ class MWExceptionRenderer {
                                "\nBacktrace:\n" .
                                MWExceptionHandler::getRedactedTraceAsString( $e ) . "\n";
                } else {
-                       return self::getShowBacktraceError( $e );
+                       return self::getShowBacktraceError( $e ) . "\n";
                }
        }
 
@@ -293,7 +243,7 @@ class MWExceptionRenderer {
                        $vars[] = '$wgShowDBErrorBacktrace = true;';
                }
                $vars = implode( ' and ', $vars );
-               return "Set $vars at the bottom of LocalSettings.php to show detailed debugging information";
+               return "Set $vars at the bottom of LocalSettings.php to show detailed debugging information.";
        }
 
        /**
@@ -313,7 +263,7 @@ class MWExceptionRenderer {
        }
 
        /**
-        * @param integer $code
+        * @param int $code
         */
        private static function statusHeader( $code ) {
                if ( !headers_sent() ) {
@@ -356,7 +306,7 @@ class MWExceptionRenderer {
                if ( $wgShowHostnames || $wgShowSQLErrors ) {
                        $info = str_replace(
                                '$1',
-                               Html::element( 'span', [ 'dir' => 'ltr' ], htmlspecialchars( $e->getMessage() ) ),
+                               Html::element( 'span', [ 'dir' => 'ltr' ], $e->getMessage() ),
                                htmlspecialchars( self::msg( 'dberr-info', '($1)' ) )
                        );
                } else {