addModules( 'mediawiki.debug.init' ); } } /** * Adds a line to the log * * @todo Add support for passing objects * * @since 1.19 * @param $str string */ public static function log( $str ) { if ( !self::$enabled ) { return; } self::$log[] = array( 'msg' => htmlspecialchars( $str ), 'type' => 'log', 'caller' => wfGetCaller(), ); } /** * Returns internal log array * @since 1.19 * @return array */ public static function getLog() { return self::$log; } /** * Clears internal log array and deprecation tracking * @since 1.19 */ public static function clearLog() { self::$log = array(); self::$deprecationWarnings = array(); } /** * Adds a warning entry to the log * * @since 1.19 * @param $msg string * @param $callerOffset int * @return mixed */ public static function warning( $msg, $callerOffset = 1, $level = E_USER_NOTICE ) { $callerDescription = self::getCallerDescription( $callerOffset ); self::sendWarning( $msg, $callerDescription, $level ); if ( self::$enabled ) { self::$log[] = array( 'msg' => htmlspecialchars( $msg ), 'type' => 'warn', 'caller' => $callerDescription['func'], ); } } /** * Show a warning that $function is deprecated. * This will send it to the following locations: * - Debug toolbar, with one item per function and caller, if $wgDebugToolbar * is set to true. * - PHP's error log, with level E_USER_DEPRECATED, if $wgDevelopmentWarnings * is set to true. * - MediaWiki's debug log, if $wgDevelopmentWarnings is set to false. * * @since 1.19 * @param $function string: Function that is deprecated. * @param $version string|bool: Version in which the function was deprecated. * @param $component string|bool: Component to which the function belongs. * If false, it is assumbed the function is in MediaWiki core. * @param $callerOffset integer: How far up the callstack is the original * caller. 2 = function that called the function that called * MWDebug::deprecated() (Added in 1.20). * @return mixed */ public static function deprecated( $function, $version = false, $component = false, $callerOffset = 2 ) { $callerDescription = self::getCallerDescription( $callerOffset ); $callerFunc = $callerDescription['func']; $sendToLog = true; // Check to see if there already was a warning about this function if ( isset( self::$deprecationWarnings[$function][$callerFunc] ) ) { return; } elseif ( isset( self::$deprecationWarnings[$function] ) ) { if ( self::$enabled ) { $sendToLog = false; } else { return; } } self::$deprecationWarnings[$function][$callerFunc] = true; if ( $version ) { global $wgDeprecationReleaseLimit; if ( $wgDeprecationReleaseLimit && $component === false ) { # Strip -* off the end of $version so that branches can use the # format #.##-branchname to avoid issues if the branch is merged into # a version of MediaWiki later than what it was branched from $comparableVersion = preg_replace( '/-.*$/', '', $version ); # If the comparableVersion is larger than our release limit then # skip the warning message for the deprecation if ( version_compare( $wgDeprecationReleaseLimit, $comparableVersion, '<' ) ) { $sendToLog = false; } } $component = $component === false ? 'MediaWiki' : $component; $msg = "Use of $function was deprecated in $component $version."; } else { $msg = "Use of $function is deprecated."; } if ( $sendToLog ) { self::sendWarning( $msg, $callerDescription, E_USER_DEPRECATED ); } if ( self::$enabled ) { $logMsg = htmlspecialchars( $msg ) . Html::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ), Html::element( 'span', array(), 'Backtrace:' ) . wfBacktrace() ); self::$log[] = array( 'msg' => $logMsg, 'type' => 'deprecated', 'caller' => $callerFunc, ); } } /** * Get an array describing the calling function at a specified offset. * * @param $callerOffset integer: How far up the callstack is the original * caller. 0 = function that called getCallerDescription() * @return array with two keys: 'file' and 'func' */ private static function getCallerDescription( $callerOffset ) { $callers = wfDebugBacktrace(); if ( isset( $callers[$callerOffset] ) ) { $callerfile = $callers[$callerOffset]; if ( isset( $callerfile['file'] ) && isset( $callerfile['line'] ) ) { $file = $callerfile['file'] . ' at line ' . $callerfile['line']; } else { $file = '(internal function)'; } } else { $file = '(unknown location)'; } if ( isset( $callers[$callerOffset + 1] ) ) { $callerfunc = $callers[$callerOffset + 1]; $func = ''; if ( isset( $callerfunc['class'] ) ) { $func .= $callerfunc['class'] . '::'; } if ( isset( $callerfunc['function'] ) ) { $func .= $callerfunc['function']; } } else { $func = 'unknown'; } return array( 'file' => $file, 'func' => $func ); } /** * Send a warning either to the debug log or by triggering an user PHP * error depending on $wgDevelopmentWarnings. * * @param $msg string Message to send * @param $caller array caller description get from getCallerDescription() * @param $level error level to use if $wgDevelopmentWarnings is true */ private static function sendWarning( $msg, $caller, $level ) { global $wgDevelopmentWarnings; $msg .= ' [Called from ' . $caller['func'] . ' in ' . $caller['file'] . ']'; if ( $wgDevelopmentWarnings ) { trigger_error( $msg, $level ); } else { wfDebug( "$msg\n" ); } } /** * This is a method to pass messages from wfDebug to the pretty debugger. * Do NOT use this method, use MWDebug::log or wfDebug() * * @since 1.19 * @param $str string */ public static function debugMsg( $str ) { global $wgDebugComments, $wgShowDebug; if ( self::$enabled || $wgDebugComments || $wgShowDebug ) { self::$debug[] = rtrim( $str ); } } /** * Begins profiling on a database query * * @since 1.19 * @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. * * @since 1.19 * @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'] ); } /** * Returns a list of files included, along with their size * * @param $context IContextSource * @return array */ protected static function getFilesIncluded( IContextSource $context ) { $files = get_included_files(); $fileList = array(); foreach ( $files as $file ) { $size = filesize( $file ); $fileList[] = array( 'name' => $file, 'size' => $context->getLanguage()->formatSize( $size ), ); } return $fileList; } /** * Returns the HTML to add to the page for the toolbar * * @since 1.19 * @param $context IContextSource * @return string */ public static function getDebugHTML( IContextSource $context ) { 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 .= ""; } 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 ''; } $curIdent = 0; $ret = "\n