2011-12-04 18:35:40 +00:00
|
|
|
<?php
|
2012-04-28 18:41:55 +00:00
|
|
|
/**
|
2013-05-20 09:16:27 +00:00
|
|
|
* Debug toolbar related code.
|
2012-04-28 18:41:55 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2011-12-04 18:35:40 +00:00
|
|
|
|
|
|
|
|
/**
|
2013-05-20 09:16:27 +00:00
|
|
|
* New debugger system that outputs a toolbar on page view.
|
2011-12-04 18:35:40 +00:00
|
|
|
*
|
2012-01-16 13:44:46 +00:00
|
|
|
* By default, most methods do nothing ( self::$enabled = false ). You have
|
|
|
|
|
* to explicitly call MWDebug::init() to enabled them.
|
|
|
|
|
*
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2011-12-04 18:35:40 +00:00
|
|
|
*/
|
|
|
|
|
class MWDebug {
|
|
|
|
|
/**
|
|
|
|
|
* Log lines
|
|
|
|
|
*
|
2013-05-20 09:16:27 +00:00
|
|
|
* @var array $log
|
2011-12-04 18:35:40 +00:00
|
|
|
*/
|
|
|
|
|
protected static $log = array();
|
|
|
|
|
|
|
|
|
|
/**
|
2013-05-20 09:16:27 +00:00
|
|
|
* Debug messages from wfDebug().
|
2011-12-04 18:35:40 +00:00
|
|
|
*
|
2013-05-20 09:16:27 +00:00
|
|
|
* @var array $debug
|
2011-12-04 18:35:40 +00:00
|
|
|
*/
|
|
|
|
|
protected static $debug = array();
|
|
|
|
|
|
|
|
|
|
/**
|
2014-10-29 17:12:45 +00:00
|
|
|
* SQL statements of the database queries.
|
2011-12-04 18:35:40 +00:00
|
|
|
*
|
2013-05-20 09:16:27 +00:00
|
|
|
* @var array $query
|
2011-12-04 18:35:40 +00:00
|
|
|
*/
|
|
|
|
|
protected static $query = array();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is the debugger enabled?
|
|
|
|
|
*
|
2013-05-20 09:16:27 +00:00
|
|
|
* @var bool $enabled
|
2011-12-04 18:35:40 +00:00
|
|
|
*/
|
2012-01-16 13:44:46 +00:00
|
|
|
protected static $enabled = false;
|
2011-12-04 18:35:40 +00:00
|
|
|
|
2012-01-03 05:56:36 +00:00
|
|
|
/**
|
|
|
|
|
* Array of functions that have already been warned, formatted
|
|
|
|
|
* function-caller to prevent a buttload of warnings
|
|
|
|
|
*
|
2013-05-20 09:16:27 +00:00
|
|
|
* @var array $deprecationWarnings
|
2012-01-03 05:56:36 +00:00
|
|
|
*/
|
|
|
|
|
protected static $deprecationWarnings = array();
|
|
|
|
|
|
2011-12-04 18:35:40 +00:00
|
|
|
/**
|
2012-01-16 13:44:46 +00:00
|
|
|
* Enabled the debugger and load resource module.
|
|
|
|
|
* This is called by Setup.php when $wgDebugToolbar is true.
|
2012-05-16 16:25:30 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.19
|
2011-12-04 18:35:40 +00:00
|
|
|
*/
|
|
|
|
|
public static function init() {
|
2012-01-16 13:44:46 +00:00
|
|
|
self::$enabled = true;
|
2012-02-06 17:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add ResourceLoader modules to the OutputPage object if debugging is
|
|
|
|
|
* enabled.
|
|
|
|
|
*
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param OutputPage $out
|
2012-02-06 17:34:33 +00:00
|
|
|
*/
|
|
|
|
|
public static function addModules( OutputPage $out ) {
|
|
|
|
|
if ( self::$enabled ) {
|
2012-02-13 15:25:08 +00:00
|
|
|
$out->addModules( 'mediawiki.debug.init' );
|
2012-02-06 17:34:33 +00:00
|
|
|
}
|
2011-12-04 18:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a line to the log
|
|
|
|
|
*
|
|
|
|
|
* @todo Add support for passing objects
|
|
|
|
|
*
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param string $str
|
2011-12-04 18:35:40 +00:00
|
|
|
*/
|
|
|
|
|
public static function log( $str ) {
|
|
|
|
|
if ( !self::$enabled ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-03 05:56:36 +00:00
|
|
|
self::$log[] = array(
|
|
|
|
|
'msg' => htmlspecialchars( $str ),
|
|
|
|
|
'type' => 'log',
|
|
|
|
|
'caller' => wfGetCaller(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-13 23:07:52 +00:00
|
|
|
/**
|
|
|
|
|
* Returns internal log array
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return array
|
2012-01-13 23:07:52 +00:00
|
|
|
*/
|
|
|
|
|
public static function getLog() {
|
|
|
|
|
return self::$log;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clears internal log array and deprecation tracking
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2012-01-13 23:07:52 +00:00
|
|
|
*/
|
|
|
|
|
public static function clearLog() {
|
|
|
|
|
self::$log = array();
|
|
|
|
|
self::$deprecationWarnings = array();
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-03 05:56:36 +00:00
|
|
|
/**
|
|
|
|
|
* Adds a warning entry to the log
|
|
|
|
|
*
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param string $msg
|
|
|
|
|
* @param int $callerOffset
|
|
|
|
|
* @param int $level A PHP error level. See sendMessage()
|
|
|
|
|
* @param string $log 'production' will always trigger a php error, 'auto'
|
|
|
|
|
* will trigger an error if $wgDevelopmentWarnings is true, and 'debug'
|
|
|
|
|
* will only write to the debug log(s).
|
2013-03-18 09:32:14 +00:00
|
|
|
*
|
2012-01-03 05:56:36 +00:00
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2013-03-18 09:32:14 +00:00
|
|
|
public static function warning( $msg, $callerOffset = 1, $level = E_USER_NOTICE, $log = 'auto' ) {
|
|
|
|
|
global $wgDevelopmentWarnings;
|
|
|
|
|
|
|
|
|
|
if ( $log === 'auto' && !$wgDevelopmentWarnings ) {
|
|
|
|
|
$log = 'debug';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $log === 'debug' ) {
|
|
|
|
|
$level = false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-25 11:09:46 +00:00
|
|
|
$callerDescription = self::getCallerDescription( $callerOffset );
|
2012-01-03 05:56:36 +00:00
|
|
|
|
2013-11-14 11:17:25 +00:00
|
|
|
self::sendMessage( $msg, $callerDescription, 'warning', $level );
|
2012-01-03 05:56:36 +00:00
|
|
|
|
2012-08-25 11:09:46 +00:00
|
|
|
if ( self::$enabled ) {
|
|
|
|
|
self::$log[] = array(
|
|
|
|
|
'msg' => htmlspecialchars( $msg ),
|
|
|
|
|
'type' => 'warn',
|
|
|
|
|
'caller' => $callerDescription['func'],
|
|
|
|
|
);
|
|
|
|
|
}
|
2012-01-03 05:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-08-25 11:09:46 +00:00
|
|
|
* 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.
|
2012-01-03 05:56:36 +00:00
|
|
|
*
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $function Function that is deprecated.
|
|
|
|
|
* @param string|bool $version Version in which the function was deprecated.
|
|
|
|
|
* @param string|bool $component Component to which the function belongs.
|
2014-04-19 20:22:20 +00:00
|
|
|
* If false, it is assumbed the function is in MediaWiki core.
|
|
|
|
|
* @param int $callerOffset How far up the callstack is the original
|
2012-08-25 11:09:46 +00:00
|
|
|
* caller. 2 = function that called the function that called
|
|
|
|
|
* MWDebug::deprecated() (Added in 1.20).
|
2012-01-03 05:56:36 +00:00
|
|
|
*/
|
2013-11-20 14:03:00 +00:00
|
|
|
public static function deprecated( $function, $version = false,
|
|
|
|
|
$component = false, $callerOffset = 2
|
|
|
|
|
) {
|
2012-08-25 11:09:46 +00:00
|
|
|
$callerDescription = self::getCallerDescription( $callerOffset );
|
|
|
|
|
$callerFunc = $callerDescription['func'];
|
2012-01-03 05:56:36 +00:00
|
|
|
|
2012-08-25 11:09:46 +00:00
|
|
|
$sendToLog = true;
|
2012-01-03 05:56:36 +00:00
|
|
|
|
|
|
|
|
// Check to see if there already was a warning about this function
|
2012-08-25 11:09:46 +00:00
|
|
|
if ( isset( self::$deprecationWarnings[$function][$callerFunc] ) ) {
|
2012-01-03 05:56:36 +00:00
|
|
|
return;
|
2012-08-25 11:09:46 +00:00
|
|
|
} elseif ( isset( self::$deprecationWarnings[$function] ) ) {
|
|
|
|
|
if ( self::$enabled ) {
|
|
|
|
|
$sendToLog = false;
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-01-03 05:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-25 11:09:46 +00:00
|
|
|
self::$deprecationWarnings[$function][$callerFunc] = true;
|
2012-01-03 05:56:36 +00:00
|
|
|
|
2012-08-25 11:09:46 +00:00
|
|
|
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 ) {
|
2013-03-18 09:32:14 +00:00
|
|
|
global $wgDevelopmentWarnings; // we could have a more specific $wgDeprecationWarnings setting.
|
2013-11-14 11:17:25 +00:00
|
|
|
self::sendMessage(
|
2013-11-20 14:03:00 +00:00
|
|
|
$msg,
|
|
|
|
|
$callerDescription,
|
2013-11-14 11:17:25 +00:00
|
|
|
'deprecated',
|
2013-11-20 14:03:00 +00:00
|
|
|
$wgDevelopmentWarnings ? E_USER_DEPRECATED : false
|
|
|
|
|
);
|
2012-08-25 11:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param int $callerOffset How far up the callstack is the original
|
2012-08-25 11:09:46 +00:00
|
|
|
* caller. 0 = function that called getCallerDescription()
|
2014-04-19 20:22:20 +00:00
|
|
|
* @return array Array with two keys: 'file' and 'func'
|
2012-08-25 11:09:46 +00:00
|
|
|
*/
|
|
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-11-14 11:17:25 +00:00
|
|
|
* Send a message to the debug log and optionally also trigger a PHP
|
2013-03-18 09:32:14 +00:00
|
|
|
* error, depending on the $level argument.
|
2012-08-25 11:09:46 +00:00
|
|
|
*
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param string $msg Message to send
|
|
|
|
|
* @param array $caller Caller description get from getCallerDescription()
|
|
|
|
|
* @param string $group Log group on which to send the message
|
|
|
|
|
* @param int|bool $level Error level to use; set to false to not trigger an error
|
2012-08-25 11:09:46 +00:00
|
|
|
*/
|
2013-11-14 11:17:25 +00:00
|
|
|
private static function sendMessage( $msg, $caller, $group, $level ) {
|
2012-08-25 11:09:46 +00:00
|
|
|
$msg .= ' [Called from ' . $caller['func'] . ' in ' . $caller['file'] . ']';
|
|
|
|
|
|
2013-03-18 09:32:14 +00:00
|
|
|
if ( $level !== false ) {
|
2012-08-25 11:09:46 +00:00
|
|
|
trigger_error( $msg, $level );
|
|
|
|
|
}
|
2013-03-18 09:32:14 +00:00
|
|
|
|
2013-11-14 11:17:25 +00:00
|
|
|
wfDebugLog( $group, $msg, 'log' );
|
2011-12-04 18:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is a method to pass messages from wfDebug to the pretty debugger.
|
|
|
|
|
* Do NOT use this method, use MWDebug::log or wfDebug()
|
|
|
|
|
*
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param string $str
|
2015-03-18 22:42:23 +00:00
|
|
|
* @param array $context
|
2011-12-04 18:35:40 +00:00
|
|
|
*/
|
2015-03-18 22:42:23 +00:00
|
|
|
public static function debugMsg( $str, $context = array() ) {
|
2012-08-27 20:57:15 +00:00
|
|
|
global $wgDebugComments, $wgShowDebug;
|
2011-12-04 18:35:40 +00:00
|
|
|
|
2012-08-27 20:57:15 +00:00
|
|
|
if ( self::$enabled || $wgDebugComments || $wgShowDebug ) {
|
2015-03-18 22:42:23 +00:00
|
|
|
if ( $context ) {
|
|
|
|
|
$prefix = '';
|
|
|
|
|
if ( isset( $context['prefix'] ) ) {
|
|
|
|
|
$prefix = $context['prefix'];
|
|
|
|
|
} elseif ( isset( $context['channel'] ) && $context['channel'] !== 'wfDebug' ) {
|
|
|
|
|
$prefix = "[{$context['channel']}] ";
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $context['seconds_elapsed'] ) && isset( $context['memory_used'] ) ) {
|
|
|
|
|
$prefix .= "{$context['seconds_elapsed']} {$context['memory_used']} ";
|
|
|
|
|
}
|
|
|
|
|
$str = $prefix . $str;
|
|
|
|
|
}
|
2013-06-03 09:50:43 +00:00
|
|
|
self::$debug[] = rtrim( UtfNormal::cleanUp( $str ) );
|
2012-08-27 20:57:15 +00:00
|
|
|
}
|
2011-12-04 18:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Begins profiling on a database query
|
|
|
|
|
*
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param string $sql
|
|
|
|
|
* @param string $function
|
|
|
|
|
* @param bool $isMaster
|
2011-12-04 18:35:40 +00:00
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 22:07:49 +00:00
|
|
|
// Replace invalid UTF-8 chars with a square UTF-8 character
|
|
|
|
|
// This prevents json_encode from erroring out due to binary SQL data
|
|
|
|
|
$sql = preg_replace(
|
|
|
|
|
'/(
|
|
|
|
|
[\xC0-\xC1] # Invalid UTF-8 Bytes
|
|
|
|
|
| [\xF5-\xFF] # Invalid UTF-8 Bytes
|
|
|
|
|
| \xE0[\x80-\x9F] # Overlong encoding of prior code point
|
|
|
|
|
| \xF0[\x80-\x8F] # Overlong encoding of prior code point
|
|
|
|
|
| [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start
|
|
|
|
|
| [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start
|
|
|
|
|
| [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start
|
|
|
|
|
| (?<=[\x0-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle
|
|
|
|
|
| (?<![\xC2-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF4]
|
|
|
|
|
|[\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence
|
|
|
|
|
| (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence
|
|
|
|
|
| (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence
|
|
|
|
|
| (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2)
|
|
|
|
|
)/x',
|
|
|
|
|
'■',
|
|
|
|
|
$sql
|
|
|
|
|
);
|
|
|
|
|
|
2011-12-04 18:35:40 +00:00
|
|
|
self::$query[] = array(
|
|
|
|
|
'sql' => $sql,
|
|
|
|
|
'function' => $function,
|
2013-08-31 16:36:02 +00:00
|
|
|
'master' => (bool)$isMaster,
|
2012-02-08 22:41:11 +00:00
|
|
|
'time' => 0.0,
|
2012-02-03 08:32:34 +00:00
|
|
|
'_start' => microtime( true ),
|
2011-12-04 18:35:40 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return count( self::$query ) - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculates how long a query took.
|
|
|
|
|
*
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param int $id
|
2011-12-04 18:35:40 +00:00
|
|
|
*/
|
|
|
|
|
public static function queryTime( $id ) {
|
|
|
|
|
if ( $id === -1 || !self::$enabled ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-03 08:32:34 +00:00
|
|
|
self::$query[$id]['time'] = microtime( true ) - self::$query[$id]['_start'];
|
2011-12-04 18:35:40 +00:00
|
|
|
unset( self::$query[$id]['_start'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a list of files included, along with their size
|
|
|
|
|
*
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param IContextSource $context
|
2011-12-04 18:35:40 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2011-12-15 02:26:14 +00:00
|
|
|
protected static function getFilesIncluded( IContextSource $context ) {
|
2011-12-04 18:35:40 +00:00
|
|
|
$files = get_included_files();
|
|
|
|
|
$fileList = array();
|
|
|
|
|
foreach ( $files as $file ) {
|
|
|
|
|
$size = filesize( $file );
|
|
|
|
|
$fileList[] = array(
|
|
|
|
|
'name' => $file,
|
2011-12-15 02:26:14 +00:00
|
|
|
'size' => $context->getLanguage()->formatSize( $size ),
|
2011-12-04 18:35:40 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $fileList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the HTML to add to the page for the toolbar
|
|
|
|
|
*
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param IContextSource $context
|
2011-12-04 18:35:40 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-12-15 02:26:14 +00:00
|
|
|
public static function getDebugHTML( IContextSource $context ) {
|
2012-08-27 20:57:15 +00:00
|
|
|
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.
|
Optimize order of styles and scripts
The current ordering of scripts and stylesheets in <head> causes all major
browsers to serialize and defer requests that could be performed in parallel.
The problem is that external stylesheets are loaded before inline scripts. As
Steven Souders explains, "all major browsers preserve the order of CSS and
JavaScript. The stylesheet has to be fully downloaded, parsed, and applied
before the inline script is executed. And the inline script must be executed
before the remaining resources can be downloaded. Therefore, resources that
follow a stylesheet and inline script are blocked from downloading."[1]
In other words: the browser could start loading body images, but it refuses to
do that until it has executed inline scripts in head. And it refuses to execute
those scripts until the external CSS is downloaded, parsed and applied. You can
see the effect of this in this image, showing the request waterfall for
[[en:Gothic Alphabet]]: [2]. Notice how no images were requested before the
browser had finished processing the three load.php requests at the top.
To fix this, we want to move the inline scripts above the external CSS. This is
a little bit tricky, because the inline scripts depend on mw.loader, which is
loaded via an external script. If we move the external script so that it too is
above the external stylesheet, we force the browser to serialize requests,
because the browser will not retrieve the external CSS until it has retrieved
and executed the external JS code. So what we want is to move the inline
scripts above the external stylesheet, but keep the external script (which the
inline scripts depend on) below the external stylesheet.
We can do this by wrapping the inline script code in a closure (which binds
'mw') and enqueuing the closure in a global array which will be processed by
the startup module at just the right time.
Net result: external CSS and JS is retrieved in parallel, retrieval of images
(and other external assets) is unblocked, but the order in which code is
evaluated remains the same.
[1]: <http://www.stevesouders.com/blog/2009/05/06/positioning-inline-scripts/>
[2]: <http://people.wikimedia.org/~ori/enwiki-waterfall.png> (excerpted from
<http://www.webpagetest.org/result/150316_0C_7MB/1/details/>.
Change-Id: I98d383a6299ffbd10210431544a505338ca8643f
2015-03-16 02:45:17 +00:00
|
|
|
$html = ResourceLoader::makeInlineScript(
|
|
|
|
|
ResourceLoader::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) )
|
2012-08-27 20:57:15 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 ) {
|
2011-12-04 18:35:40 +00:00
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-27 20:57:15 +00:00
|
|
|
$curIdent = 0;
|
|
|
|
|
$ret = "\n<hr />\n<strong>Debug data:</strong><ul id=\"mw-debug-html\">\n<li>";
|
|
|
|
|
|
|
|
|
|
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;
|
2012-05-13 09:20:04 +00:00
|
|
|
|
2012-08-27 20:57:15 +00:00
|
|
|
$display = $pre . $display;
|
|
|
|
|
if ( $display == '' ) {
|
|
|
|
|
$display = "\xc2\xa0";
|
|
|
|
|
}
|
2012-05-13 09:20:04 +00:00
|
|
|
|
2013-11-20 14:03:00 +00:00
|
|
|
if ( !$ident
|
|
|
|
|
&& $diff < 0
|
|
|
|
|
&& substr( $display, 0, 9 ) != 'Entering '
|
|
|
|
|
&& substr( $display, 0, 8 ) != 'Exiting '
|
|
|
|
|
) {
|
2012-08-27 20:57:15 +00:00
|
|
|
$ident = $curIdent;
|
|
|
|
|
$diff = 0;
|
2013-11-20 14:03:00 +00:00
|
|
|
$display = '<span style="background:yellow;">' .
|
|
|
|
|
nl2br( htmlspecialchars( $display ) ) . '</span>';
|
2012-08-27 20:57:15 +00:00
|
|
|
} 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 );
|
|
|
|
|
}
|
2013-11-20 16:21:31 +00:00
|
|
|
$ret .= "<code>$display</code>\n";
|
2012-08-27 20:57:15 +00:00
|
|
|
|
|
|
|
|
$curIdent = $ident;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ret .= str_repeat( '</li></ul>', $curIdent ) . "</li>\n</ul>\n";
|
|
|
|
|
|
|
|
|
|
return $ret;
|
2012-05-13 09:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Append the debug info to given ApiResult
|
|
|
|
|
*
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param IContextSource $context
|
|
|
|
|
* @param ApiResult $result
|
2012-05-13 09:20:04 +00:00
|
|
|
*/
|
|
|
|
|
public static function appendDebugInfoToApiResult( IContextSource $context, ApiResult $result ) {
|
|
|
|
|
if ( !self::$enabled ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 20:00:04 +00:00
|
|
|
// output errors as debug info, when display_errors is on
|
|
|
|
|
// this is necessary for all non html output of the api, because that clears all errors first
|
|
|
|
|
$obContents = ob_get_contents();
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( $obContents ) {
|
2012-08-14 20:00:04 +00:00
|
|
|
$obContentArray = explode( '<br />', $obContents );
|
2013-04-20 20:28:52 +00:00
|
|
|
foreach ( $obContentArray as $obContent ) {
|
|
|
|
|
if ( trim( $obContent ) ) {
|
2012-08-14 20:00:04 +00:00
|
|
|
self::debugMsg( Sanitizer::stripAllTags( $obContent ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-13 09:20:04 +00:00
|
|
|
MWDebug::log( 'MWDebug output complete' );
|
|
|
|
|
$debugInfo = self::getDebugInfo( $context );
|
|
|
|
|
|
|
|
|
|
$result->setIndexedTagName( $debugInfo, 'debuginfo' );
|
|
|
|
|
$result->setIndexedTagName( $debugInfo['log'], 'line' );
|
|
|
|
|
$result->setIndexedTagName( $debugInfo['debugLog'], 'msg' );
|
|
|
|
|
$result->setIndexedTagName( $debugInfo['queries'], 'query' );
|
|
|
|
|
$result->setIndexedTagName( $debugInfo['includes'], 'queries' );
|
2013-07-10 01:54:09 +00:00
|
|
|
$result->addValue( null, 'debuginfo', $debugInfo );
|
2012-05-13 09:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the HTML to add to the page for the toolbar
|
|
|
|
|
*
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param IContextSource $context
|
2012-05-13 09:20:04 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function getDebugInfo( IContextSource $context ) {
|
|
|
|
|
if ( !self::$enabled ) {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
global $wgVersion, $wgRequestTime;
|
2012-02-06 17:34:33 +00:00
|
|
|
$request = $context->getRequest();
|
2013-11-20 14:03:00 +00:00
|
|
|
|
2014-04-02 20:22:33 +00:00
|
|
|
// HHVM's reported memory usage from memory_get_peak_usage()
|
|
|
|
|
// is not useful when passing false, but we continue passing
|
|
|
|
|
// false for consistency of historical data in zend.
|
|
|
|
|
// see: https://github.com/facebook/hhvm/issues/2257#issuecomment-39362246
|
|
|
|
|
$realMemoryUsage = wfIsHHVM();
|
|
|
|
|
|
2012-05-13 09:20:04 +00:00
|
|
|
return array(
|
2011-12-04 18:35:40 +00:00
|
|
|
'mwVersion' => $wgVersion,
|
2014-08-03 21:02:45 +00:00
|
|
|
'phpEngine' => wfIsHHVM() ? 'HHVM' : 'PHP',
|
|
|
|
|
'phpVersion' => wfIsHHVM() ? HHVM_VERSION : PHP_VERSION,
|
2012-03-28 10:43:35 +00:00
|
|
|
'gitRevision' => GitInfo::headSHA1(),
|
|
|
|
|
'gitBranch' => GitInfo::currentBranch(),
|
2012-04-10 08:52:11 +00:00
|
|
|
'gitViewUrl' => GitInfo::headViewUrl(),
|
2012-02-03 08:32:34 +00:00
|
|
|
'time' => microtime( true ) - $wgRequestTime,
|
2011-12-04 18:35:40 +00:00
|
|
|
'log' => self::$log,
|
|
|
|
|
'debugLog' => self::$debug,
|
|
|
|
|
'queries' => self::$query,
|
2012-02-06 17:34:33 +00:00
|
|
|
'request' => array(
|
2012-08-07 06:33:41 +00:00
|
|
|
'method' => $request->getMethod(),
|
2012-02-06 17:34:33 +00:00
|
|
|
'url' => $request->getRequestURL(),
|
|
|
|
|
'headers' => $request->getAllHeaders(),
|
|
|
|
|
'params' => $request->getValues(),
|
|
|
|
|
),
|
2014-04-02 20:22:33 +00:00
|
|
|
'memory' => $context->getLanguage()->formatSize( memory_get_usage( $realMemoryUsage ) ),
|
|
|
|
|
'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage( $realMemoryUsage ) ),
|
2011-12-15 02:26:14 +00:00
|
|
|
'includes' => self::getFilesIncluded( $context ),
|
2011-12-04 18:35:40 +00:00
|
|
|
);
|
|
|
|
|
}
|
2012-01-13 23:07:52 +00:00
|
|
|
}
|