From: John Du Hart Date: Sun, 4 Dec 2011 18:35:40 +0000 (+0000) Subject: svn:eol-style native on all phase3 stuff, since I messed that up in r105122 X-Git-Tag: 1.31.0-rc.0~26184 X-Git-Url: http://git.cyclocoop.org/%28?a=commitdiff_plain;h=33df2669584bf358923bac8f8f6deb9e4cb00750;p=lhc%2Fweb%2Fwiklou.git svn:eol-style native on all phase3 stuff, since I messed that up in r105122 Also adding the MWDebug class I missed there as well --- diff --git a/includes/debug/Debug.php b/includes/debug/Debug.php new file mode 100644 index 0000000000..181b81d404 --- /dev/null +++ b/includes/debug/Debug.php @@ -0,0 +1,216 @@ +getOutput()->addModules( 'mediawiki.debug' ); + } + + /** + * Adds a line to the log + * + * This does nothing atm, there's not frontend for it + * + * @todo Add error and warning log type + * @todo Add support for passing objects + * + * @param $str string + */ + public static function log( $str ) { + if ( !self::$enabled ) { + return; + } + + self::$log[] = $str; + } + + /** + * This is a method to pass messages from wfDebug to the pretty debugger. + * Do NOT use this method, use MWDebug::log or wfDebug() + * + * @param $str string + */ + public static function debugMsg( $str ) { + if ( !self::$enabled ) { + return; + } + + self::$debug[] = trim( $str ); + } + + /** + * Begins profiling on a database query + * + * @param $sql string + * @param $function string + * @param $isMaster bool + * @return int ID number of the query to pass to queryTime or -1 if the + * debugger is disabled + */ + public static function query( $sql, $function, $isMaster ) { + if ( !self::$enabled ) { + return -1; + } + + self::$query[] = array( + 'sql' => $sql, + 'function' => $function, + 'master' => (bool) $isMaster, + 'time' > 0.0, + '_start' => microtime( true ), + ); + + return count( self::$query ) - 1; + } + + /** + * Calculates how long a query took. + * + * @param $id int + */ + public static function queryTime( $id ) { + if ( $id === -1 || !self::$enabled ) { + return; + } + + self::$query[$id]['time'] = microtime( true ) - self::$query[$id]['_start']; + unset( self::$query[$id]['_start'] ); + } + + /** + * Processes a WebRequest object + * + * @param $request WebRequest + */ + public static function processRequest( WebRequest $request ) { + if ( !self::$enabled ) { + return; + } + + self::$request = array( + 'method' => $_SERVER['REQUEST_METHOD'], + 'url' => $request->getRequestURL(), + 'headers' => $request->getAllHeaders(), + 'params' => $request->getValues(), + ); + } + + /** + * Returns a list of files included, along with their size + * + * @return array + */ + protected static function getFilesIncluded() { + $files = get_included_files(); + $fileList = array(); + foreach ( $files as $file ) { + $size = filesize( $file ); + $fileList[] = array( + 'name' => $file, + 'size' => self::formatBytes( $size ), + ); + } + + return $fileList; + } + + /** + * Returns the HTML to add to the page for the toolbar + * + * @return string + */ + public static function getDebugHTML() { + if ( !self::$enabled ) { + return ''; + } + + global $wgVersion, $wgRequestTime; + $debugInfo = array( + 'mwVersion' => $wgVersion, + 'phpVersion' => PHP_VERSION, + 'time' => microtime( true ) - $wgRequestTime, + 'log' => self::$log, + '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(), + ); + // TODO: Clean this up + $html = Html::openElement( 'script' ); + $html .= 'var debugInfo = ' . Xml::encodeJsVar( $debugInfo ) . ';'; + $html .= " $(function() { mw.loader.using( 'mediawiki.debug', function() { mw.Debug.init( debugInfo ) } ); }); "; + $html .= Html::closeElement( 'script' ); + + 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