From 61dd1d037b5eb5ac54ac03e29ba94d4440477063 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Mon, 27 Aug 2012 22:57:15 +0200 Subject: [PATCH] Use the MWDebug class to display debug log back in the request. - This removes the duplication of MWDebug::$debug and OutputPage::$mDebugtext, so there's no need to store two times the same text - Removed OutputPage::debug() since it's no longer used - Had to keep OutputPage::$mDebugtext because it's still referenced by SemanticPageMaker extension - Moved Skin::formatDebugHTML() to MWDebug::GetHTMLDebugLog() and replaced the call in Skin::generateDebugHTML() to use it - Also check $logonly before sending entries to the debug toolbar in wfDebug(), for consistency - Changed MWDebug::getDebugHTML() to also return the debug log in an HTML comment if $wgDebugComments is set to true and changed the location of this call to BaseTemplate::printTrail() so that its result is the latest possible. This also includes the debug toolbar. - Removed MockOutputPage and related test cases since they are no longer accurate Change-Id: Ie0f389f8566457b1c938c627ed930040741ac9d9 --- includes/GlobalFunctions.php | 18 +--- includes/OutputPage.php | 17 +--- includes/Skin.php | 66 +------------ includes/SkinTemplate.php | 16 +-- includes/debug/Debug.php | 99 ++++++++++++++++--- tests/TestsAutoLoader.php | 1 - .../includes/GlobalFunctions/GlobalTest.php | 30 +----- tests/phpunit/includes/MockOutputPage.php | 10 -- 8 files changed, 95 insertions(+), 162 deletions(-) delete mode 100644 tests/phpunit/includes/MockOutputPage.php diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 35c7bbb676..3e38435ffb 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -847,10 +847,7 @@ function wfMatchesDomainList( $url, $domains ) { * @param $logonly Bool: set true to avoid appearing in HTML when $wgDebugComments is set */ function wfDebug( $text, $logonly = false ) { - global $wgOut, $wgDebugLogFile, $wgDebugComments, $wgProfileOnly, $wgDebugRawPage; - global $wgDebugLogPrefix, $wgShowDebug; - - static $cache = array(); // Cache of unoutputted messages + global $wgDebugLogFile, $wgProfileOnly, $wgDebugRawPage, $wgDebugLogPrefix; if ( !$wgDebugRawPage && wfIsDebugRawPage() ) { return; @@ -861,15 +858,10 @@ function wfDebug( $text, $logonly = false ) { $text = preg_replace( '/[^\n]/', $timer . '\0', $text, 1 ); } - if ( ( $wgDebugComments || $wgShowDebug ) && !$logonly ) { - $cache[] = $text; - - if ( isset( $wgOut ) && is_object( $wgOut ) ) { - // add the message and any cached messages to the output - array_map( array( $wgOut, 'debug' ), $cache ); - $cache = array(); - } + if ( !$logonly ) { + MWDebug::debugMsg( $text ); } + if ( wfRunHooks( 'Debug', array( $text, null /* no log group */ ) ) ) { if ( $wgDebugLogFile != '' && !$wgProfileOnly ) { # Strip unprintables; they can switch terminal modes when binary data @@ -879,8 +871,6 @@ function wfDebug( $text, $logonly = false ) { wfErrorLog( $text, $wgDebugLogFile ); } } - - MWDebug::debugMsg( $text ); } /** diff --git a/includes/OutputPage.php b/includes/OutputPage.php index e9e2b6f3b2..bc9eaa9a0c 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -56,9 +56,9 @@ class OutputPage extends ContextSource { /** * Holds the debug lines that will be output as comments in page source if * $wgDebugComments is enabled. See also $wgShowDebug. - * TODO: make a getter method for this + * @deprecated since 1.20; use MWDebug class instead. */ - public $mDebugtext = ''; // TODO: we might want to replace it by wfDebug() wfDebugLog() + public $mDebugtext = ''; /// Should be private. Stores contents of "" tag var $mHTMLtitle = ''; @@ -1314,15 +1314,6 @@ class OutputPage extends ContextSource { return $this->mBodytext; } - /** - * Add $text to the debug output - * - * @param $text String: debug text - */ - public function debug( $text ) { - $this->mDebugtext .= $text; - } - /** * Get/set the ParserOptions object to use for wikitext parsing * @@ -2032,10 +2023,6 @@ class OutputPage extends ContextSource { * based on $pageTitle */ public function prepareErrorPage( $pageTitle, $htmlTitle = false ) { - if ( $this->getTitle() ) { - $this->mDebugtext .= 'Original title: ' . $this->getTitle()->getPrefixedText() . "\n"; - } - $this->setPageTitle( $pageTitle ); if ( $htmlTitle !== false ) { $this->setHTMLTitle( $htmlTitle ); diff --git a/includes/Skin.php b/includes/Skin.php index 5af6aec660..c13fed5c7a 100644 --- a/includes/Skin.php +++ b/includes/Skin.php @@ -568,71 +568,7 @@ abstract class Skin extends ContextSource { * @return String HTML containing debug data, if enabled (otherwise empty). */ protected function generateDebugHTML() { - global $wgShowDebug; - - $html = MWDebug::getDebugHTML( $this->getContext() ); - - if ( $wgShowDebug ) { - $listInternals = $this->formatDebugHTML( $this->getOutput()->mDebugtext ); - $html .= "\n<hr />\n<strong>Debug data:</strong><ul id=\"mw-debug-html\">" . - $listInternals . "</ul>\n"; - } - - return $html; - } - - /** - * @param $debugText string - * @return string - */ - private function formatDebugHTML( $debugText ) { - global $wgDebugTimestamps; - - $lines = explode( "\n", $debugText ); - $curIdent = 0; - $ret = '<li>'; - - foreach ( $lines as $line ) { - $pre = ''; - if ( $wgDebugTimestamps ) { - $matches = array(); - if ( preg_match( '/^(\d+\.\d+ {1,3}\d+.\dM\s{2})/', $line, $matches ) ) { - $pre = $matches[1]; - $line = substr( $line, strlen( $pre ) ); - } - } - $display = ltrim( $line ); - $ident = strlen( $line ) - strlen( $display ); - $diff = $ident - $curIdent; - - $display = $pre . $display; - if ( $display == '' ) { - $display = "\xc2\xa0"; - } - - if ( !$ident && $diff < 0 && substr( $display, 0, 9 ) != 'Entering ' && substr( $display, 0, 8 ) != 'Exiting ' ) { - $ident = $curIdent; - $diff = 0; - $display = '<span style="background:yellow;">' . htmlspecialchars( $display ) . '</span>'; - } else { - $display = htmlspecialchars( $display ); - } - - if ( $diff < 0 ) { - $ret .= str_repeat( "</li></ul>\n", -$diff ) . "</li><li>\n"; - } elseif ( $diff == 0 ) { - $ret .= "</li><li>\n"; - } else { - $ret .= str_repeat( "<ul><li>\n", $diff ); - } - $ret .= "<tt>$display</tt>\n"; - - $curIdent = $ident; - } - - $ret .= str_repeat( '</li></ul>', $curIdent ) . '</li>'; - - return $ret; + return MWDebug::getHTMLDebugLog(); } /** diff --git a/includes/SkinTemplate.php b/includes/SkinTemplate.php index 2c869db238..d3502e92ff 100644 --- a/includes/SkinTemplate.php +++ b/includes/SkinTemplate.php @@ -135,7 +135,6 @@ class SkinTemplate extends Skin { global $wgDisableCounters, $wgSitename, $wgLogo, $wgHideInterlanguageLinks; global $wgMaxCredits, $wgShowCreditsIfMax; global $wgPageShowWatchingUsers; - global $wgDebugComments; global $wgArticlePath, $wgScriptPath, $wgServer; wfProfileIn( __METHOD__ ); @@ -387,12 +386,6 @@ class SkinTemplate extends Skin { } } - if ( $wgDebugComments ) { - $tpl->setRef( 'debug', $out->mDebugtext ); - } else { - $tpl->set( 'debug', '' ); - } - $tpl->set( 'sitenotice', $this->getSiteNotice() ); $tpl->set( 'bottomscripts', $this->bottomScripts() ); $tpl->set( 'printfooter', $this->printSource() ); @@ -468,6 +461,7 @@ class SkinTemplate extends Skin { $tpl->set( 'headscripts', $out->getHeadScripts() . $out->getHeadItems() ); } + $tpl->set( 'debug', '' ); $tpl->set( 'debughtml', $this->generateDebugHTML() ); $tpl->set( 'reporttime', wfReportTime() ); @@ -1886,13 +1880,7 @@ abstract class BaseTemplate extends QuickTemplate { function printTrail() { ?> <?php $this->html( 'bottomscripts' ); /* JS call to runBodyOnloadHook */ ?> <?php $this->html( 'reporttime' ) ?> -<?php if ( $this->data['debug'] ): ?> -<!-- Debug output: -<?php $this->text( 'debug' ); ?> - ---> -<?php endif; +<?php echo MWDebug::getDebugHTML( $this->getSkin()->getContext() ); } } - diff --git a/includes/debug/Debug.php b/includes/debug/Debug.php index 1ad25aece2..0b623e621d 100644 --- a/includes/debug/Debug.php +++ b/includes/debug/Debug.php @@ -207,11 +207,11 @@ class MWDebug { * @param $str string */ public static function debugMsg( $str ) { - if ( !self::$enabled ) { - return; - } + global $wgDebugComments, $wgShowDebug; - self::$debug[] = trim( $str ); + if ( self::$enabled || $wgDebugComments || $wgShowDebug ) { + self::$debug[] = rtrim( $str ); + } } /** @@ -283,22 +283,91 @@ class MWDebug { * @return string */ public static function getDebugHTML( IContextSource $context ) { - if ( !self::$enabled ) { + global $wgDebugComments; + + $html = ''; + + if ( self::$enabled ) { + MWDebug::log( 'MWDebug output complete' ); + $debugInfo = self::getDebugInfo( $context ); + + // Cannot use OutputPage::addJsConfigVars because those are already outputted + // by the time this method is called. + $html = Html::inlineScript( + ResourceLoader::makeLoaderConditionalScript( + ResourceLoader::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) ) + ) + ); + } + + if ( $wgDebugComments ) { + $html .= "<!-- Debug output:\n" . + htmlspecialchars( implode( "\n", self::$debug ) ) . + "\n\n-->"; + } + + return $html; + } + + /** + * Generate debug log in HTML for displaying at the bottom of the main + * content area. + * If $wgShowDebug is false, an empty string is always returned. + * + * @since 1.20 + * @return string HTML fragment + */ + public static function getHTMLDebugLog() { + global $wgDebugTimestamps, $wgShowDebug; + + if ( !$wgShowDebug ) { return ''; } - MWDebug::log( 'MWDebug output complete' ); - $debugInfo = self::getDebugInfo( $context ); + $curIdent = 0; + $ret = "\n<hr />\n<strong>Debug data:</strong><ul id=\"mw-debug-html\">\n<li>"; - // Cannot use OutputPage::addJsConfigVars because those are already outputted - // by the time this method is called. - $html = Html::inlineScript( - ResourceLoader::makeLoaderConditionalScript( - ResourceLoader::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) ) - ) - ); + foreach ( self::$debug as $line ) { + $pre = ''; + if ( $wgDebugTimestamps ) { + $matches = array(); + if ( preg_match( '/^(\d+\.\d+ {1,3}\d+.\dM\s{2})/', $line, $matches ) ) { + $pre = $matches[1]; + $line = substr( $line, strlen( $pre ) ); + } + } + $display = ltrim( $line ); + $ident = strlen( $line ) - strlen( $display ); + $diff = $ident - $curIdent; - return $html; + $display = $pre . $display; + if ( $display == '' ) { + $display = "\xc2\xa0"; + } + + if ( !$ident && $diff < 0 && substr( $display, 0, 9 ) != 'Entering ' && substr( $display, 0, 8 ) != 'Exiting ' ) { + $ident = $curIdent; + $diff = 0; + $display = '<span style="background:yellow;">' . nl2br( htmlspecialchars( $display ) ) . '</span>'; + } else { + $display = nl2br( htmlspecialchars( $display ) ); + } + + if ( $diff < 0 ) { + $ret .= str_repeat( "</li></ul>\n", -$diff ) . "</li><li>\n"; + } elseif ( $diff == 0 ) { + $ret .= "</li><li>\n"; + } else { + $ret .= str_repeat( "<ul><li>\n", $diff ); + } + $ret .= "<tt>$display</tt>\n"; + + $curIdent = $ident; + } + + $ret .= str_repeat( '</li></ul>', $curIdent ) . "</li>\n</ul>\n"; + + return $ret; } /** diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index e853b13cea..cb5ca36f13 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -13,7 +13,6 @@ $wgAutoloadClasses += array( //includes 'BlockTest' => "$testFolder/phpunit/includes/BlockTest.php", - 'MockOutputPage' => "$testFolder/phpunit/includes/MockOutputPage.php", //API 'ApiFormatTestBase' => "$testFolder/phpunit/includes/api/format/ApiFormatTestBase.php", diff --git a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php index 746add5b52..129f271bf0 100644 --- a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php +++ b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php @@ -311,7 +311,7 @@ class GlobalTest extends MediaWikiTestCase { function testDebugFunctionTest() { - global $wgDebugLogFile, $wgOut, $wgShowDebug, $wgDebugTimestamps; + global $wgDebugLogFile, $wgDebugTimestamps; $old_log_file = $wgDebugLogFile; $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' ); @@ -333,33 +333,7 @@ class GlobalTest extends MediaWikiTestCase { wfDebug( "\00305This has böth UTF and control chars\003" ); $this->assertEquals( " 05This has böth UTF and control chars ", file_get_contents( $wgDebugLogFile ) ); unlink( $wgDebugLogFile ); - - - - $old_wgOut = $wgOut; - $old_wgShowDebug = $wgShowDebug; - - $wgOut = new MockOutputPage; - - $wgShowDebug = true; - - $message = "\00305This has böth UTF and control chars\003"; - - wfDebug( $message ); - - if( $wgOut->message == "JAJA is a stupid error message. Anyway, here's your message: $message" ) { - $this->assertTrue( true, 'MockOutputPage called, set the proper message.' ); - } - else { - $this->assertTrue( false, 'MockOutputPage was not called.' ); - } - - $wgOut = $old_wgOut; - $wgShowDebug = $old_wgShowDebug; - unlink( $wgDebugLogFile ); - - - + wfDebugMem(); $this->assertGreaterThan( 5000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) ); unlink( $wgDebugLogFile ); diff --git a/tests/phpunit/includes/MockOutputPage.php b/tests/phpunit/includes/MockOutputPage.php deleted file mode 100644 index bdee4832bf..0000000000 --- a/tests/phpunit/includes/MockOutputPage.php +++ /dev/null @@ -1,10 +0,0 @@ -<?php - -class MockOutputPage { - - public $message; - - function debug( $message ) { - $this->message = "JAJA is a stupid error message. Anyway, here's your message: $message"; - } -} -- 2.20.1