MediaWiki
REL1_20
|
00001 <?php 00033 class MWDebug { 00034 00040 protected static $log = array(); 00041 00047 protected static $debug = array(); 00048 00054 protected static $query = array(); 00055 00061 protected static $enabled = false; 00062 00069 protected static $deprecationWarnings = array(); 00070 00077 public static function init() { 00078 self::$enabled = true; 00079 } 00080 00088 public static function addModules( OutputPage $out ) { 00089 if ( self::$enabled ) { 00090 $out->addModules( 'mediawiki.debug.init' ); 00091 } 00092 } 00093 00102 public static function log( $str ) { 00103 if ( !self::$enabled ) { 00104 return; 00105 } 00106 00107 self::$log[] = array( 00108 'msg' => htmlspecialchars( $str ), 00109 'type' => 'log', 00110 'caller' => wfGetCaller(), 00111 ); 00112 } 00113 00119 public static function getLog() { 00120 return self::$log; 00121 } 00122 00127 public static function clearLog() { 00128 self::$log = array(); 00129 self::$deprecationWarnings = array(); 00130 } 00131 00140 public static function warning( $msg, $callerOffset = 1, $level = E_USER_NOTICE ) { 00141 $callerDescription = self::getCallerDescription( $callerOffset ); 00142 00143 self::sendWarning( $msg, $callerDescription, $level ); 00144 00145 if ( self::$enabled ) { 00146 self::$log[] = array( 00147 'msg' => htmlspecialchars( $msg ), 00148 'type' => 'warn', 00149 'caller' => $callerDescription['func'], 00150 ); 00151 } 00152 } 00153 00173 public static function deprecated( $function, $version = false, $component = false, $callerOffset = 2 ) { 00174 $callerDescription = self::getCallerDescription( $callerOffset ); 00175 $callerFunc = $callerDescription['func']; 00176 00177 $sendToLog = true; 00178 00179 // Check to see if there already was a warning about this function 00180 if ( isset( self::$deprecationWarnings[$function][$callerFunc] ) ) { 00181 return; 00182 } elseif ( isset( self::$deprecationWarnings[$function] ) ) { 00183 if ( self::$enabled ) { 00184 $sendToLog = false; 00185 } else { 00186 return; 00187 } 00188 } 00189 00190 self::$deprecationWarnings[$function][$callerFunc] = true; 00191 00192 if ( $version ) { 00193 global $wgDeprecationReleaseLimit; 00194 if ( $wgDeprecationReleaseLimit && $component === false ) { 00195 # Strip -* off the end of $version so that branches can use the 00196 # format #.##-branchname to avoid issues if the branch is merged into 00197 # a version of MediaWiki later than what it was branched from 00198 $comparableVersion = preg_replace( '/-.*$/', '', $version ); 00199 00200 # If the comparableVersion is larger than our release limit then 00201 # skip the warning message for the deprecation 00202 if ( version_compare( $wgDeprecationReleaseLimit, $comparableVersion, '<' ) ) { 00203 $sendToLog = false; 00204 } 00205 } 00206 00207 $component = $component === false ? 'MediaWiki' : $component; 00208 $msg = "Use of $function was deprecated in $component $version."; 00209 } else { 00210 $msg = "Use of $function is deprecated."; 00211 } 00212 00213 if ( $sendToLog ) { 00214 self::sendWarning( $msg, $callerDescription, E_USER_DEPRECATED ); 00215 } 00216 00217 if ( self::$enabled ) { 00218 $logMsg = htmlspecialchars( $msg ) . 00219 Html::rawElement( 'div', array( 'class' => 'mw-debug-backtrace' ), 00220 Html::element( 'span', array(), 'Backtrace:' ) . wfBacktrace() 00221 ); 00222 00223 self::$log[] = array( 00224 'msg' => $logMsg, 00225 'type' => 'deprecated', 00226 'caller' => $callerFunc, 00227 ); 00228 } 00229 } 00230 00238 private static function getCallerDescription( $callerOffset ) { 00239 $callers = wfDebugBacktrace(); 00240 00241 if ( isset( $callers[$callerOffset] ) ) { 00242 $callerfile = $callers[$callerOffset]; 00243 if ( isset( $callerfile['file'] ) && isset( $callerfile['line'] ) ) { 00244 $file = $callerfile['file'] . ' at line ' . $callerfile['line']; 00245 } else { 00246 $file = '(internal function)'; 00247 } 00248 } else { 00249 $file = '(unknown location)'; 00250 } 00251 00252 if ( isset( $callers[$callerOffset + 1] ) ) { 00253 $callerfunc = $callers[$callerOffset + 1]; 00254 $func = ''; 00255 if ( isset( $callerfunc['class'] ) ) { 00256 $func .= $callerfunc['class'] . '::'; 00257 } 00258 if ( isset( $callerfunc['function'] ) ) { 00259 $func .= $callerfunc['function']; 00260 } 00261 } else { 00262 $func = 'unknown'; 00263 } 00264 00265 return array( 'file' => $file, 'func' => $func ); 00266 } 00267 00276 private static function sendWarning( $msg, $caller, $level ) { 00277 global $wgDevelopmentWarnings; 00278 00279 $msg .= ' [Called from ' . $caller['func'] . ' in ' . $caller['file'] . ']'; 00280 00281 if ( $wgDevelopmentWarnings ) { 00282 trigger_error( $msg, $level ); 00283 } else { 00284 wfDebug( "$msg\n" ); 00285 } 00286 } 00287 00295 public static function debugMsg( $str ) { 00296 global $wgDebugComments, $wgShowDebug; 00297 00298 if ( self::$enabled || $wgDebugComments || $wgShowDebug ) { 00299 self::$debug[] = rtrim( $str ); 00300 } 00301 } 00302 00313 public static function query( $sql, $function, $isMaster ) { 00314 if ( !self::$enabled ) { 00315 return -1; 00316 } 00317 00318 self::$query[] = array( 00319 'sql' => $sql, 00320 'function' => $function, 00321 'master' => (bool) $isMaster, 00322 'time' => 0.0, 00323 '_start' => microtime( true ), 00324 ); 00325 00326 return count( self::$query ) - 1; 00327 } 00328 00335 public static function queryTime( $id ) { 00336 if ( $id === -1 || !self::$enabled ) { 00337 return; 00338 } 00339 00340 self::$query[$id]['time'] = microtime( true ) - self::$query[$id]['_start']; 00341 unset( self::$query[$id]['_start'] ); 00342 } 00343 00350 protected static function getFilesIncluded( IContextSource $context ) { 00351 $files = get_included_files(); 00352 $fileList = array(); 00353 foreach ( $files as $file ) { 00354 $size = filesize( $file ); 00355 $fileList[] = array( 00356 'name' => $file, 00357 'size' => $context->getLanguage()->formatSize( $size ), 00358 ); 00359 } 00360 00361 return $fileList; 00362 } 00363 00371 public static function getDebugHTML( IContextSource $context ) { 00372 global $wgDebugComments; 00373 00374 $html = ''; 00375 00376 if ( self::$enabled ) { 00377 MWDebug::log( 'MWDebug output complete' ); 00378 $debugInfo = self::getDebugInfo( $context ); 00379 00380 // Cannot use OutputPage::addJsConfigVars because those are already outputted 00381 // by the time this method is called. 00382 $html = Html::inlineScript( 00383 ResourceLoader::makeLoaderConditionalScript( 00384 ResourceLoader::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) ) 00385 ) 00386 ); 00387 } 00388 00389 if ( $wgDebugComments ) { 00390 $html .= "<!-- Debug output:\n" . 00391 htmlspecialchars( implode( "\n", self::$debug ) ) . 00392 "\n\n-->"; 00393 } 00394 00395 return $html; 00396 } 00397 00406 public static function getHTMLDebugLog() { 00407 global $wgDebugTimestamps, $wgShowDebug; 00408 00409 if ( !$wgShowDebug ) { 00410 return ''; 00411 } 00412 00413 $curIdent = 0; 00414 $ret = "\n<hr />\n<strong>Debug data:</strong><ul id=\"mw-debug-html\">\n<li>"; 00415 00416 foreach ( self::$debug as $line ) { 00417 $pre = ''; 00418 if ( $wgDebugTimestamps ) { 00419 $matches = array(); 00420 if ( preg_match( '/^(\d+\.\d+ {1,3}\d+.\dM\s{2})/', $line, $matches ) ) { 00421 $pre = $matches[1]; 00422 $line = substr( $line, strlen( $pre ) ); 00423 } 00424 } 00425 $display = ltrim( $line ); 00426 $ident = strlen( $line ) - strlen( $display ); 00427 $diff = $ident - $curIdent; 00428 00429 $display = $pre . $display; 00430 if ( $display == '' ) { 00431 $display = "\xc2\xa0"; 00432 } 00433 00434 if ( !$ident && $diff < 0 && substr( $display, 0, 9 ) != 'Entering ' && substr( $display, 0, 8 ) != 'Exiting ' ) { 00435 $ident = $curIdent; 00436 $diff = 0; 00437 $display = '<span style="background:yellow;">' . nl2br( htmlspecialchars( $display ) ) . '</span>'; 00438 } else { 00439 $display = nl2br( htmlspecialchars( $display ) ); 00440 } 00441 00442 if ( $diff < 0 ) { 00443 $ret .= str_repeat( "</li></ul>\n", -$diff ) . "</li><li>\n"; 00444 } elseif ( $diff == 0 ) { 00445 $ret .= "</li><li>\n"; 00446 } else { 00447 $ret .= str_repeat( "<ul><li>\n", $diff ); 00448 } 00449 $ret .= "<tt>$display</tt>\n"; 00450 00451 $curIdent = $ident; 00452 } 00453 00454 $ret .= str_repeat( '</li></ul>', $curIdent ) . "</li>\n</ul>\n"; 00455 00456 return $ret; 00457 } 00458 00465 public static function appendDebugInfoToApiResult( IContextSource $context, ApiResult $result ) { 00466 if ( !self::$enabled ) { 00467 return; 00468 } 00469 00470 // output errors as debug info, when display_errors is on 00471 // this is necessary for all non html output of the api, because that clears all errors first 00472 $obContents = ob_get_contents(); 00473 if( $obContents ) { 00474 $obContentArray = explode( '<br />', $obContents ); 00475 foreach( $obContentArray as $obContent ) { 00476 if( trim( $obContent ) ) { 00477 self::debugMsg( Sanitizer::stripAllTags( $obContent ) ); 00478 } 00479 } 00480 } 00481 00482 MWDebug::log( 'MWDebug output complete' ); 00483 $debugInfo = self::getDebugInfo( $context ); 00484 00485 $result->setIndexedTagName( $debugInfo, 'debuginfo' ); 00486 $result->setIndexedTagName( $debugInfo['log'], 'line' ); 00487 foreach( $debugInfo['debugLog'] as $index => $debugLogText ) { 00488 $vals = array(); 00489 ApiResult::setContent( $vals, $debugLogText ); 00490 $debugInfo['debugLog'][$index] = $vals; //replace 00491 } 00492 $result->setIndexedTagName( $debugInfo['debugLog'], 'msg' ); 00493 $result->setIndexedTagName( $debugInfo['queries'], 'query' ); 00494 $result->setIndexedTagName( $debugInfo['includes'], 'queries' ); 00495 $result->addValue( array(), 'debuginfo', $debugInfo ); 00496 } 00497 00504 public static function getDebugInfo( IContextSource $context ) { 00505 if ( !self::$enabled ) { 00506 return array(); 00507 } 00508 00509 global $wgVersion, $wgRequestTime; 00510 $request = $context->getRequest(); 00511 return array( 00512 'mwVersion' => $wgVersion, 00513 'phpVersion' => PHP_VERSION, 00514 'gitRevision' => GitInfo::headSHA1(), 00515 'gitBranch' => GitInfo::currentBranch(), 00516 'gitViewUrl' => GitInfo::headViewUrl(), 00517 'time' => microtime( true ) - $wgRequestTime, 00518 'log' => self::$log, 00519 'debugLog' => self::$debug, 00520 'queries' => self::$query, 00521 'request' => array( 00522 'method' => $request->getMethod(), 00523 'url' => $request->getRequestURL(), 00524 'headers' => $request->getAllHeaders(), 00525 'params' => $request->getValues(), 00526 ), 00527 'memory' => $context->getLanguage()->formatSize( memory_get_usage() ), 00528 'memoryPeak' => $context->getLanguage()->formatSize( memory_get_peak_usage() ), 00529 'includes' => self::getFilesIncluded( $context ), 00530 ); 00531 } 00532 }