| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Debug; |
| 22 |
|
| 23 |
use LogicException; |
| 24 |
use MediaWiki\Api\ApiResult; |
| 25 |
use MediaWiki\Context\IContextSource; |
| 26 |
use MediaWiki\Html\Html; |
| 27 |
use MediaWiki\Logger\LegacyLogger; |
| 28 |
use MediaWiki\Output\OutputPage; |
| 29 |
use MediaWiki\Parser\Sanitizer; |
| 30 |
use MediaWiki\ResourceLoader\ResourceLoader; |
| 31 |
use MediaWiki\Utils\GitInfo; |
| 32 |
use ReflectionMethod; |
| 33 |
use UtfNormal; |
| 34 |
use Wikimedia\WrappedString; |
| 35 |
use Wikimedia\WrappedStringList; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class MWDebug { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
protected static $log = []; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
protected static $debug = []; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
protected static $query = []; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
protected static $enabled = false; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
protected static $deprecationWarnings = []; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
protected static $deprecationFilters = []; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public static function setup() { |
| 94 |
global $wgDebugToolbar, |
| 95 |
$wgUseCdn, $wgUseFileCache; |
| 96 |
|
| 97 |
if ( |
| 98 |
|
| 99 |
|
| 100 |
$wgUseCdn || |
| 101 |
$wgUseFileCache || |
| 102 |
|
| 103 |
|
| 104 |
MW_ENTRY_POINT === 'cli' |
| 105 |
) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
if ( $wgDebugToolbar ) { |
| 110 |
self::init(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public static function init() { |
| 121 |
self::$enabled = true; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public static function deinit() { |
| 130 |
self::$enabled = false; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public static function addModules( OutputPage $out ) { |
| 141 |
if ( self::$enabled ) { |
| 142 |
$out->addModules( 'mediawiki.debug' ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public static function log( $str ) { |
| 153 |
if ( !self::$enabled ) { |
| 154 |
return; |
| 155 |
} |
| 156 |
if ( !is_string( $str ) ) { |
| 157 |
$str = print_r( $str, true ); |
| 158 |
} |
| 159 |
self::$log[] = [ |
| 160 |
'msg' => htmlspecialchars( $str ), |
| 161 |
'type' => 'log', |
| 162 |
'caller' => wfGetCaller(), |
| 163 |
]; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public static function getLog() { |
| 172 |
return self::$log; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public static function clearLog() { |
| 180 |
self::$log = []; |
| 181 |
self::$deprecationWarnings = []; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public static function warning( $msg, $callerOffset = 1, $level = E_USER_NOTICE, $log = 'auto' ) { |
| 196 |
global $wgDevelopmentWarnings; |
| 197 |
|
| 198 |
if ( $log === 'auto' && !$wgDevelopmentWarnings ) { |
| 199 |
$log = 'debug'; |
| 200 |
} |
| 201 |
|
| 202 |
if ( $log === 'debug' ) { |
| 203 |
$level = false; |
| 204 |
} |
| 205 |
|
| 206 |
$callerDescription = self::getCallerDescription( $callerOffset ); |
| 207 |
|
| 208 |
self::sendMessage( |
| 209 |
self::formatCallerDescription( $msg, $callerDescription ), |
| 210 |
'warning', |
| 211 |
$level ); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public static function deprecated( $function, $version = false, |
| 229 |
$component = false, $callerOffset = 2 |
| 230 |
) { |
| 231 |
if ( $version ) { |
| 232 |
$component = $component ?: 'MediaWiki'; |
| 233 |
$msg = "Use of $function was deprecated in $component $version."; |
| 234 |
} else { |
| 235 |
$msg = "Use of $function is deprecated."; |
| 236 |
} |
| 237 |
self::deprecatedMsg( $msg, $version, $component, $callerOffset + 1 ); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public static function detectDeprecatedOverride( $instance, $class, $method, $version = false, |
| 263 |
$component = false, $callerOffset = 2 |
| 264 |
) { |
| 265 |
$reflectionMethod = new ReflectionMethod( $instance, $method ); |
| 266 |
$declaringClass = $reflectionMethod->getDeclaringClass()->getName(); |
| 267 |
|
| 268 |
if ( $declaringClass === $class ) { |
| 269 |
|
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
if ( $version ) { |
| 274 |
$component = $component ?: 'MediaWiki'; |
| 275 |
$msg = "$declaringClass overrides $method which was deprecated in $component $version."; |
| 276 |
self::deprecatedMsg( $msg, $version, $component, $callerOffset + 1 ); |
| 277 |
} |
| 278 |
|
| 279 |
return true; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public static function deprecatedMsg( $msg, $version = false, |
| 311 |
$component = false, $callerOffset = 2 |
| 312 |
) { |
| 313 |
if ( $callerOffset === false ) { |
| 314 |
$callerFunc = ''; |
| 315 |
$rawMsg = $msg; |
| 316 |
} else { |
| 317 |
$callerDescription = self::getCallerDescription( $callerOffset ); |
| 318 |
$callerFunc = $callerDescription['func']; |
| 319 |
$rawMsg = self::formatCallerDescription( $msg, $callerDescription ); |
| 320 |
} |
| 321 |
|
| 322 |
$sendToLog = true; |
| 323 |
|
| 324 |
|
| 325 |
if ( isset( self::$deprecationWarnings[$msg][$callerFunc] ) ) { |
| 326 |
return; |
| 327 |
} elseif ( isset( self::$deprecationWarnings[$msg] ) ) { |
| 328 |
if ( self::$enabled ) { |
| 329 |
$sendToLog = false; |
| 330 |
} else { |
| 331 |
return; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
self::$deprecationWarnings[$msg][$callerFunc] = true; |
| 336 |
|
| 337 |
if ( $version ) { |
| 338 |
global $wgDeprecationReleaseLimit; |
| 339 |
|
| 340 |
$component = $component ?: 'MediaWiki'; |
| 341 |
if ( $wgDeprecationReleaseLimit && $component === 'MediaWiki' ) { |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
$comparableVersion = preg_replace( '/-.*$/', '', $version ); |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
if ( version_compare( $wgDeprecationReleaseLimit, $comparableVersion, '<' ) ) { |
| 350 |
$sendToLog = false; |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
self::sendRawDeprecated( |
| 356 |
$rawMsg, |
| 357 |
$sendToLog, |
| 358 |
$callerFunc |
| 359 |
); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public static function sendRawDeprecated( $msg, $sendToLog = true, $callerFunc = '' ) { |
| 375 |
foreach ( self::$deprecationFilters as $filter => $callback ) { |
| 376 |
if ( preg_match( $filter, $msg ) ) { |
| 377 |
if ( is_callable( $callback ) ) { |
| 378 |
$callback(); |
| 379 |
} |
| 380 |
return; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
if ( $sendToLog ) { |
| 385 |
trigger_error( $msg, E_USER_DEPRECATED ); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
public static function filterDeprecationForTest( |
| 397 |
string $regex, ?callable $callback = null |
| 398 |
): void { |
| 399 |
if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) { |
| 400 |
throw new LogicException( __METHOD__ . ' can only be used in tests' ); |
| 401 |
} |
| 402 |
self::$deprecationFilters[$regex] = $callback; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public static function clearDeprecationFilters() { |
| 409 |
self::$deprecationFilters = []; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
private static function getCallerDescription( $callerOffset ) { |
| 420 |
$callers = wfDebugBacktrace(); |
| 421 |
|
| 422 |
if ( isset( $callers[$callerOffset] ) ) { |
| 423 |
$callerfile = $callers[$callerOffset]; |
| 424 |
if ( isset( $callerfile['file'] ) && isset( $callerfile['line'] ) ) { |
| 425 |
$file = $callerfile['file'] . ' at line ' . $callerfile['line']; |
| 426 |
} else { |
| 427 |
$file = '(internal function)'; |
| 428 |
} |
| 429 |
} else { |
| 430 |
$file = '(unknown location)'; |
| 431 |
} |
| 432 |
|
| 433 |
if ( isset( $callers[$callerOffset + 1] ) ) { |
| 434 |
$callerfunc = $callers[$callerOffset + 1]; |
| 435 |
$func = ''; |
| 436 |
if ( isset( $callerfunc['class'] ) ) { |
| 437 |
$func .= $callerfunc['class'] . '::'; |
| 438 |
} |
| 439 |
if ( isset( $callerfunc['function'] ) ) { |
| 440 |
$func .= $callerfunc['function']; |
| 441 |
} |
| 442 |
} else { |
| 443 |
$func = 'unknown'; |
| 444 |
} |
| 445 |
|
| 446 |
return [ 'file' => $file, 'func' => $func ]; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
private static function formatCallerDescription( $msg, $caller ) { |
| 457 |
|
| 458 |
return $msg . ' [Called from ' . $caller['func'] . ' in ' . $caller['file'] . ']'; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
public static function parseCallerDescription( $msg ) { |
| 474 |
$match = null; |
| 475 |
preg_match( '/(.*) \[Called from ([^ ]+) in ([^ ]+) at line (\d+)\]$/', $msg, $match ); |
| 476 |
if ( $match ) { |
| 477 |
return [ |
| 478 |
'message' => "{$match[1]} [Called from {$match[2]}]", |
| 479 |
'func' => $match[2], |
| 480 |
'file' => $match[3], |
| 481 |
'line' => $match[4], |
| 482 |
]; |
| 483 |
} else { |
| 484 |
return null; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
private static function sendMessage( $msg, $group, $level ) { |
| 497 |
if ( $level !== false ) { |
| 498 |
trigger_error( $msg, $level ); |
| 499 |
} |
| 500 |
|
| 501 |
wfDebugLog( $group, $msg ); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
public static function debugMsg( $str, $context = [] ) { |
| 515 |
global $wgDebugComments, $wgShowDebug; |
| 516 |
|
| 517 |
if ( self::$enabled || $wgDebugComments || $wgShowDebug ) { |
| 518 |
if ( $context ) { |
| 519 |
$prefix = ''; |
| 520 |
if ( isset( $context['prefix'] ) ) { |
| 521 |
$prefix = $context['prefix']; |
| 522 |
} elseif ( isset( $context['channel'] ) && $context['channel'] !== 'wfDebug' ) { |
| 523 |
$prefix = "[{$context['channel']}] "; |
| 524 |
} |
| 525 |
if ( isset( $context['seconds_elapsed'] ) && isset( $context['memory_used'] ) ) { |
| 526 |
$prefix .= "{$context['seconds_elapsed']} {$context['memory_used']} "; |
| 527 |
} |
| 528 |
$str = LegacyLogger::interpolate( $str, $context ); |
| 529 |
$str = $prefix . $str; |
| 530 |
} |
| 531 |
$str = rtrim( UtfNormal\Validator::cleanUp( $str ) ); |
| 532 |
self::$debug[] = $str; |
| 533 |
if ( isset( $context['channel'] ) && $context['channel'] === 'error' ) { |
| 534 |
$message = isset( $context['exception'] ) |
| 535 |
? $context['exception']->getMessage() |
| 536 |
: $str; |
| 537 |
$real = self::parseCallerDescription( $message ); |
| 538 |
if ( $real ) { |
| 539 |
|
| 540 |
$message = $real['message']; |
| 541 |
$caller = $real['func']; |
| 542 |
} else { |
| 543 |
$trace = isset( $context['exception'] ) ? $context['exception']->getTrace() : []; |
| 544 |
if ( ( $trace[5]['function'] ?? null ) === 'wfDeprecated' ) { |
| 545 |
|
| 546 |
$offset = 6; |
| 547 |
} elseif ( ( $trace[1]['function'] ?? null ) === 'trigger_error' ) { |
| 548 |
|
| 549 |
$offset = 2; |
| 550 |
} else { |
| 551 |
|
| 552 |
$offset = 1; |
| 553 |
} |
| 554 |
$frame = $trace[$offset] ?? $trace[0]; |
| 555 |
$caller = ( isset( $frame['class'] ) ? $frame['class'] . '::' : '' ) |
| 556 |
. $frame['function']; |
| 557 |
} |
| 558 |
|
| 559 |
self::$log[] = [ |
| 560 |
'msg' => htmlspecialchars( $message ), |
| 561 |
'type' => 'warn', |
| 562 |
'caller' => $caller, |
| 563 |
]; |
| 564 |
} |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
public static function query( $sql, $function, $runTime, $dbhost ) { |
| 579 |
if ( !self::$enabled ) { |
| 580 |
return false; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
$sql = preg_replace( |
| 586 |
'/( |
| 587 |
[\xC0-\xC1] # Invalid UTF-8 Bytes |
| 588 |
| [\xF5-\xFF] # Invalid UTF-8 Bytes |
| 589 |
| \xE0[\x80-\x9F] # Overlong encoding of prior code point |
| 590 |
| \xF0[\x80-\x8F] # Overlong encoding of prior code point |
| 591 |
| [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start |
| 592 |
| [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start |
| 593 |
| [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start |
| 594 |
| (?<=[\x0-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle |
| 595 |
| (?<![\xC2-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF4] |
| 596 |
| [\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence |
| 597 |
| (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence |
| 598 |
| (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence |
| 599 |
| (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2) |
| 600 |
)/x', |
| 601 |
'■', |
| 602 |
$sql |
| 603 |
); |
| 604 |
|
| 605 |
|
| 606 |
$sql = UtfNormal\Validator::cleanUp( $sql ); |
| 607 |
|
| 608 |
self::$query[] = [ |
| 609 |
'sql' => "$dbhost: $sql", |
| 610 |
'function' => $function, |
| 611 |
'time' => $runTime, |
| 612 |
]; |
| 613 |
|
| 614 |
return true; |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
protected static function getFilesIncluded( IContextSource $context ) { |
| 624 |
$files = get_included_files(); |
| 625 |
$fileList = []; |
| 626 |
foreach ( $files as $file ) { |
| 627 |
|
| 628 |
$size = @filesize( $file ); |
| 629 |
if ( $size === false ) { |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
$size = 0; |
| 634 |
} |
| 635 |
$fileList[] = [ |
| 636 |
'name' => $file, |
| 637 |
'size' => $context->getLanguage()->formatSize( $size ), |
| 638 |
]; |
| 639 |
} |
| 640 |
|
| 641 |
return $fileList; |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
public static function getDebugHTML( IContextSource $context ) { |
| 652 |
global $wgDebugComments; |
| 653 |
|
| 654 |
$html = []; |
| 655 |
|
| 656 |
if ( self::$enabled ) { |
| 657 |
self::log( 'MWDebug output complete' ); |
| 658 |
$debugInfo = self::getDebugInfo( $context ); |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
$html[] = ResourceLoader::makeInlineScript( |
| 663 |
ResourceLoader::makeConfigSetScript( [ 'debugInfo' => $debugInfo ] ) |
| 664 |
); |
| 665 |
} |
| 666 |
|
| 667 |
if ( $wgDebugComments ) { |
| 668 |
$html[] = '<!-- Debug output:'; |
| 669 |
foreach ( self::$debug as $line ) { |
| 670 |
$html[] = htmlspecialchars( $line, ENT_NOQUOTES ); |
| 671 |
} |
| 672 |
$html[] = '-->'; |
| 673 |
} |
| 674 |
|
| 675 |
return WrappedString::join( "\n", $html ); |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
public static function getHTMLDebugLog() { |
| 687 |
global $wgShowDebug; |
| 688 |
|
| 689 |
$html = []; |
| 690 |
if ( $wgShowDebug ) { |
| 691 |
$html[] = Html::openElement( 'div', [ 'id' => 'mw-html-debug-log' ] ); |
| 692 |
$html[] = "<hr />\n<strong>Debug data:</strong><ul id=\"mw-debug-html\">"; |
| 693 |
|
| 694 |
foreach ( self::$debug as $line ) { |
| 695 |
$display = nl2br( htmlspecialchars( trim( $line ) ) ); |
| 696 |
|
| 697 |
$html[] = "<li><code>$display</code></li>"; |
| 698 |
} |
| 699 |
|
| 700 |
$html[] = '</ul>'; |
| 701 |
$html[] = '</div>'; |
| 702 |
} |
| 703 |
return WrappedString::join( "\n", $html ); |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
public static function appendDebugInfoToApiResult( IContextSource $context, ApiResult $result ) { |
| 713 |
if ( !self::$enabled ) { |
| 714 |
return; |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
$obContents = ob_get_contents(); |
| 720 |
if ( $obContents ) { |
| 721 |
$obContentArray = explode( '<br />', $obContents ); |
| 722 |
foreach ( $obContentArray as $obContent ) { |
| 723 |
if ( trim( $obContent ) ) { |
| 724 |
self::debugMsg( Sanitizer::stripAllTags( $obContent ) ); |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
self::log( 'MWDebug output complete' ); |
| 730 |
$debugInfo = self::getDebugInfo( $context ); |
| 731 |
|
| 732 |
ApiResult::setIndexedTagName( $debugInfo, 'debuginfo' ); |
| 733 |
ApiResult::setIndexedTagName( $debugInfo['log'], 'line' ); |
| 734 |
ApiResult::setIndexedTagName( $debugInfo['debugLog'], 'msg' ); |
| 735 |
ApiResult::setIndexedTagName( $debugInfo['queries'], 'query' ); |
| 736 |
ApiResult::setIndexedTagName( $debugInfo['includes'], 'queries' ); |
| 737 |
$result->addValue( null, 'debuginfo', $debugInfo ); |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
public static function getDebugInfo( IContextSource $context ) { |
| 747 |
if ( !self::$enabled ) { |
| 748 |
return []; |
| 749 |
} |
| 750 |
|
| 751 |
$request = $context->getRequest(); |
| 752 |
|
| 753 |
$branch = GitInfo::currentBranch(); |
| 754 |
if ( GitInfo::isSHA1( $branch ) ) { |
| 755 |
|
| 756 |
|
| 757 |
$branch = false; |
| 758 |
} |
| 759 |
|
| 760 |
return [ |
| 761 |
'mwVersion' => MW_VERSION, |
| 762 |
'phpEngine' => 'PHP', |
| 763 |
'phpVersion' => PHP_VERSION, |
| 764 |
'gitRevision' => GitInfo::headSHA1(), |
| 765 |
'gitBranch' => $branch, |
| 766 |
'gitViewUrl' => GitInfo::headViewUrl(), |
| 767 |
'time' => $request->getElapsedTime(), |
| 768 |
'log' => self::$log, |
| 769 |
'debugLog' => self::$debug, |
| 770 |
'queries' => self::$query, |
| 771 |
'request' => [ |
| 772 |
'method' => $request->getMethod(), |
| 773 |
'url' => $request->getRequestURL(), |
| 774 |
'headers' => $request->getAllHeaders(), |
| 775 |
'params' => $request->getValues(), |
| 776 |
], |
| 777 |
'memory' => $context->getLanguage()->formatSize( memory_get_usage() ), |
| 778 |
'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ), |
| 779 |
'includes' => self::getFilesIncluded( $context ), |
| 780 |
]; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
class_alias( MWDebug::class, 'MWDebug' ); |