MediaWiki
REL1_23
|
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 00187 public static function deprecated( $function, $version = false, 00188 $component = false, $callerOffset = 2 00189 ) { 00190 $callerDescription = self::getCallerDescription( $callerOffset ); 00191 $callerFunc = $callerDescription['func']; 00192 00193 $sendToLog = true; 00194 00195 // Check to see if there already was a warning about this function 00196 if ( isset( self::$deprecationWarnings[$function][$callerFunc] ) ) { 00197 return; 00198 } elseif ( isset( self::$deprecationWarnings[$function] ) ) { 00199 if ( self::$enabled ) { 00200 $sendToLog = false; 00201 } else { 00202 return; 00203 } 00204 } 00205 00206 self::$deprecationWarnings[$function][$callerFunc] = true; 00207 00208 if ( $version ) { 00209 global $wgDeprecationReleaseLimit; 00210 if ( $wgDeprecationReleaseLimit && $component === false ) { 00211 # Strip -* off the end of $version so that branches can use the 00212 # format #.##-branchname to avoid issues if the branch is merged into 00213 # a version of MediaWiki later than what it was branched from 00214 $comparableVersion = preg_replace( '/-.*$/', '', $version ); 00215 00216 # If the comparableVersion is larger than our release limit then 00217 # skip the warning message for the deprecation 00218 if ( version_compare( $wgDeprecationReleaseLimit, $comparableVersion, '<' ) ) { 00219 $sendToLog = false; 00220 } 00221 } 00222 00223 $component = $component === false ? 'MediaWiki' : $component; 00224 $msg = "Use of $function was deprecated in $component $version."; 00225 } else { 00226 $msg = "Use of $function is deprecated."; 00227 } 00228 00229 if ( $sendToLog ) { 00230 global $wgDevelopmentWarnings; // we could have a more specific $wgDeprecationWarnings setting. 00231 self::sendMessage( 00232 $msg, 00233 $callerDescription, 00234 'deprecated', 00235 $wgDevelopmentWarnings ? E_USER_DEPRECATED : false 00236 ); 00237 } 00238 00239 if ( self::$enabled ) { 00240 $logMsg = htmlspecialchars( $msg ) . 00241 Html::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ), 00242 Html::element( 'span', array(), 'Backtrace:' ) . wfBacktrace() 00243 ); 00244 00245 self::$log[] = array( 00246 'msg' => $logMsg, 00247 'type' => 'deprecated', 00248 'caller' => $callerFunc, 00249 ); 00250 } 00251 } 00252 00260 private static function getCallerDescription( $callerOffset ) { 00261 $callers = wfDebugBacktrace(); 00262 00263 if ( isset( $callers[$callerOffset] ) ) { 00264 $callerfile = $callers[$callerOffset]; 00265 if ( isset( $callerfile['file'] ) && isset( $callerfile['line'] ) ) { 00266 $file = $callerfile['file'] . ' at line ' . $callerfile['line']; 00267 } else { 00268 $file = '(internal function)'; 00269 } 00270 } else { 00271 $file = '(unknown location)'; 00272 } 00273 00274 if ( isset( $callers[$callerOffset + 1] ) ) { 00275 $callerfunc = $callers[$callerOffset + 1]; 00276 $func = ''; 00277 if ( isset( $callerfunc['class'] ) ) { 00278 $func .= $callerfunc['class'] . '::'; 00279 } 00280 if ( isset( $callerfunc['function'] ) ) { 00281 $func .= $callerfunc['function']; 00282 } 00283 } else { 00284 $func = 'unknown'; 00285 } 00286 00287 return array( 'file' => $file, 'func' => $func ); 00288 } 00289 00299 private static function sendMessage( $msg, $caller, $group, $level ) { 00300 $msg .= ' [Called from ' . $caller['func'] . ' in ' . $caller['file'] . ']'; 00301 00302 if ( $level !== false ) { 00303 trigger_error( $msg, $level ); 00304 } 00305 00306 wfDebugLog( $group, $msg, 'log' ); 00307 } 00308 00316 public static function debugMsg( $str ) { 00317 global $wgDebugComments, $wgShowDebug; 00318 00319 if ( self::$enabled || $wgDebugComments || $wgShowDebug ) { 00320 self::$debug[] = rtrim( UtfNormal::cleanUp( $str ) ); 00321 } 00322 } 00323 00334 public static function query( $sql, $function, $isMaster ) { 00335 if ( !self::$enabled ) { 00336 return -1; 00337 } 00338 00339 self::$query[] = array( 00340 'sql' => $sql, 00341 'function' => $function, 00342 'master' => (bool)$isMaster, 00343 'time' => 0.0, 00344 '_start' => microtime( true ), 00345 ); 00346 00347 return count( self::$query ) - 1; 00348 } 00349 00356 public static function queryTime( $id ) { 00357 if ( $id === -1 || !self::$enabled ) { 00358 return; 00359 } 00360 00361 self::$query[$id]['time'] = microtime( true ) - self::$query[$id]['_start']; 00362 unset( self::$query[$id]['_start'] ); 00363 } 00364 00371 protected static function getFilesIncluded( IContextSource $context ) { 00372 $files = get_included_files(); 00373 $fileList = array(); 00374 foreach ( $files as $file ) { 00375 $size = filesize( $file ); 00376 $fileList[] = array( 00377 'name' => $file, 00378 'size' => $context->getLanguage()->formatSize( $size ), 00379 ); 00380 } 00381 00382 return $fileList; 00383 } 00384 00392 public static function getDebugHTML( IContextSource $context ) { 00393 global $wgDebugComments; 00394 00395 $html = ''; 00396 00397 if ( self::$enabled ) { 00398 MWDebug::log( 'MWDebug output complete' ); 00399 $debugInfo = self::getDebugInfo( $context ); 00400 00401 // Cannot use OutputPage::addJsConfigVars because those are already outputted 00402 // by the time this method is called. 00403 $html = Html::inlineScript( 00404 ResourceLoader::makeLoaderConditionalScript( 00405 ResourceLoader::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) ) 00406 ) 00407 ); 00408 } 00409 00410 if ( $wgDebugComments ) { 00411 $html .= "<!-- Debug output:\n" . 00412 htmlspecialchars( implode( "\n", self::$debug ) ) . 00413 "\n\n-->"; 00414 } 00415 00416 return $html; 00417 } 00418 00427 public static function getHTMLDebugLog() { 00428 global $wgDebugTimestamps, $wgShowDebug; 00429 00430 if ( !$wgShowDebug ) { 00431 return ''; 00432 } 00433 00434 $curIdent = 0; 00435 $ret = "\n<hr />\n<strong>Debug data:</strong><ul id=\"mw-debug-html\">\n<li>"; 00436 00437 foreach ( self::$debug as $line ) { 00438 $pre = ''; 00439 if ( $wgDebugTimestamps ) { 00440 $matches = array(); 00441 if ( preg_match( '/^(\d+\.\d+ {1,3}\d+.\dM\s{2})/', $line, $matches ) ) { 00442 $pre = $matches[1]; 00443 $line = substr( $line, strlen( $pre ) ); 00444 } 00445 } 00446 $display = ltrim( $line ); 00447 $ident = strlen( $line ) - strlen( $display ); 00448 $diff = $ident - $curIdent; 00449 00450 $display = $pre . $display; 00451 if ( $display == '' ) { 00452 $display = "\xc2\xa0"; 00453 } 00454 00455 if ( !$ident 00456 && $diff < 0 00457 && substr( $display, 0, 9 ) != 'Entering ' 00458 && substr( $display, 0, 8 ) != 'Exiting ' 00459 ) { 00460 $ident = $curIdent; 00461 $diff = 0; 00462 $display = '<span style="background:yellow;">' . 00463 nl2br( htmlspecialchars( $display ) ) . '</span>'; 00464 } else { 00465 $display = nl2br( htmlspecialchars( $display ) ); 00466 } 00467 00468 if ( $diff < 0 ) { 00469 $ret .= str_repeat( "</li></ul>\n", -$diff ) . "</li><li>\n"; 00470 } elseif ( $diff == 0 ) { 00471 $ret .= "</li><li>\n"; 00472 } else { 00473 $ret .= str_repeat( "<ul><li>\n", $diff ); 00474 } 00475 $ret .= "<code>$display</code>\n"; 00476 00477 $curIdent = $ident; 00478 } 00479 00480 $ret .= str_repeat( '</li></ul>', $curIdent ) . "</li>\n</ul>\n"; 00481 00482 return $ret; 00483 } 00484 00491 public static function appendDebugInfoToApiResult( IContextSource $context, ApiResult $result ) { 00492 if ( !self::$enabled ) { 00493 return; 00494 } 00495 00496 // output errors as debug info, when display_errors is on 00497 // this is necessary for all non html output of the api, because that clears all errors first 00498 $obContents = ob_get_contents(); 00499 if ( $obContents ) { 00500 $obContentArray = explode( '<br />', $obContents ); 00501 foreach ( $obContentArray as $obContent ) { 00502 if ( trim( $obContent ) ) { 00503 self::debugMsg( Sanitizer::stripAllTags( $obContent ) ); 00504 } 00505 } 00506 } 00507 00508 MWDebug::log( 'MWDebug output complete' ); 00509 $debugInfo = self::getDebugInfo( $context ); 00510 00511 $result->setIndexedTagName( $debugInfo, 'debuginfo' ); 00512 $result->setIndexedTagName( $debugInfo['log'], 'line' ); 00513 $result->setIndexedTagName( $debugInfo['debugLog'], 'msg' ); 00514 $result->setIndexedTagName( $debugInfo['queries'], 'query' ); 00515 $result->setIndexedTagName( $debugInfo['includes'], 'queries' ); 00516 $result->addValue( null, 'debuginfo', $debugInfo ); 00517 } 00518 00525 public static function getDebugInfo( IContextSource $context ) { 00526 if ( !self::$enabled ) { 00527 return array(); 00528 } 00529 00530 global $wgVersion, $wgRequestTime; 00531 $request = $context->getRequest(); 00532 00533 // HHVM's reported memory usage from memory_get_peak_usage() 00534 // is not useful when passing false, but we continue passing 00535 // false for consistency of historical data in zend. 00536 // see: https://github.com/facebook/hhvm/issues/2257#issuecomment-39362246 00537 $realMemoryUsage = wfIsHHVM(); 00538 00539 return array( 00540 'mwVersion' => $wgVersion, 00541 'phpVersion' => PHP_VERSION, 00542 'gitRevision' => GitInfo::headSHA1(), 00543 'gitBranch' => GitInfo::currentBranch(), 00544 'gitViewUrl' => GitInfo::headViewUrl(), 00545 'time' => microtime( true ) - $wgRequestTime, 00546 'log' => self::$log, 00547 'debugLog' => self::$debug, 00548 'queries' => self::$query, 00549 'request' => array( 00550 'method' => $request->getMethod(), 00551 'url' => $request->getRequestURL(), 00552 'headers' => $request->getAllHeaders(), 00553 'params' => $request->getValues(), 00554 ), 00555 'memory' => $context->getLanguage()->formatSize( memory_get_usage( $realMemoryUsage ) ), 00556 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage( $realMemoryUsage ) ), 00557 'includes' => self::getFilesIncluded( $context ), 00558 'profile' => Profiler::instance()->getRawData(), 00559 ); 00560 } 00561 }