MediaWiki
REL1_24
|
00001 <?php 00033 class MWDebug { 00039 protected static $log = array(); 00040 00046 protected static $debug = array(); 00047 00053 protected static $query = array(); 00054 00060 protected static $enabled = false; 00061 00068 protected static $deprecationWarnings = array(); 00069 00076 public static function init() { 00077 self::$enabled = true; 00078 } 00079 00087 public static function addModules( OutputPage $out ) { 00088 if ( self::$enabled ) { 00089 $out->addModules( 'mediawiki.debug.init' ); 00090 } 00091 } 00092 00101 public static function log( $str ) { 00102 if ( !self::$enabled ) { 00103 return; 00104 } 00105 00106 self::$log[] = array( 00107 'msg' => htmlspecialchars( $str ), 00108 'type' => 'log', 00109 'caller' => wfGetCaller(), 00110 ); 00111 } 00112 00118 public static function getLog() { 00119 return self::$log; 00120 } 00121 00126 public static function clearLog() { 00127 self::$log = array(); 00128 self::$deprecationWarnings = array(); 00129 } 00130 00144 public static function warning( $msg, $callerOffset = 1, $level = E_USER_NOTICE, $log = 'auto' ) { 00145 global $wgDevelopmentWarnings; 00146 00147 if ( $log === 'auto' && !$wgDevelopmentWarnings ) { 00148 $log = 'debug'; 00149 } 00150 00151 if ( $log === 'debug' ) { 00152 $level = false; 00153 } 00154 00155 $callerDescription = self::getCallerDescription( $callerOffset ); 00156 00157 self::sendMessage( $msg, $callerDescription, 'warning', $level ); 00158 00159 if ( self::$enabled ) { 00160 self::$log[] = array( 00161 'msg' => htmlspecialchars( $msg ), 00162 'type' => 'warn', 00163 'caller' => $callerDescription['func'], 00164 ); 00165 } 00166 } 00167 00186 public static function deprecated( $function, $version = false, 00187 $component = false, $callerOffset = 2 00188 ) { 00189 $callerDescription = self::getCallerDescription( $callerOffset ); 00190 $callerFunc = $callerDescription['func']; 00191 00192 $sendToLog = true; 00193 00194 // Check to see if there already was a warning about this function 00195 if ( isset( self::$deprecationWarnings[$function][$callerFunc] ) ) { 00196 return; 00197 } elseif ( isset( self::$deprecationWarnings[$function] ) ) { 00198 if ( self::$enabled ) { 00199 $sendToLog = false; 00200 } else { 00201 return; 00202 } 00203 } 00204 00205 self::$deprecationWarnings[$function][$callerFunc] = true; 00206 00207 if ( $version ) { 00208 global $wgDeprecationReleaseLimit; 00209 if ( $wgDeprecationReleaseLimit && $component === false ) { 00210 # Strip -* off the end of $version so that branches can use the 00211 # format #.##-branchname to avoid issues if the branch is merged into 00212 # a version of MediaWiki later than what it was branched from 00213 $comparableVersion = preg_replace( '/-.*$/', '', $version ); 00214 00215 # If the comparableVersion is larger than our release limit then 00216 # skip the warning message for the deprecation 00217 if ( version_compare( $wgDeprecationReleaseLimit, $comparableVersion, '<' ) ) { 00218 $sendToLog = false; 00219 } 00220 } 00221 00222 $component = $component === false ? 'MediaWiki' : $component; 00223 $msg = "Use of $function was deprecated in $component $version."; 00224 } else { 00225 $msg = "Use of $function is deprecated."; 00226 } 00227 00228 if ( $sendToLog ) { 00229 global $wgDevelopmentWarnings; // we could have a more specific $wgDeprecationWarnings setting. 00230 self::sendMessage( 00231 $msg, 00232 $callerDescription, 00233 'deprecated', 00234 $wgDevelopmentWarnings ? E_USER_DEPRECATED : false 00235 ); 00236 } 00237 00238 if ( self::$enabled ) { 00239 $logMsg = htmlspecialchars( $msg ) . 00240 Html::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ), 00241 Html::element( 'span', array(), 'Backtrace:' ) . wfBacktrace() 00242 ); 00243 00244 self::$log[] = array( 00245 'msg' => $logMsg, 00246 'type' => 'deprecated', 00247 'caller' => $callerFunc, 00248 ); 00249 } 00250 } 00251 00259 private static function getCallerDescription( $callerOffset ) { 00260 $callers = wfDebugBacktrace(); 00261 00262 if ( isset( $callers[$callerOffset] ) ) { 00263 $callerfile = $callers[$callerOffset]; 00264 if ( isset( $callerfile['file'] ) && isset( $callerfile['line'] ) ) { 00265 $file = $callerfile['file'] . ' at line ' . $callerfile['line']; 00266 } else { 00267 $file = '(internal function)'; 00268 } 00269 } else { 00270 $file = '(unknown location)'; 00271 } 00272 00273 if ( isset( $callers[$callerOffset + 1] ) ) { 00274 $callerfunc = $callers[$callerOffset + 1]; 00275 $func = ''; 00276 if ( isset( $callerfunc['class'] ) ) { 00277 $func .= $callerfunc['class'] . '::'; 00278 } 00279 if ( isset( $callerfunc['function'] ) ) { 00280 $func .= $callerfunc['function']; 00281 } 00282 } else { 00283 $func = 'unknown'; 00284 } 00285 00286 return array( 'file' => $file, 'func' => $func ); 00287 } 00288 00298 private static function sendMessage( $msg, $caller, $group, $level ) { 00299 $msg .= ' [Called from ' . $caller['func'] . ' in ' . $caller['file'] . ']'; 00300 00301 if ( $level !== false ) { 00302 trigger_error( $msg, $level ); 00303 } 00304 00305 wfDebugLog( $group, $msg, 'log' ); 00306 } 00307 00315 public static function debugMsg( $str ) { 00316 global $wgDebugComments, $wgShowDebug; 00317 00318 if ( self::$enabled || $wgDebugComments || $wgShowDebug ) { 00319 self::$debug[] = rtrim( UtfNormal::cleanUp( $str ) ); 00320 } 00321 } 00322 00333 public static function query( $sql, $function, $isMaster ) { 00334 if ( !self::$enabled ) { 00335 return -1; 00336 } 00337 00338 // Replace invalid UTF-8 chars with a square UTF-8 character 00339 // This prevents json_encode from erroring out due to binary SQL data 00340 $sql = preg_replace( 00341 '/( 00342 [\xC0-\xC1] # Invalid UTF-8 Bytes 00343 | [\xF5-\xFF] # Invalid UTF-8 Bytes 00344 | \xE0[\x80-\x9F] # Overlong encoding of prior code point 00345 | \xF0[\x80-\x8F] # Overlong encoding of prior code point 00346 | [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start 00347 | [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start 00348 | [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start 00349 | (?<=[\x0-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle 00350 | (?<![\xC2-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF4] 00351 |[\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence 00352 | (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence 00353 | (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence 00354 | (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2) 00355 )/x', 00356 '■', 00357 $sql 00358 ); 00359 00360 self::$query[] = array( 00361 'sql' => $sql, 00362 'function' => $function, 00363 'master' => (bool)$isMaster, 00364 'time' => 0.0, 00365 '_start' => microtime( true ), 00366 ); 00367 00368 return count( self::$query ) - 1; 00369 } 00370 00377 public static function queryTime( $id ) { 00378 if ( $id === -1 || !self::$enabled ) { 00379 return; 00380 } 00381 00382 self::$query[$id]['time'] = microtime( true ) - self::$query[$id]['_start']; 00383 unset( self::$query[$id]['_start'] ); 00384 } 00385 00392 protected static function getFilesIncluded( IContextSource $context ) { 00393 $files = get_included_files(); 00394 $fileList = array(); 00395 foreach ( $files as $file ) { 00396 $size = filesize( $file ); 00397 $fileList[] = array( 00398 'name' => $file, 00399 'size' => $context->getLanguage()->formatSize( $size ), 00400 ); 00401 } 00402 00403 return $fileList; 00404 } 00405 00413 public static function getDebugHTML( IContextSource $context ) { 00414 global $wgDebugComments; 00415 00416 $html = ''; 00417 00418 if ( self::$enabled ) { 00419 MWDebug::log( 'MWDebug output complete' ); 00420 $debugInfo = self::getDebugInfo( $context ); 00421 00422 // Cannot use OutputPage::addJsConfigVars because those are already outputted 00423 // by the time this method is called. 00424 $html = Html::inlineScript( 00425 ResourceLoader::makeLoaderConditionalScript( 00426 ResourceLoader::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) ) 00427 ) 00428 ); 00429 } 00430 00431 if ( $wgDebugComments ) { 00432 $html .= "<!-- Debug output:\n" . 00433 htmlspecialchars( implode( "\n", self::$debug ) ) . 00434 "\n\n-->"; 00435 } 00436 00437 return $html; 00438 } 00439 00448 public static function getHTMLDebugLog() { 00449 global $wgDebugTimestamps, $wgShowDebug; 00450 00451 if ( !$wgShowDebug ) { 00452 return ''; 00453 } 00454 00455 $curIdent = 0; 00456 $ret = "\n<hr />\n<strong>Debug data:</strong><ul id=\"mw-debug-html\">\n<li>"; 00457 00458 foreach ( self::$debug as $line ) { 00459 $pre = ''; 00460 if ( $wgDebugTimestamps ) { 00461 $matches = array(); 00462 if ( preg_match( '/^(\d+\.\d+ {1,3}\d+.\dM\s{2})/', $line, $matches ) ) { 00463 $pre = $matches[1]; 00464 $line = substr( $line, strlen( $pre ) ); 00465 } 00466 } 00467 $display = ltrim( $line ); 00468 $ident = strlen( $line ) - strlen( $display ); 00469 $diff = $ident - $curIdent; 00470 00471 $display = $pre . $display; 00472 if ( $display == '' ) { 00473 $display = "\xc2\xa0"; 00474 } 00475 00476 if ( !$ident 00477 && $diff < 0 00478 && substr( $display, 0, 9 ) != 'Entering ' 00479 && substr( $display, 0, 8 ) != 'Exiting ' 00480 ) { 00481 $ident = $curIdent; 00482 $diff = 0; 00483 $display = '<span style="background:yellow;">' . 00484 nl2br( htmlspecialchars( $display ) ) . '</span>'; 00485 } else { 00486 $display = nl2br( htmlspecialchars( $display ) ); 00487 } 00488 00489 if ( $diff < 0 ) { 00490 $ret .= str_repeat( "</li></ul>\n", -$diff ) . "</li><li>\n"; 00491 } elseif ( $diff == 0 ) { 00492 $ret .= "</li><li>\n"; 00493 } else { 00494 $ret .= str_repeat( "<ul><li>\n", $diff ); 00495 } 00496 $ret .= "<code>$display</code>\n"; 00497 00498 $curIdent = $ident; 00499 } 00500 00501 $ret .= str_repeat( '</li></ul>', $curIdent ) . "</li>\n</ul>\n"; 00502 00503 return $ret; 00504 } 00505 00512 public static function appendDebugInfoToApiResult( IContextSource $context, ApiResult $result ) { 00513 if ( !self::$enabled ) { 00514 return; 00515 } 00516 00517 // output errors as debug info, when display_errors is on 00518 // this is necessary for all non html output of the api, because that clears all errors first 00519 $obContents = ob_get_contents(); 00520 if ( $obContents ) { 00521 $obContentArray = explode( '<br />', $obContents ); 00522 foreach ( $obContentArray as $obContent ) { 00523 if ( trim( $obContent ) ) { 00524 self::debugMsg( Sanitizer::stripAllTags( $obContent ) ); 00525 } 00526 } 00527 } 00528 00529 MWDebug::log( 'MWDebug output complete' ); 00530 $debugInfo = self::getDebugInfo( $context ); 00531 00532 $result->setIndexedTagName( $debugInfo, 'debuginfo' ); 00533 $result->setIndexedTagName( $debugInfo['log'], 'line' ); 00534 $result->setIndexedTagName( $debugInfo['debugLog'], 'msg' ); 00535 $result->setIndexedTagName( $debugInfo['queries'], 'query' ); 00536 $result->setIndexedTagName( $debugInfo['includes'], 'queries' ); 00537 $result->setIndexedTagName( $debugInfo['profile'], 'function' ); 00538 $result->addValue( null, 'debuginfo', $debugInfo ); 00539 } 00540 00547 public static function getDebugInfo( IContextSource $context ) { 00548 if ( !self::$enabled ) { 00549 return array(); 00550 } 00551 00552 global $wgVersion, $wgRequestTime; 00553 $request = $context->getRequest(); 00554 00555 // HHVM's reported memory usage from memory_get_peak_usage() 00556 // is not useful when passing false, but we continue passing 00557 // false for consistency of historical data in zend. 00558 // see: https://github.com/facebook/hhvm/issues/2257#issuecomment-39362246 00559 $realMemoryUsage = wfIsHHVM(); 00560 00561 return array( 00562 'mwVersion' => $wgVersion, 00563 'phpEngine' => wfIsHHVM() ? 'HHVM' : 'PHP', 00564 'phpVersion' => wfIsHHVM() ? HHVM_VERSION : PHP_VERSION, 00565 'gitRevision' => GitInfo::headSHA1(), 00566 'gitBranch' => GitInfo::currentBranch(), 00567 'gitViewUrl' => GitInfo::headViewUrl(), 00568 'time' => microtime( true ) - $wgRequestTime, 00569 'log' => self::$log, 00570 'debugLog' => self::$debug, 00571 'queries' => self::$query, 00572 'request' => array( 00573 'method' => $request->getMethod(), 00574 'url' => $request->getRequestURL(), 00575 'headers' => $request->getAllHeaders(), 00576 'params' => $request->getValues(), 00577 ), 00578 'memory' => $context->getLanguage()->formatSize( memory_get_usage( $realMemoryUsage ) ), 00579 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage( $realMemoryUsage ) ), 00580 'includes' => self::getFilesIncluded( $context ), 00581 'profile' => Profiler::instance()->getRawData(), 00582 ); 00583 } 00584 }