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