Merge "Use the MWDebug class to display debug log back in the request."
authorNikerabbit <niklas.laxstrom@gmail.com>
Tue, 28 Aug 2012 05:49:41 +0000 (05:49 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 28 Aug 2012 05:49:41 +0000 (05:49 +0000)
includes/GlobalFunctions.php
includes/OutputPage.php
includes/Skin.php
includes/SkinTemplate.php
includes/debug/Debug.php
tests/TestsAutoLoader.php
tests/phpunit/includes/GlobalFunctions/GlobalTest.php
tests/phpunit/includes/MockOutputPage.php [deleted file]

index e64d38c..328d429 100644 (file)
@@ -932,10 +932,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;
@@ -946,15 +943,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
@@ -964,8 +956,6 @@ function wfDebug( $text, $logonly = false ) {
                        wfErrorLog( $text, $wgDebugLogFile );
                }
        }
-
-       MWDebug::debugMsg( $text );
 }
 
 /**
index e9e2b6f..bc9eaa9 100644 (file)
@@ -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 "<title>" 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 );
index 0ab46ae..0b0b616 100644 (file)
@@ -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();
        }
 
        /**
index 2c869db..d3502e9 100644 (file)
@@ -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() );
        }
 
 }
-
index 1ad25ae..0b623e6 100644 (file)
@@ -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;
        }
 
        /**
index e853b13..cb5ca36 100644 (file)
@@ -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",
index 94158bf..9097d30 100644 (file)
@@ -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 (file)
index bdee483..0000000
+++ /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";
-       }
-}