转自:donnki 的个人空间 http://www.phpchina.com/52027/viewspace_20041.html
源文件的页面说明
# This does the initial setup for a web request. It does some security checks, # starts the profiler and loads the configuration, and optionally loads # Setup.php depending on whether MW_NO_SETUP is defined.
WebStart.php初始化流程分析(一)
作为wiki入口的index.php第一次require的页面,WebStart首先对当前请求的一些参数做检查,即检查超级全局变量$_REQUEST是否已经设定了下面数组中的值。
$verboten = array( 'GLOBALS', '_SERVER', 'HTTP_SERVER_VARS', '_GET', 'HTTP_GET_VARS', '_POST', 'HTTP_POST_VARS', '_COOKIE', 'HTTP_COOKIE_VARS', '_FILES', 'HTTP_POST_FILES', '_ENV', 'HTTP_ENV_VARS', '_REQUEST', '_SESSION', 'HTTP_SESSION_VARS' ); foreach (转自:donnki 的个人空间 http://www.phpchina.com/52027/viewspace_20041.html
源文件的页面说明
# This does the initial setup for a web request. It does some security checks, # starts the profiler and loads the configuration, and optionally loads # Setup.php depending on whether MW_NO_SETUP is defined.WebStart.php初始化流程分析(一)
作为wiki入口的index.php第一次require的页面,WebStart首先对当前请求的一些参数做检查,即检查超级全局变量$_REQUEST是否已经设定了下面数组中的值。
___FCKpd___1
- 接下来的代码:
define( 'MEDIAWIKI', true ); //Start profiler require_once( './StartProfiler.php' );
StartProfiler.php文件里只调用了ProfilerStub.php。根据上下文来看ProfilerStub.php里定义的两个主要的函数wfProfileIn()、wfProfileOut()应该是做Debug用的。Debug程序依次执行了什么页面,执行了什么操作。这里ProfilerStub的入口函数setproctitle不存在,因此无法进行debug。估计是开放的一个接口,以供用户去实现这个函数。注意到StartProfiler.php有这样一段注释:
/** * To use a profiler, delete the line above and add something like this: * * require_once( dirname(__FILE__).'/includes/Profiler.php' ); * $wgProfiler = new Profiler; */
因此,可以将原来的代码注释掉,换成上面这段代码,并且在LocationSettings.php里设置$wgDebugLogFile值,则可以完全开启debug功能了~~ 下面便是我开启了debug功能后访问index.php得到的debug信息,可以看到这个信息非常详尽!不仅包含了请求的一些基本信息,还清楚的列出了依次调用了什么函数,函数的执行时间等等。太强大了!
PS:$wgDebugLogFile 调试信息保存于的文件名
Start request GET /wiki/index.php Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */* Accept-Language: zh-cn Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727) Host: localhost Connection: Keep-Alive Cookie: cdb_cookietime=2592000; wikiUserName=Wiki; wikiUserID=1; wikiToken=3b2eb6c9a7f7d30bc6e292347bc254d9; cdb_sid=GhDGhX; cdb_visitedfid=19D2D1; cdb_auth=R84Xdk4tTY%2B6xJQz7W%2FbYgblAt7j8RtRmUjZMKLmEomXo%2BrW9E11ILzEad%2FXH9k6; XQ3_sid=Det4JP; XQ3_cookietime=2592000; XQ3_auth=71elDnltHBnZNb1c8n%2FwBALtHR3yDHpbzA%2FLZ%2B6ieeNTADB16GusIp2GKw; cdb_smile=1D1; CAKEPHP=0nain3gihsdqeing3d8tuigul1; wiki_session=obbjlbivu5ocqo1nvefj0akhl3 Main cache: FakeMemCachedClient Message cache: MediaWikiBagOStuff Parser cache: MediaWikiBagOStuff Fully initialised Zend Optimizer detected; skipping debug_backtrace for safety. Unstubbing $wgMessageCache on call of $wgMessageCache->getTransform from unknown Zend Optimizer detected; skipping debug_backtrace for safety. Unstubbing $wgContLang on call of $wgContLang->getCode from unknown Zend Optimizer detected; skipping debug_backtrace for safety. Unstubbing $wgLoadBalancer on call of $wgLoadBalancer->getConnection from unknown MessageCache::load(): got from global cache Language::loadLocalisation(): got localisation for zh-cn from source Language::loadLocalisation(): got localisation for en from source Zend Optimizer detected; skipping debug_backtrace for safety. Unstubbing $wgParser on call of $wgParser->firstCallInit from unknown Zend Optimizer detected; skipping debug_backtrace for safety. Unstubbing $wgUser on call of $wgUser->isAllowed from unknown Cache miss for user 1 Logged in from cookie Zend Optimizer detected; skipping debug_backtrace for safety. Unstubbing $wgOut on call of $wgOut->setSquidMaxage from unknown OutputPage::sendCacheControl: private caching; Mon, 03 Dec 2007 08:06:50 GMT ** Profile section ended by close(): -total 20071203080650 0.416 /wiki/index.php Profiling data Name Calls Total Each % Mem -total 1 415.680 415.680 100.000% 0 ( 415.680 - 415.680) [0] __autoload 17 225.801 13.282 54.321% 0 ( 1.073 - 41.644) [2] main-misc-setup 1 222.821 222.821 53.604% 0 ( 222.821 - 222.821) [52] wfMsgReal 1 172.337 172.337 41.459% 0 ( 172.337 - 172.337) [47] Language::loadLocalisation 2 75.578 37.789 18.182% 0 ( 28.314 - 47.264) [1] Setup.php 1 69.901 69.901 16.816% 0 ( 69.901 - 69.901) [11] MediaWiki::finalCleanup 1 55.147 55.147 13.267% 0 ( 55.147 - 55.147) [18] OutputPage::output 1 49.714 49.714 11.960% 0 ( 49.714 - 49.714) [5] User::getSkin 1 47.728 47.728 11.482% 0 ( 47.728 - 47.728) [3] Setup.php-includes 1 44.905 44.905 10.803% 0 ( 44.905 - 44.905) [0] StubObject::_unstub-wgParser 1 42.477 42.477 10.219% 0 ( 42.477 - 42.477) [1] MessageCache::load 1 32.781 32.781 7.886% 0 ( 32.781 - 32.781) [33] MessageCache::load-fromlocal 1 29.961 29.961 7.208% 0 ( 29.961 - 29.961) [19] MediaWiki::initialize 1 28.330 28.330 6.815% 0 ( 28.330 - 28.330) [11] LoadBalancer::getConnection 13 22.291 1.715 5.362% 0 ( 0.020 - 21.884) [21] LoadBalancer::openConnection 13 22.190 1.707 5.338% 0 ( 0.021 - 21.864) [7] StubObject::_unstub-wgContLang 1 20.709 20.709 4.982% 0 ( 20.709 - 20.709) [1] StubObject::_unstub-wgUser 1 20.000 20.000 4.811% 0 ( 20.000 - 20.000) [1] WebStart.php-conf 1 18.389 18.389 4.424% 0 ( 18.389 - 18.389) [0] Parser::firstCallInit 1 15.402 15.402 3.705% 0 ( 15.402 - 15.402) [2] StubObject::_unstub-wgMessageCache 1 9.858 9.858 2.371% 0 ( 9.858 - 9.858) [2] Setup.php-misc1 1 9.522 9.522 2.291% 0 ( 9.522 - 9.522) [1] Parser::initialiseVariables 1 9.004 9.004 2.166% 0 ( 9.004 - 9.004) [0] Setup.php-memcached 1 8.207 8.207 1.974% 0 ( 8.207 - 8.207) [1] -overhead-total 104 8.187 0.079 1.970% 0 ( 8.187 - 8.187) [104] Database::query 8 8.094 1.012 1.947% 0 ( 0.368 - 2.406) [10] StubObject::_unstub-wgLoadBalancer 1 6.832 6.832 1.644% 0 ( 6.832 - 6.832) [1] User::load 1 6.284 6.284 1.512% 0 ( 6.284 - 6.284) [6] -setup 1 5.340 5.340 1.285% 0 ( 5.340 - 5.340) [0] Database::open 1 3.426 3.426 0.824% 0 ( 3.426 - 3.426) [5] Setup.php-exception 1 3.141 3.141 0.756% 0 ( 3.141 - 3.141) [0] -overhead-internal 104 3.029 0.029 0.729% 0 ( 0.020 - 0.595) [0] Language::fixUpSettings 1 2.859 2.859 0.688% 0 ( 2.859 - 2.859) [0] MessageCache::load-fromcache 1 2.807 2.807 0.675% 0 ( 2.807 - 2.807) [12] query: SELECT ug_group FROM `user_groups` WHERE ug_user = 'X' 1 2.380 2.380 0.573% 0 ( 2.380 - 2.380) [0] query: SELECT value,exptime FROM `objectcache` WHERE keyname='X' 2 2.282 1.141 0.549% 0 ( 0.684 - 1.598) [0] dbconnect-localhost 1 1.835 1.835 0.441% 0 ( 1.835 - 1.835) [0] MediaWiki::initializeSpecialCases 1 1.577 1.577 0.379% 0 ( 1.577 - 1.577) [1] Skin::getSkinNames-init 1 1.487 1.487 0.358% 0 ( 1.487 - 1.487) [0] Job::pop 1 1.276 1.276 0.307% 0 ( 1.276 - 1.276) [5] StubObject::_unstub-wgOut 1 1.183 1.183 0.285% 0 ( 1.183 - 1.183) [0] query: SELECT * FROM `user` WHERE user_id = 'X' LIMIT N 1 0.884 0.884 0.213% 0 ( 0.884 - 0.884) [0] query: SET sql_mode = 'X' 1 0.843 0.843 0.203% 0 ( 0.843 - 0.843) [2] query: SELECT * FROM `job` WHERE job_id >= N ORDER BY job_id LIMIT N 1 0.769 0.769 0.185% 0 ( 0.769 - 0.769) [0] Setup.php-SetupSession 1 0.573 0.573 0.138% 0 ( 0.573 - 0.573) [0] Setup.php-extensions 1 0.504 0.504 0.121% 0 ( 0.504 - 0.504) [0] query: COMMIT 1 0.435 0.435 0.105% 0 ( 0.435 - 0.435) [0] query: BEGIN 1 0.361 0.361 0.087% 0 ( 0.361 - 0.361) [0] Setup.php-globals 1 0.104 0.104 0.025% 0 ( 0.104 - 0.104) [0] MediaWiki::doUpdates 2 0.060 0.030 0.014% 0 ( 0.028 - 0.033) [4] Setup.php-User 1 0.027 0.027 0.006% 0 ( 0.027 - 0.027) [0] MessageCache::__construct 1 0.016 0.016 0.004% 0 ( 0.016 - 0.016) [0] LoadBalancer::getReaderIndex 1 0.005 0.005 0.001% 0 ( 0.005 - 0.005) [0] Setup.php-misc2 1 0.002 0.002 0.000% 0 ( 0.002 - 0.002) [0] Profile section ended by close(): -total 1 0.000 0.000 0.000% 0 ( 0.000 - 0.000) [102] WebStart.php-ob_start 1 -0.000 -0.000 -0.000% 0 ( -0.000 - 0.000) [0] Total: 0.41567989266836 Request ended normally
- 注意:wfProfileIn和wfProfileOut需要成对出现,否则会出错。而且调试信息的输出顺序是:先输出已经匹配好了的一对wfProfileIn和wfProfileOut的调试信息,也即遇到wfProfileOut时才输出该段调试信息,而不是wfProfileIn。这一点在上面的调试信息中可以看出。
- 此外,注意DefaultSettings.php里的关于"调试/日志记录"和"Profiling"两个大选项里的一些参数。可以定制输出信息的。
WebStart.php初始化流程分析(二)
接下来的这行代码:
# Load up some global defines. require_once( './includes/Defines.php' );
- 这里定义了很多的常量,大部分常量在Defines.php中都有注释,在这里先不仔细去看其意思了。接下来
# LocalSettings.php is the per site customization file. If it does not exit # the wiki installer need to be launched or the generated file moved from # ./config/ to ./ if( !file_exists( './LocalSettings.php' ) ) { $IP = '.'; require_once( './includes/DefaultSettings.php' ); # used for printing the version require_once( './includes/templates/NoLocalSettings.php' ); die(); }
- 这段代码的意思已经在注释里说得很明白了:若wiki根目录下不存在LocalSettings.php,则认为设置不存在,应该进行Wiki的系统配置、或者将config目录下的LocalSettings.php拷贝到wiki根目录下。
接下来的代码:
# Include this site setttings require_once( './LocalSettings.php' ); wfProfileOut( 'WebStart.php-conf' ); wfProfileIn( 'WebStart.php-ob_start' ); # Initialise output buffering if ( ob_get_level() ) { # Someone's been mixing configuration data with code! # How annoying. } elseif ( !defined( 'MW_NO_OUTPUT_BUFFER' ) ) { require_once( './includes/OutputHandler.php' ); ob_start( 'wfOutputHandler' ); } wfProfileOut( 'WebStart.php-ob_start' );
装入LocalSettings设定、初始化输出缓冲区,其回调方法为wfOutputHandler,关于缓冲区可以参考PHP缓冲区。关于wfOutputHandler以后再进一步去跟踪。
WebStart.php初始化流程分析(三)
if ( !defined( 'MW_NO_SETUP' ) ) { require_once( './includes/Setup.php' ); }
这一行调用setup.php,里面执行了很多初始化的工作。具体有待分析Setup.php。。。
REQUEST as $name => $value ) { if( in_array( $name, $verboten ) ) { header( "HTTP/1.x 500 Internal Server Error" ); echo "register_globals security paranoia: trying to overwrite superglobals, aborting."; die( -1 ); } unset( $GLOBALS[$name] ); }- 接下来的代码:
___FCKpd___2
StartProfiler.php文件里只调用了ProfilerStub.php。根据上下文来看ProfilerStub.php里定义的两个主要的函数wfProfileIn()、wfProfileOut()应该是做Debug用的。Debug程序依次执行了什么页面,执行了什么操作。这里ProfilerStub的入口函数setproctitle不存在,因此无法进行debug。估计是开放的一个接口,以供用户去实现这个函数。注意到StartProfiler.php有这样一段注释:
___FCKpd___3
因此,可以将原来的代码注释掉,换成上面这段代码,并且在LocationSettings.php里设置$wgDebugLogFile值,则可以完全开启debug功能了~~ 下面便是我开启了debug功能后访问index.php得到的debug信息,可以看到这个信息非常详尽!不仅包含了请求的一些基本信息,还清楚的列出了依次调用了什么函数,函数的执行时间等等。太强大了!
PS:$wgDebugLogFile 调试信息保存于的文件名
___FCKpd___4
- 注意:wfProfileIn和wfProfileOut需要成对出现,否则会出错。而且调试信息的输出顺序是:先输出已经匹配好了的一对wfProfileIn和wfProfileOut的调试信息,也即遇到wfProfileOut时才输出该段调试信息,而不是wfProfileIn。这一点在上面的调试信息中可以看出。
- 此外,注意DefaultSettings.php里的关于"调试/日志记录"和"Profiling"两个大选项里的一些参数。可以定制输出信息的。
WebStart.php初始化流程分析(二)
接下来的这行代码:
___FCKpd___5
- 这里定义了很多的常量,大部分常量在Defines.php中都有注释,在这里先不仔细去看其意思了。接下来
___FCKpd___6
- 这段代码的意思已经在注释里说得很明白了:若wiki根目录下不存在LocalSettings.php,则认为设置不存在,应该进行Wiki的系统配置、或者将config目录下的LocalSettings.php拷贝到wiki根目录下。
接下来的代码:
___FCKpd___7
装入LocalSettings设定、初始化输出缓冲区,其回调方法为wfOutputHandler,关于缓冲区可以参考PHP缓冲区。关于wfOutputHandler以后再进一步去跟踪。
WebStart.php初始化流程分析(三)
___FCKpd___8