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
|
|
|
|
2016-08-24 23:12:02 +00:00
|
|
|
use MediaWiki\Logger\LegacyLogger;
|
|
|
|
|
|
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
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected static $log = [];
|
2011-12-04 18:35:40 +00:00
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected static $debug = [];
|
2011-12-04 18:35:40 +00:00
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected static $query = [];
|
2011-12-04 18:35:40 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected static $deprecationWarnings = [];
|
2012-01-03 05:56:36 +00:00
|
|
|
|
2020-04-17 04:41:04 +00:00
|
|
|
/**
|
|
|
|
|
* @var string[] Deprecation filter regexes
|
|
|
|
|
*/
|
|
|
|
|
protected static $deprecationFilters = [];
|
|
|
|
|
|
2019-08-31 22:43:23 +00:00
|
|
|
/**
|
|
|
|
|
* @internal For use by Setup.php only.
|
|
|
|
|
*/
|
|
|
|
|
public static function setup() {
|
|
|
|
|
global $wgDebugToolbar,
|
|
|
|
|
$wgUseCdn, $wgUseFileCache, $wgCommandLineMode;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
// Easy to forget to falsify $wgDebugToolbar for static caches.
|
|
|
|
|
// If file cache or CDN cache is on, just disable this (DWIMD).
|
|
|
|
|
$wgUseCdn ||
|
|
|
|
|
$wgUseFileCache ||
|
|
|
|
|
// Keep MWDebug off on CLI. This prevents MWDebug from eating up
|
|
|
|
|
// all the memory for logging SQL queries in maintenance scripts.
|
|
|
|
|
$wgCommandLineMode
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $wgDebugToolbar ) {
|
|
|
|
|
self::init();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2016-05-15 05:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* Disable the debugger.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
public static function deinit() {
|
|
|
|
|
self::$enabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
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 ) {
|
2016-08-24 23:00:09 +00:00
|
|
|
$out->addModules( 'mediawiki.debug' );
|
2012-02-06 17:34:33 +00:00
|
|
|
}
|
2011-12-04 18:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a line to the log
|
|
|
|
|
*
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2015-01-04 07:20:45 +00:00
|
|
|
* @param mixed $str
|
2011-12-04 18:35:40 +00:00
|
|
|
*/
|
|
|
|
|
public static function log( $str ) {
|
|
|
|
|
if ( !self::$enabled ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-01-04 07:20:45 +00:00
|
|
|
if ( !is_string( $str ) ) {
|
|
|
|
|
$str = print_r( $str, true );
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
self::$log[] = [
|
2012-01-03 05:56:36 +00:00
|
|
|
'msg' => htmlspecialchars( $str ),
|
|
|
|
|
'type' => 'log',
|
|
|
|
|
'caller' => wfGetCaller(),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2012-01-03 05:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
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() {
|
2016-02-17 09:09:32 +00:00
|
|
|
self::$log = [];
|
|
|
|
|
self::$deprecationWarnings = [];
|
2012-01-13 23:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
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).
|
2012-01-03 05:56:36 +00:00
|
|
|
*/
|
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
|
|
|
|
2020-04-17 04:41:04 +00:00
|
|
|
self::sendMessage(
|
|
|
|
|
self::formatCallerDescription( $msg, $callerDescription ),
|
|
|
|
|
'warning',
|
|
|
|
|
$level );
|
2012-01-03 05:56:36 +00:00
|
|
|
|
2012-08-25 11:09:46 +00:00
|
|
|
if ( self::$enabled ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
self::$log[] = [
|
2012-08-25 11:09:46 +00:00
|
|
|
'msg' => htmlspecialchars( $msg ),
|
|
|
|
|
'type' => 'warn',
|
|
|
|
|
'caller' => $callerDescription['func'],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2012-08-25 11:09:46 +00:00
|
|
|
}
|
2012-01-03 05:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-08-25 11:09:46 +00:00
|
|
|
* Show a warning that $function is deprecated.
|
2012-01-03 05:56:36 +00:00
|
|
|
*
|
2020-12-02 21:49:22 +00:00
|
|
|
* @see deprecatedMsg()
|
2012-05-16 16:25:30 +00:00
|
|
|
* @since 1.19
|
2020-12-02 21:49:22 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $function Function that is deprecated.
|
2019-12-31 11:17:36 +00:00
|
|
|
* @param string|false $version Version in which the function was deprecated.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string|bool $component Component to which the function belongs.
|
2018-12-27 20:29:14 +00:00
|
|
|
* If false, it is assumed the function is in MediaWiki core.
|
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. 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
|
|
|
|
|
) {
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
if ( $version ) {
|
|
|
|
|
$component = $component ?: 'MediaWiki';
|
|
|
|
|
$msg = "Use of $function was deprecated in $component $version.";
|
|
|
|
|
} else {
|
|
|
|
|
$msg = "Use of $function is deprecated.";
|
|
|
|
|
}
|
|
|
|
|
self::deprecatedMsg( $msg, $version, $component, $callerOffset + 1 );
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 21:49:22 +00:00
|
|
|
/**
|
|
|
|
|
* Show a warning if $method declared in $class is overridden in $instance.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.36
|
|
|
|
|
* @see deprecatedMsg()
|
|
|
|
|
*
|
|
|
|
|
* phpcs:ignore MediaWiki.Commenting.FunctionComment.ObjectTypeHintParam
|
|
|
|
|
* @param object $instance Object on which to detect deprecated overrides (typically $this).
|
|
|
|
|
* @param string $class Class declaring the deprecated method (typically __CLASS__ )
|
|
|
|
|
* @param string $method The name of the deprecated method.
|
|
|
|
|
* @param string|false $version Version in which the method was deprecated.
|
|
|
|
|
* @param string|bool $component Component to which the class belongs.
|
|
|
|
|
* If false, it is assumed the class is in MediaWiki core.
|
|
|
|
|
* @param int $callerOffset How far up the callstack is the original
|
|
|
|
|
* caller. 2 = function that called the function that called
|
|
|
|
|
* MWDebug::detectDeprecatedOverride()
|
|
|
|
|
*
|
|
|
|
|
* @return bool True if the method was overridden, false otherwise. If the method
|
|
|
|
|
* was overridden, it should be called. The deprecated method's default
|
|
|
|
|
* implementation should call MWDebug::deprecated().
|
|
|
|
|
*/
|
|
|
|
|
public static function detectDeprecatedOverride( $instance, $class, $method, $version = false,
|
|
|
|
|
$component = false, $callerOffset = 2
|
|
|
|
|
) {
|
|
|
|
|
$reflectionMethod = new ReflectionMethod( $instance, $method );
|
|
|
|
|
$declaringClass = $reflectionMethod->getDeclaringClass()->getName();
|
|
|
|
|
|
|
|
|
|
if ( $declaringClass === $class ) {
|
|
|
|
|
// not overridden, nothing to do
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $version ) {
|
|
|
|
|
$component = $component ?: 'MediaWiki';
|
|
|
|
|
$msg = "$declaringClass overrides $method which was deprecated in $component $version.";
|
|
|
|
|
} else {
|
|
|
|
|
$msg = "$declaringClass overrides $method which is deprecated.";
|
|
|
|
|
}
|
|
|
|
|
self::deprecatedMsg( $msg, $version, $component, $callerOffset + 1 );
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
/**
|
|
|
|
|
* Log a deprecation warning with arbitrary message text. A caller
|
|
|
|
|
* description will be appended. If the message has already been sent for
|
|
|
|
|
* this caller, it won't be sent again.
|
|
|
|
|
*
|
|
|
|
|
* Although there are component and version parameters, they are not
|
|
|
|
|
* automatically appended to the message. The message text should include
|
|
|
|
|
* information about when the thing was deprecated.
|
|
|
|
|
*
|
2020-12-02 21:49:22 +00:00
|
|
|
* The warning will be sent 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. This is the case in phpunit tests per default, and will
|
|
|
|
|
* cause tests to fail.
|
|
|
|
|
* - MediaWiki's debug log, if $wgDevelopmentWarnings is set to false.
|
|
|
|
|
*
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
* @since 1.35
|
|
|
|
|
*
|
|
|
|
|
* @param string $msg The message
|
|
|
|
|
* @param string|false $version Version of MediaWiki that the function
|
|
|
|
|
* was deprecated in.
|
|
|
|
|
* @param string|bool $component Component to which the function belongs.
|
|
|
|
|
* If false, it is assumed the function is in MediaWiki core.
|
|
|
|
|
* @param int|false $callerOffset How far up the call stack is the original
|
|
|
|
|
* caller. 2 = function that called the function that called us. If false,
|
|
|
|
|
* the caller description will not be appended.
|
|
|
|
|
*/
|
|
|
|
|
public static function deprecatedMsg( $msg, $version = false,
|
|
|
|
|
$component = false, $callerOffset = 2
|
|
|
|
|
) {
|
|
|
|
|
if ( $callerOffset === false ) {
|
|
|
|
|
$callerFunc = '';
|
|
|
|
|
$rawMsg = $msg;
|
|
|
|
|
} else {
|
|
|
|
|
$callerDescription = self::getCallerDescription( $callerOffset );
|
|
|
|
|
$callerFunc = $callerDescription['func'];
|
|
|
|
|
$rawMsg = self::formatCallerDescription( $msg, $callerDescription );
|
|
|
|
|
}
|
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
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
if ( isset( self::$deprecationWarnings[$msg][$callerFunc] ) ) {
|
2012-01-03 05:56:36 +00:00
|
|
|
return;
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
} elseif ( isset( self::$deprecationWarnings[$msg] ) ) {
|
2012-08-25 11:09:46 +00:00
|
|
|
if ( self::$enabled ) {
|
|
|
|
|
$sendToLog = false;
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-01-03 05:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
self::$deprecationWarnings[$msg][$callerFunc] = true;
|
2012-01-03 05:56:36 +00:00
|
|
|
|
2012-08-25 11:09:46 +00:00
|
|
|
if ( $version ) {
|
|
|
|
|
global $wgDeprecationReleaseLimit;
|
2020-07-08 17:06:21 +00:00
|
|
|
|
|
|
|
|
$component = $component ?: 'MediaWiki';
|
|
|
|
|
if ( $wgDeprecationReleaseLimit && $component === 'MediaWiki' ) {
|
2012-08-25 11:09:46 +00:00
|
|
|
# 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 04:41:04 +00:00
|
|
|
self::sendRawDeprecated(
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
$rawMsg,
|
2020-04-17 04:41:04 +00:00
|
|
|
$sendToLog,
|
2020-07-08 17:06:21 +00:00
|
|
|
$callerFunc
|
|
|
|
|
);
|
2020-04-17 04:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a raw deprecation message to the log and the debug toolbar,
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
* without filtering of duplicate messages. A caller description will
|
|
|
|
|
* not be appended.
|
2020-04-17 04:41:04 +00:00
|
|
|
*
|
|
|
|
|
* @param string $msg The complete message including relevant caller information.
|
|
|
|
|
* @param bool $sendToLog If true, the message will be sent to the debug
|
2020-05-28 02:14:07 +00:00
|
|
|
* toolbar, the debug log, and raised as a warning to PHP. If false, the message
|
|
|
|
|
* will only be sent to the debug toolbar.
|
2020-04-17 04:41:04 +00:00
|
|
|
* @param string $callerFunc The caller, for display in the debug toolbar's
|
|
|
|
|
* caller column.
|
|
|
|
|
*/
|
|
|
|
|
public static function sendRawDeprecated( $msg, $sendToLog = true, $callerFunc = '' ) {
|
|
|
|
|
foreach ( self::$deprecationFilters as $filter ) {
|
|
|
|
|
if ( preg_match( $filter, $msg ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-25 11:09:46 +00:00
|
|
|
if ( $sendToLog ) {
|
2020-05-28 02:14:07 +00:00
|
|
|
trigger_error( $msg, E_USER_DEPRECATED );
|
2012-08-25 11:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( self::$enabled ) {
|
|
|
|
|
$logMsg = htmlspecialchars( $msg ) .
|
2016-02-17 09:09:32 +00:00
|
|
|
Html::rawElement( 'div', [ 'class' => 'mw-debug-backtrace' ],
|
|
|
|
|
Html::element( 'span', [], 'Backtrace:' ) . wfBacktrace()
|
2012-08-25 11:09:46 +00:00
|
|
|
);
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
self::$log[] = [
|
2012-08-25 11:09:46 +00:00
|
|
|
'msg' => $logMsg,
|
|
|
|
|
'type' => 'deprecated',
|
|
|
|
|
'caller' => $callerFunc,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2012-08-25 11:09:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 04:41:04 +00:00
|
|
|
/**
|
|
|
|
|
* Deprecation messages matching the supplied regex will be suppressed.
|
|
|
|
|
* Use this to filter deprecation warnings when testing deprecated code.
|
|
|
|
|
*
|
|
|
|
|
* @param string $regex
|
|
|
|
|
*/
|
|
|
|
|
public static function filterDeprecationForTest( $regex ) {
|
2020-04-22 05:55:00 +00:00
|
|
|
if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
|
2020-04-17 04:41:04 +00:00
|
|
|
throw new RuntimeException( __METHOD__ . ' can only be used in tests' );
|
|
|
|
|
}
|
|
|
|
|
self::$deprecationFilters[] = $regex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear all deprecation filters.
|
|
|
|
|
*/
|
|
|
|
|
public static function clearDeprecationFilters() {
|
|
|
|
|
self::$deprecationFilters = [];
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-25 11:09:46 +00:00
|
|
|
/**
|
|
|
|
|
* 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';
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ 'file' => $file, 'func' => $func ];
|
2012-08-25 11:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-17 04:41:04 +00:00
|
|
|
/**
|
|
|
|
|
* Append a caller description to an error message
|
|
|
|
|
*
|
|
|
|
|
* @param string $msg
|
|
|
|
|
* @param array $caller Caller description from getCallerDescription()
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private static function formatCallerDescription( $msg, $caller ) {
|
2020-05-28 02:14:07 +00:00
|
|
|
// When changing this, update the below parseCallerDescription() method as well.
|
2020-04-17 04:41:04 +00:00
|
|
|
return $msg . ' [Called from ' . $caller['func'] . ' in ' . $caller['file'] . ']';
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 02:14:07 +00:00
|
|
|
/**
|
|
|
|
|
* Append a caller description to an error message
|
|
|
|
|
*
|
|
|
|
|
* @internal For use by MWExceptionHandler to override 'exception.file' in error logs.
|
|
|
|
|
* @param string $msg Formatted message from formatCallerDescription() and getCallerDescription()
|
|
|
|
|
* @return null|array<string,string> Null if unable to recognise all parts, or array with:
|
|
|
|
|
* - 'file': string of file path
|
|
|
|
|
* - 'line': string of line number
|
|
|
|
|
* - 'func': string of function or method name
|
|
|
|
|
* - 'message': Re-formatted version of $msg for use with ErrorException,
|
|
|
|
|
* so as to not include file/line twice.
|
|
|
|
|
*/
|
|
|
|
|
public static function parseCallerDescription( $msg ) {
|
|
|
|
|
$match = null;
|
|
|
|
|
preg_match( '/(.*) \[Called from ([^ ]+) in ([^ ]+) at line (\d+)\]$/', $msg, $match );
|
|
|
|
|
if ( $match ) {
|
|
|
|
|
return [
|
|
|
|
|
'message' => "{$match[1]} [Called from {$match[2]}]",
|
|
|
|
|
'func' => $match[2],
|
|
|
|
|
'file' => $match[3],
|
|
|
|
|
'line' => $match[4],
|
|
|
|
|
];
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-25 11:09:46 +00:00
|
|
|
/**
|
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 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
|
|
|
*/
|
2020-04-17 04:41:04 +00:00
|
|
|
private static function sendMessage( $msg, $group, $level ) {
|
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
|
|
|
|
2020-04-16 06:15:44 +00:00
|
|
|
wfDebugLog( $group, $msg );
|
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
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public static function debugMsg( $str, $context = [] ) {
|
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']} ";
|
|
|
|
|
}
|
2016-08-24 23:12:02 +00:00
|
|
|
$str = LegacyLogger::interpolate( $str, $context );
|
2015-03-18 22:42:23 +00:00
|
|
|
$str = $prefix . $str;
|
|
|
|
|
}
|
2015-03-07 09:27:42 +00:00
|
|
|
self::$debug[] = rtrim( UtfNormal\Validator::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
|
2016-08-21 01:40:26 +00:00
|
|
|
* @param float $runTime Query run time
|
rdbms: Restore debug toolbar "Queries" feature
This broke after e0cc49ce3971e19, due to the field 'master'
being removed from the log context. The LegacyLogger logic
forwarding these messages to MWDebug (for the debug toolbar)
however, was dependant on.
Users of debug toolbar experienced a silent failure because the
logic in question is very tolerant of missing fields. This is
because it uses those fields to distinguish the 'sql' messages
from channel=DBQuery from other messages in the same channel.
Making that less fragile is outside the scope of this commit.
This commit:
* Restore the basic functionality by making sure MWDebug::query()
gets called again for DBQuery messages.
* Remove the code relating to the 'master' field as this no longer
exists in RDBMS. It also wasn't used anywhere (to be used,
it would need to be read by mediawiki.debug/debug.js).
* Remove unexpanded "{method}" and "{runtime}" noise in the debug
toolbar text. This was introduced by he conversion to PSR-3
logging.. These fields are already rendered separately by
the toolbar and should not be part of the "SQL" column.
To do this, we need to log the $sql bit as its own key, so
I've made this a context field as well.
* Reduce the condition logic in LegacyLogger to only looking for
'DBQuery' and 'sql'. This way, if it breaks again it will
still call the logic within and emit E_NOTICE instead, which
would help detect the issue (and still fallback to at least
showing the queries). Unlike before this commit where it took
quite some time to figure out why it wasn't working.
* The above fixes still weren't enough to get queries to show
up in the Debug toolbar for me. Turns out, this was because
my local setup (mediawiki-docker-dev) uses a master-replica
set up. The setup doesn't use any custom LBFactory config,
just plain $wgDBservers. The logic for turning these plain
settings into LBFactory (in MWLBFactory.php) does kick in,
and does run (unlike if I had custom wgLBFactoryConf).
But, the DBO_DEBUG flag didn't make it through because of
the += operator preferring any pre-existing value my setup
has, which is just `DBO_DEFAULT`.
Merging 'flags' keys seems unsafe in general, but adding
DBO_DEBUG based on $wgDebugDumpSql seems innocent and doesn't
affect other behaviour (it's a case of DWIM).
Bug: T231742
Change-Id: I122bb1a65620a7ae4e1943136c975b63524a5111
2019-10-05 01:56:12 +00:00
|
|
|
* @param string $dbhost
|
2018-09-14 23:02:12 +00:00
|
|
|
* @return bool True if debugger is enabled, false otherwise
|
2011-12-04 18:35:40 +00:00
|
|
|
*/
|
rdbms: Restore debug toolbar "Queries" feature
This broke after e0cc49ce3971e19, due to the field 'master'
being removed from the log context. The LegacyLogger logic
forwarding these messages to MWDebug (for the debug toolbar)
however, was dependant on.
Users of debug toolbar experienced a silent failure because the
logic in question is very tolerant of missing fields. This is
because it uses those fields to distinguish the 'sql' messages
from channel=DBQuery from other messages in the same channel.
Making that less fragile is outside the scope of this commit.
This commit:
* Restore the basic functionality by making sure MWDebug::query()
gets called again for DBQuery messages.
* Remove the code relating to the 'master' field as this no longer
exists in RDBMS. It also wasn't used anywhere (to be used,
it would need to be read by mediawiki.debug/debug.js).
* Remove unexpanded "{method}" and "{runtime}" noise in the debug
toolbar text. This was introduced by he conversion to PSR-3
logging.. These fields are already rendered separately by
the toolbar and should not be part of the "SQL" column.
To do this, we need to log the $sql bit as its own key, so
I've made this a context field as well.
* Reduce the condition logic in LegacyLogger to only looking for
'DBQuery' and 'sql'. This way, if it breaks again it will
still call the logic within and emit E_NOTICE instead, which
would help detect the issue (and still fallback to at least
showing the queries). Unlike before this commit where it took
quite some time to figure out why it wasn't working.
* The above fixes still weren't enough to get queries to show
up in the Debug toolbar for me. Turns out, this was because
my local setup (mediawiki-docker-dev) uses a master-replica
set up. The setup doesn't use any custom LBFactory config,
just plain $wgDBservers. The logic for turning these plain
settings into LBFactory (in MWLBFactory.php) does kick in,
and does run (unlike if I had custom wgLBFactoryConf).
But, the DBO_DEBUG flag didn't make it through because of
the += operator preferring any pre-existing value my setup
has, which is just `DBO_DEFAULT`.
Merging 'flags' keys seems unsafe in general, but adding
DBO_DEBUG based on $wgDebugDumpSql seems innocent and doesn't
affect other behaviour (it's a case of DWIM).
Bug: T231742
Change-Id: I122bb1a65620a7ae4e1943136c975b63524a5111
2019-10-05 01:56:12 +00:00
|
|
|
public static function query( $sql, $function, $runTime, $dbhost ) {
|
2011-12-04 18:35:40 +00:00
|
|
|
if ( !self::$enabled ) {
|
2018-09-14 23:02:12 +00:00
|
|
|
return false;
|
2011-12-04 18:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
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]
|
2017-02-25 21:53:36 +00:00
|
|
|
| [\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence
|
2014-08-22 22:07:49 +00:00
|
|
|
| (?<=[\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
|
|
|
|
|
);
|
|
|
|
|
|
2016-04-22 19:50:44 +00:00
|
|
|
// last check for invalid utf8
|
|
|
|
|
$sql = UtfNormal\Validator::cleanUp( $sql );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
self::$query[] = [
|
rdbms: Restore debug toolbar "Queries" feature
This broke after e0cc49ce3971e19, due to the field 'master'
being removed from the log context. The LegacyLogger logic
forwarding these messages to MWDebug (for the debug toolbar)
however, was dependant on.
Users of debug toolbar experienced a silent failure because the
logic in question is very tolerant of missing fields. This is
because it uses those fields to distinguish the 'sql' messages
from channel=DBQuery from other messages in the same channel.
Making that less fragile is outside the scope of this commit.
This commit:
* Restore the basic functionality by making sure MWDebug::query()
gets called again for DBQuery messages.
* Remove the code relating to the 'master' field as this no longer
exists in RDBMS. It also wasn't used anywhere (to be used,
it would need to be read by mediawiki.debug/debug.js).
* Remove unexpanded "{method}" and "{runtime}" noise in the debug
toolbar text. This was introduced by he conversion to PSR-3
logging.. These fields are already rendered separately by
the toolbar and should not be part of the "SQL" column.
To do this, we need to log the $sql bit as its own key, so
I've made this a context field as well.
* Reduce the condition logic in LegacyLogger to only looking for
'DBQuery' and 'sql'. This way, if it breaks again it will
still call the logic within and emit E_NOTICE instead, which
would help detect the issue (and still fallback to at least
showing the queries). Unlike before this commit where it took
quite some time to figure out why it wasn't working.
* The above fixes still weren't enough to get queries to show
up in the Debug toolbar for me. Turns out, this was because
my local setup (mediawiki-docker-dev) uses a master-replica
set up. The setup doesn't use any custom LBFactory config,
just plain $wgDBservers. The logic for turning these plain
settings into LBFactory (in MWLBFactory.php) does kick in,
and does run (unlike if I had custom wgLBFactoryConf).
But, the DBO_DEBUG flag didn't make it through because of
the += operator preferring any pre-existing value my setup
has, which is just `DBO_DEFAULT`.
Merging 'flags' keys seems unsafe in general, but adding
DBO_DEBUG based on $wgDebugDumpSql seems innocent and doesn't
affect other behaviour (it's a case of DWIM).
Bug: T231742
Change-Id: I122bb1a65620a7ae4e1943136c975b63524a5111
2019-10-05 01:56:12 +00:00
|
|
|
'sql' => "$dbhost: $sql",
|
2011-12-04 18:35:40 +00:00
|
|
|
'function' => $function,
|
2016-08-21 01:40:26 +00:00
|
|
|
'time' => $runTime,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-12-04 18:35:40 +00:00
|
|
|
|
2018-09-14 23:02:12 +00:00
|
|
|
return true;
|
2011-12-04 18:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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();
|
2016-02-17 09:09:32 +00:00
|
|
|
$fileList = [];
|
2011-12-04 18:35:40 +00:00
|
|
|
foreach ( $files as $file ) {
|
|
|
|
|
$size = filesize( $file );
|
2016-02-17 09:09:32 +00:00
|
|
|
$fileList[] = [
|
2011-12-04 18:35:40 +00:00
|
|
|
'name' => $file,
|
2011-12-15 02:26:14 +00:00
|
|
|
'size' => $context->getLanguage()->formatSize( $size ),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
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 ) {
|
2017-07-23 01:24:09 +00:00
|
|
|
self::log( 'MWDebug output complete' );
|
2012-08-27 20:57:15 +00:00
|
|
|
$debugInfo = self::getDebugInfo( $context );
|
|
|
|
|
|
|
|
|
|
// Cannot use OutputPage::addJsConfigVars because those are already outputted
|
|
|
|
|
// by the time this method is called.
|
2015-03-25 04:48:02 +00:00
|
|
|
$html = ResourceLoader::makeInlineScript(
|
Initial support for Content Security Policy, disabled by default
The primary goal here is a defense in depth measure to
stop an attacker who found a bug in the parser allowing
them to insert malicious attributes.
This wouldn't stop someone who could insert a full
script tag (since at current it can't distinguish between
malicious and legit user js). It also would not prevent
DOM-based or reflected XSS for anons, as the nonce value
is guessable for anons when receiving a response cached
by varnish. However, the limited protection of just stopping
stored XSS where the attacker only has control of attributes,
is still a big win in my opinion. (But it wouldn't prevent
someone who has that type of xss from abusing things like
data-ooui attribute).
This will likely break many gadgets. Its expected that any
sort of rollout on Wikimedia will be done very slowly, with
lots of testing and the report-only option to begin with.
This is behind feature flags that are off by default, so
merging this patch should not cause any change in default
behaviour.
This may break some extensions (The most obvious one
is charinsert (See fe648d41005), but will probably need
some testing in report-only mode to see if anything else breaks)
This uses the unsafe-eval option of CSP, in order to
support RL's local storage thingy. For better security,
we may want to remove some of the sillier uses of eval
(e.g. jquery.ui.datepicker.js).
For more info, see spec: https://www.w3.org/TR/CSP2/
Additionally see:
https://www.mediawiki.org/wiki/Requests_for_comment/Content-Security-Policy
Bug: T135963
Change-Id: I80f6f469ba4c0b608385483457df96ccb7429ae5
2016-02-29 04:13:10 +00:00
|
|
|
ResourceLoader::makeConfigSetScript( [ 'debugInfo' => $debugInfo ] ),
|
2019-10-28 05:01:17 +00:00
|
|
|
$context->getOutput()->getCSP()->getNonce()
|
2012-08-27 20:57:15 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $wgDebugComments ) {
|
|
|
|
|
$html .= "<!-- Debug output:\n" .
|
2016-09-15 19:53:43 +00:00
|
|
|
htmlspecialchars( implode( "\n", self::$debug ), ENT_NOQUOTES ) .
|
2012-08-27 20:57:15 +00:00
|
|
|
"\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() {
|
2015-10-01 05:47:09 +00:00
|
|
|
global $wgShowDebug;
|
2012-08-27 20:57:15 +00:00
|
|
|
|
|
|
|
|
if ( !$wgShowDebug ) {
|
2011-12-04 18:35:40 +00:00
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 05:47:09 +00:00
|
|
|
$ret = "\n<hr />\n<strong>Debug data:</strong><ul id=\"mw-debug-html\">\n";
|
2012-08-27 20:57:15 +00:00
|
|
|
|
|
|
|
|
foreach ( self::$debug as $line ) {
|
2015-10-01 05:47:09 +00:00
|
|
|
$display = nl2br( htmlspecialchars( trim( $line ) ) );
|
2012-08-27 20:57:15 +00:00
|
|
|
|
2015-10-01 05:47:09 +00:00
|
|
|
$ret .= "<li><code>$display</code></li>\n";
|
2012-08-27 20:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-01 05:47:09 +00:00
|
|
|
$ret .= '</ul>' . "\n";
|
2012-08-27 20:57:15 +00:00
|
|
|
|
2020-07-29 00:31:44 +00:00
|
|
|
return Html::rawElement( 'div', [ 'id' => 'mw-html-debug-log' ], $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 ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-23 01:24:09 +00:00
|
|
|
self::log( 'MWDebug output complete' );
|
2012-05-13 09:20:04 +00:00
|
|
|
$debugInfo = self::getDebugInfo( $context );
|
|
|
|
|
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
ApiResult::setIndexedTagName( $debugInfo, 'debuginfo' );
|
|
|
|
|
ApiResult::setIndexedTagName( $debugInfo['log'], 'line' );
|
|
|
|
|
ApiResult::setIndexedTagName( $debugInfo['debugLog'], 'msg' );
|
|
|
|
|
ApiResult::setIndexedTagName( $debugInfo['queries'], 'query' );
|
|
|
|
|
ApiResult::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 ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2012-05-13 09:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
2012-02-06 17:34:33 +00:00
|
|
|
$request = $context->getRequest();
|
2013-11-20 14:03:00 +00:00
|
|
|
|
2016-08-24 22:44:41 +00:00
|
|
|
$branch = GitInfo::currentBranch();
|
|
|
|
|
if ( GitInfo::isSHA1( $branch ) ) {
|
|
|
|
|
// If it's a detached HEAD, the SHA1 will already be
|
|
|
|
|
// included in the MW version, so don't show it.
|
|
|
|
|
$branch = false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2020-02-25 01:33:18 +00:00
|
|
|
'mwVersion' => MW_VERSION,
|
2019-10-04 12:07:10 +00:00
|
|
|
'phpEngine' => 'PHP',
|
|
|
|
|
'phpVersion' => PHP_VERSION,
|
2012-03-28 10:43:35 +00:00
|
|
|
'gitRevision' => GitInfo::headSHA1(),
|
2016-08-24 22:44:41 +00:00
|
|
|
'gitBranch' => $branch,
|
2012-04-10 08:52:11 +00:00
|
|
|
'gitViewUrl' => GitInfo::headViewUrl(),
|
2016-08-12 10:33:37 +00:00
|
|
|
'time' => $request->getElapsedTime(),
|
2011-12-04 18:35:40 +00:00
|
|
|
'log' => self::$log,
|
|
|
|
|
'debugLog' => self::$debug,
|
|
|
|
|
'queries' => self::$query,
|
2016-02-17 09:09:32 +00:00
|
|
|
'request' => [
|
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(),
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2019-10-04 12:07:10 +00:00
|
|
|
'memory' => $context->getLanguage()->formatSize( memory_get_usage() ),
|
|
|
|
|
'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ),
|
2011-12-15 02:26:14 +00:00
|
|
|
'includes' => self::getFilesIncluded( $context ),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-12-04 18:35:40 +00:00
|
|
|
}
|
2012-01-13 23:07:52 +00:00
|
|
|
}
|