From f12c373d1eeb18c502abf46e343d0bda5d87dd2f Mon Sep 17 00:00:00 2001 From: John Du Hart Date: Thu, 15 Dec 2011 02:26:14 +0000 Subject: [PATCH] Followup r105122 & r105123, fixes and improvements per CR Using Language::getSize per Nikerabbit Cleanup HTML generation per brion (yay) --- includes/Skin.php | 2 +- includes/debug/Debug.php | 31 +--- resources/mediawiki/mediawiki.debug.js | 197 +++++++++++++++---------- 3 files changed, 132 insertions(+), 98 deletions(-) diff --git a/includes/Skin.php b/includes/Skin.php index 5f0a1f8fa9..0416112d84 100644 --- a/includes/Skin.php +++ b/includes/Skin.php @@ -531,7 +531,7 @@ abstract class Skin extends ContextSource { protected function generateDebugHTML() { global $wgShowDebug; - $html = MWDebug::getDebugHTML(); + $html = MWDebug::getDebugHTML( $this->getContext() ); if ( $wgShowDebug ) { $listInternals = $this->formatDebugHTML( $this->getOutput()->mDebugtext ); diff --git a/includes/debug/Debug.php b/includes/debug/Debug.php index 181b81d404..baadc70c72 100644 --- a/includes/debug/Debug.php +++ b/includes/debug/Debug.php @@ -3,7 +3,6 @@ /** * New debugger system that outputs a toolbar on page view * - * @todo Clean up HTML generated by the javascript * @todo Profiler support */ class MWDebug { @@ -150,16 +149,17 @@ class MWDebug { /** * Returns a list of files included, along with their size * + * @param $context IContextSource * @return array */ - protected static function getFilesIncluded() { + protected static function getFilesIncluded( IContextSource $context ) { $files = get_included_files(); $fileList = array(); foreach ( $files as $file ) { $size = filesize( $file ); $fileList[] = array( 'name' => $file, - 'size' => self::formatBytes( $size ), + 'size' => $context->getLanguage()->formatSize( $size ), ); } @@ -169,9 +169,10 @@ class MWDebug { /** * Returns the HTML to add to the page for the toolbar * + * @param $context IContextSource * @return string */ - public static function getDebugHTML() { + public static function getDebugHTML( IContextSource $context ) { if ( !self::$enabled ) { return ''; } @@ -185,9 +186,9 @@ class MWDebug { 'debugLog' => self::$debug, 'queries' => self::$query, 'request' => self::$request, - 'memory' => self::formatBytes( memory_get_usage() ), - 'memoryPeak' => self::formatBytes( memory_get_peak_usage() ), - 'includes' => self::getFilesIncluded(), + 'memory' => $context->getLanguage()->formatSize( memory_get_usage() ), + 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ), + 'includes' => self::getFilesIncluded( $context ), ); // TODO: Clean this up $html = Html::openElement( 'script' ); @@ -197,20 +198,4 @@ class MWDebug { return $html; } - - /** - * Formats raw bytes integer into a human readable format - * - * @author John Himmelman - http://stackoverflow.com/a/2510540/343911 - * @param $size int - * @param $precision int - * @return string - */ - protected static function formatBytes( $size, $precision = 2 ) { - $base = log( $size ) / log( 1024 ); - // If we ever use 1TB of RAM we're fucked - $suffixes = array( '', 'kb', 'MB', 'GB', 'TB' ); - - return round( pow( 1024, $base - floor( $base ) ), $precision ) . $suffixes[floor( $base )]; - } } \ No newline at end of file diff --git a/resources/mediawiki/mediawiki.debug.js b/resources/mediawiki/mediawiki.debug.js index 12755e6225..e41ad3be78 100644 --- a/resources/mediawiki/mediawiki.debug.js +++ b/resources/mediawiki/mediawiki.debug.js @@ -72,93 +72,133 @@ * Constructs the HTML for the debugging toolbar */ buildHtml: function() { - this.$container = $( '
' ) + var $container = this.$container = $( '
' ) .attr({ id: 'mw-debug-container', class: 'mw-debug' }); - var html = ''; - - html += '
' - + 'MediaWiki: ' - + this.data.mwVersion + '
'; - - html += '
' - + 'PHP: ' - + this.data.phpVersion + '
'; - - html += '
' - + 'Time: ' + this.data.time.toFixed( 5 ) + 's
'; - html += '
' - + 'Memory: ' + this.data.memory + ' (' - + this.data.memoryPeak + ')
'; - - var queryLink = 'Queries: ' - + this.data.queries.length + ''; - html += '
' - + queryLink + '
'; - - var debugLink = 'Debug Log (' - + this.data.debugLog.length + ' lines)'; - html += '
' - + debugLink + '
'; - - var requestLink = 'Request'; - html += '
' - + requestLink + '
'; - - var filesLink = '' - + this.data.includes.length + ' Files Included'; - html += '
' - + filesLink + '
'; - - html += '
' - + this.buildQueryTable() + '
'; - html += '
' - + this.buildDebugLogTable() + '
'; - html += '
' - + this.buildRequestPane() + '
'; - html += '
' - + this.buildIncludesPane() + '
'; - - this.$container.html( html ); + /** + * Returns a jQuery element for a debug-bit div + * + * @param id + * @return {jQuery} + */ + var bitDiv = function( id ) { + return $( '
' ).attr({ + id: 'mw-debug-' + id, + class: 'mw-debug-bit' + }); + }; + /** + * Returns a jQuery element for a pane link + * + * @param id + * @param text + * @return {jQuery} + */ + var paneLink = function( id, text ) { + return $( '' ) + .attr({ + href: '#', + class: 'mw-debug-panelink', + id: 'mw-debug-' + id + '-link' + }) + .text( text ); + } + + bitDiv( 'mwversion' ) + .append( $( '' ).text( 'MediaWiki' ) ) + .append( ': ' + this.data.mwVersion ) + .appendTo( $container ); + + bitDiv( 'phpversion' ) + .append( $( '' ).text( 'PHP' ) ) + .append( ': ' + this.data.phpVersion ) + .appendTo( $container ); + + bitDiv( 'time' ) + .text( 'Time: ' + this.data.time.toFixed( 5 ) ) + .appendTo( $container ); + bitDiv( 'memory' ) + .text( 'Memory: ' + this.data.memory ) + .append( $( '' ).text( ' (' + this.data.memoryPeak + ')' ) ) + .appendTo( $container ); + + bitDiv( 'querylist' ) + .append( paneLink( 'query', 'Queries: ' + this.data.queries.length ) ) + .appendTo( $container ); + + bitDiv( 'debuglog' ) + .append( paneLink( 'debuglog', 'Debug Log (' + this.data.debugLog.length + ' lines)' ) ) + .appendTo( $container ); + + bitDiv( 'request' ) + .append( paneLink( 'request', 'Request' ) ) + .appendTo( $container ); + + bitDiv( 'includes' ) + .append( paneLink( 'files-includes', this.data.includes.length + ' Files Included' ) ) + .appendTo( $container ); + + var panes = { + 'querylist': this.buildQueryTable(), + 'debuglog': this.buildDebugLogTable(), + 'request': this.buildRequestPane(), + 'includes': this.buildIncludesPane() + }; + + for ( var id in panes ) { + if ( !panes.hasOwnProperty( id ) ) { + continue; + } + + $( '
' ) + .attr({ + class: 'mw-debug-pane', + id: 'mw-debug-pane-' + id + }) + .append( panes[id] ) + .appendTo( $container ); + } }, /** * Query list pane */ buildQueryTable: function() { - var html = ''; + var $table = $( '
' ); for ( var i = 0, length = this.data.queries.length; i < length; i++ ) { var query = this.data.queries[i]; - html += '' + ( i + 1 ) + ''; - - html += '' + query.sql + ''; - html += '(' + query.time.toFixed( 4 ) + 'ms) ' + query.function + ''; - - html += ''; + $( '' ) + .append( $( '' ).text( i + 1 ) ) + .append( $( '' ).text( query.sql ) ) + .append( $( '' ) + .append( $( '' ).text( '(' + query.time.toFixed( 4 ) + 'ms) ' ) ) + .append( query['function'] ) + ) + .appendTo( $table ); } - html += ''; - - return html; + return $table; }, /** * Legacy debug log pane */ buildDebugLogTable: function() { - var html = '
    '; + var $list = $( '
      ' ); for ( var i = 0, length = this.data.debugLog.length; i < length; i++ ) { var line = this.data.debugLog[i]; - html += '
    • ' + line.replace( /\n/g, "
      \n" ) + '
    • '; + $( '
    • ' ) + .html( mw.html.escape( line ).replace( /\n/g, "
      \n" ) ) + .appendTo( $list ); } - return html + '
    '; + return $list; }, /** @@ -166,42 +206,51 @@ */ buildRequestPane: function() { var buildTable = function( title, data ) { - var t = '

    ' + title + '

    ' - + ''; + var $unit = $( '
    ' ) + .append( $( '

    ' ).text( title ) ); + + var $table = $( '

    Key Value
    ' ).appendTo( $unit ); + + $( '' ) + .html( '' ) + .appendTo( $table ); for ( var key in data ) { if ( !data.hasOwnProperty( key ) ) { continue; } - var value = data[key]; - t += ''; + $( '' ) + .append( $( '
    Key Value
    ' + key + '' + value + '
    ' ).text( key ) ) + .append( $( '' ).text( data[key] ) ) + .appendTo( $table ); } - t += '
    '; - return t; + return $unit; }; - var html = this.data.request.method + ' ' + this.data.request.url; - html += buildTable( 'Headers', this.data.request.headers ); - html += buildTable( 'Parameters', this.data.request.params ); - - return html; + var $pane = $( '
    ' ) + .text( this.data.request.method + ' ' + this.data.request.url ) + .append( buildTable( 'Headers', this.data.request.headers ) ) + .append( buildTable( 'Parameters', this.data.request.params ) ); + return $pane; }, /** * Included files pane */ buildIncludesPane: function() { - var html = '
      '; + var $list = $( '
        ' ) for ( var i = 0, l = this.data.includes.length; i < l; i++ ) { var file = this.data.includes[i]; - html += '
      • ' + file.size + ' ' + file.name + '
      • '; + $( '
      • ' ) + .text( file.name ) + .prepend( $( '' ).text( file.size ) ) + .appendTo( $list ); } - html += '
      '; - return html; + return $list; } }; -- 2.20.1