`
qepwqnp
  • 浏览: 106089 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

mediawiki1.24源码分析(一)

PHP 
阅读更多

所有分析说明采用文字使用浅红色、小四号楷体。

Index.php

//mediawiki程序入口

This is the main web entry point for MediaWiki. 

现在开始看程序的第一句代码,判断PHP版本是否是5.3.2及以上,如果不是就在页面报错提示。

 

Php代码 
  1. if ( !function_exists( 'version_compare' ) || version_compare( PHP_VERSION, '5.3.2' ) < 0 ) {  
  2. // We need to use dirname( __FILE__ ) here cause __DIR__ is PHP5.3+  
  3. require dirname( __FILE__ ) . '/includes/PHPVersionError.php';  
  4. wfPHPVersionError( 'index.php' );  
  5. }  

 

 

接下来是比较关键的代码了,引入一个PHP文件 Webstart.php。

 

Php代码 
  1. require __DIR__ . '/includes/WebStart.php';  

 

 

Webstart.php

 * This does the initial set up 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.

 * Setup.php (if loaded) then sets up GlobalFunctions, the AutoLoader,

 * and the configuration globals (though not $wgTitle).

 

WebStart.php的文件注解部分如上,大概说此文件执行的操作是为一个Web请求进行初始化设置:进行安全检查、调试开启、装载配置文件里的全局变量及常量。最后如果没有安装过mediawiki则调用setup.php执行安装mediawiki操作。这个文件中调用了Defines.php(常量)、LocalSettings.php(配置文件,全局变量),另外还在这里根据配置开启字符缓冲区,回调方法是OutputHandler.php的wfOutputHandler方法。 

 

 

 

Php代码 
  1. if ( ini_get'register_globals' ) ) {  
  2. die'MediaWiki does not support installations where register_globals is enabled. '  
  3. 'Please see <a href="https://www.mediawiki.org/wiki/register_globals">mediawiki.org</a> '  
  4. 'for help on how to disable it.' );  
  5. }  

 

 

如果PHP配置项register_globals是打开状态(ON),则mediawiki无法运行。

 

# bug 15461: Make IE8 turn off content sniffing. Everybody else should ignore this

# We're adding it here so that it's *always* set, even for alternate entry

# points and when $wgOut gets disabled or overridden.

 

Php代码 
  1. header( 'X-Content-Type-Options: nosniff' );  

 

 

针对IE8进行关闭内容嗅探,大家应该应该忽略这个

 

 

Php代码 
  1. $wgRequestTime = microtime( true );  

 

 

 函数返回当前 Unix 时间戳和微秒数。

 

Php代码 
  1. unset( $IP );  

 

 

注销定义$IP的变量

 

Php代码 
  1. define( 'MEDIAWIKI', true );  

 

 

定义一个常量mediawiki

# Full path to working directory.

# Makes it possible to for example to have effective exclude path in apc.

# __DIR__ breaks symlinked includes, but realpath() returns false

# if we don't have permissions on parent directories.

 

Php代码 
  1. $IP = getenv'MW_INSTALL_PATH' );  
  2. if ( $IP === false ) {  
  3. $IP = realpath'.' ) ?: dirname( __DIR__ );  
  4. }  

 

 

通过获取PHP的环境变量,获取安装的安装目录。

 

# Load the profiler

 

Php代码 
  1. require_once "$IP/includes/profiler/Profiler.php";  
  2. $wgRUstart = wfGetRusage() ?: array();  

 

 

...

# Start the profiler

//StartProfiler.php文件里只调用了ProfilerStub.php。根据上下文来看ProfilerStub.php里定义的两个主要的函数wfProfileIn()、wfProfileOut()应该是做Debug用的。

 

Php代码 
  1. $wgProfiler = array();  
  2. if ( file_exists"$IP/StartProfiler.php" ) ) {  
  3. require "$IP/StartProfiler.php";  
  4. }  
  5. ...  
  6. if ( !defined( 'MW_NO_SETUP' ) ) {  
  7. require_once "$IP/includes/Setup.php";  
  8. }  

 

 

 

require_once了一大堆文件:Profiler.php(分析其,主要用于DEBUG调试使用)、AutoLoader.php(类管理器,类似JAVA中SPRING的IOC容器)、Defines.php、StartProfiler.php、DefaultSettings.php、autoload.php、NoLocalSettings.php、OutputHandler.php、Setup.php……

 

接下来到了程序业务处理入口: 

 

Php代码 
  1. $mediaWiki = new MediaWiki();  
  2. $mediaWiki->run();  

 

 

Mediawiki.php

 

 mediawiki.php里定义了MediaWiki类。其中包括很多的wiki对象的方法。接着为$mediaWiki对象开辟内存空间。

 

Php代码 
  1. public function __construct( IContextSource $context = null ) {  
  2. if ( !$context ) {  
  3. $context = RequestContext::getMain();  
  4. }  
  5.    
  6. $this->context = $context;  
  7. $this->config = $context->getConfig();  
  8. }  

 

 

通过构造方法,获取request请求对象、配置信息。

 

Php代码 
  1. public function run() {  
  2. try {  
  3. //请求中如果包含延迟请求,和系统最后一次操作时间对比。如果最后一次操作时间大于请求最大延迟,则提示超时。  
  4. $this->checkMaxLag();  
  5. try {  
  6. //关键方法,主要做业务流转相关操作。  
  7. $this->main();  
  8. } catch ( ErrorPageError $e ) {  
  9. // Bug 62091: while exceptions are convenient to bubble up GUI errors,  
  10. // they are not internal application faults. As with normal requests, this  
  11. // should commit, print the output, do deferred updates, jobs, and profiling.  
  12. wfGetLBFactory()->commitMasterChanges();  
  13. $e->report(); // display the GUI error  
  14. }  
  15. if ( function_exists( 'fastcgi_finish_request' ) ) {  
  16. fastcgi_finish_request();  
  17. }  
  18. $this->triggerJobs();  
  19. $this->restInPeace();  
  20. } catch ( Exception $e ) {  
  21. MWExceptionHandler::handle( $e );  
  22. }  
  23. }  
  24. 现在进入关键方法main()方法  
  25. // Send Ajax requests to the Ajax dispatcher.  
  26. if ( $this->config->get( 'UseAjax' ) && $request->getVal( 'action''view' ) == 'ajax' ) {  
  27.    
  28. // Set a dummy title, because $wgTitle == null might break things  
  29. $title = Title::makeTitle( NS_MAIN, 'AJAX' );  
  30. $this->context->setTitle( $title );  
  31. $wgTitle = $title;  
  32.    
  33. $dispatcher = new AjaxDispatcher( $this->config );  
  34. $dispatcher->performAction( $this->context->getUser() );  
  35. wfProfileOut( __METHOD__ );  
  36. return;  
  37. }  

 

判断是否启用AJAX请求,并且请求中$action值为ajax,则将Ajax请求发送到Ajax dispather处理器。

If the user has forceHTTPS set to true, or if the user

// is in a group requiring HTTPS, or if they have the HTTPS

// preference set, redirect them to HTTPS.

// Note: Do this after $wgTitle is setup, otherwise the hooks run from

// isLoggedIn() will do all sorts of weird stuff.

Php代码 
  1. if (  
  2. $request->getProtocol() == 'http' &&  
  3. (  
  4. ...  
  5. wfProfileOut( __METHOD__ );  
  6. return;  
  7. }  
  8. }  

 

如果forcehttps设置为true,并且使用https访问,进行重定项处理

 

Php代码 
  1. if ( $this->config->get( 'UseFileCache' ) && $title->getNamespace() >= 0 ) {  
  2. wfProfileIn( 'main-try-filecache' );  
  3. ...  
  4. wfProfileOut( 'main-try-filecache' );  
  5. }  

 

判断配置是否开启文件缓存功能,并且命名空间大于等于1的情况,使用文件缓存机制相关功能

 

命名空间值

命名空间值含义

-1

Special:        

0

Template:       

1

Talk:           

2

User:           

3

User_talk:      

4

Test:           

5

Test_talk:      

6

Image:          

7

Image_talk:     

8

MediaWiki:      

9

MediaWiki_talk: 

10

Template:       

11

Template_talk:  

12

Help:           

13

Help_talk:      

14

Category:       

15

Category_talk:  

16

ONLINEPAY

 

// Actually do the work of the request and build up any output

Php代码 
  1. $this->performRequest();  

 

处理请求的工作和建立输出。在此方法会生程一个输出对象$output,此对象有相应方法可以设置不同的输出结果。

Php代码 
  1. wfProfileIn( __METHOD__ );  

 

方法第一句,发现mediawiki中基本方法入口都要这么一句,他的后面出现wfProfileOut( __METHOD__ )跟踪发现为启动DEBUG模式后,进行相应数据的打印。开启打印方法LocationSettings.php里设置$wgDebugLogFile=d:\a.txt值。注意:wfProfileIn和wfProfileOut需要成对出现,否则会出错。而且调试信息的输出顺序是:先输出已经匹配好了的一对wfProfileIn和wfProfileOut的调试信息,也即遇到wfProfileOut时才输出该段调试信息,而不是wfProfileIn。。 

Php代码 
  1. if ( $request->getVal( 'printable' ) === 'yes' ) {  
  2. $output->setPrintable();  
  3. }  

 

判断请求是否有打印请求。如果有就在输出对象中进行标注。

Php代码 
  1. $unused = null; // To pass it by reference  
  2. wfRunHooks( 'BeforeInitialize'array( &$title, &$unused, &$output, &$user$request$this ) );  

 

通过请求对象,进行初始化之前的检查工作。这个属于系统钩子程序,应该需要插件进行实现BeforeInitialize方法,我全文搜索没有此方法的具体实用。

// Check user's permissions to read this page.

// We have to check here to catch special pages etc.

// We will check again in Article::view().

Php代码 
  1. $permErrors = $title->isSpecial( 'RunJobs' )  
  2. array() // relies on HMAC key signature alone  
  3. $title->getUserPermissionsErrors( 'read'$user );  
  4. if ( count$permErrors ) ) {  

 

根据title进行判断用户是否有次页面的访问read权限。如果权限不足构造项页面进行返回。

 

 

// Either all DB and deferred updates should happen or none.

// The later should not be cancelled due to client disconnect.

Php代码 
  1. ignore_user_abort( true );  

 

PHP提供的函数,如果设置为 true,则忽略与用户的断开。PHP 不会检测到用户是否已断开连接,直到尝试向客户机发送信息为止。

// Now commit any transactions, so that unreported errors after

// output() don't roll back the whole DB transaction

Php代码 
  1. wfGetLBFactory()->commitMasterChanges();  

 

事物提交,存在错误进行回滚。

// Output everything!

Php代码 
  1. $this->context->getOutput()->output();  

 

页面输出到前台页面,在此句之前所有数据不携带样式。词句代码执行会按返回数据类型进行添加不同的skin。

Php代码 
  1. wfProfileOut( __METHOD__ );  
分享到:
评论

相关推荐

    mediawiki源码分析-DefaultSettings变量说明

    文档是DefaultSettings.php文件中提供的所有配置选项。使用当中,请注意不要直接编辑该文件本身,而是将需要的代码拷贝到LocalSettings.php文件中,并做出相应的修改。

    MediaWiki使用MSUpload扩展上传文件功能

    MediaWiki是一个比较流行的wiki百科软件,部署使用都比较简单,容易上手。查看搜索都非常方便,但是编辑起来就比较麻烦,很多人不太习惯。而且很多知识都已经以word或excel、pdf等类型文档存放。重新输入到wiki中不...

    MediaWiki 1.25.1.gz

    MediaWiki是一个运行在服务器端的自由软件,基于GNU General Public License(GPL协议)发行。它能够平稳地运行在日访问量上百万的网站服务器集群中。支持多国语言,包括简繁中文。MediaWiki是一个强大、可扩展、...

    python-mediawiki-utilities, 用于访问和处理MediaWiki数据的一组实用程序.zip

    python-mediawiki-utilities, 用于访问和处理MediaWiki数据的一组实用程序 MediaWikiMediaWiki实用程序是 Aaron Halfaker开发的一个开源( MIT授权) 库,用于从MediaWiki安装。从数据库和xml转储中提取和处理数据。...

    MediaWiki中文使用手册

    MediaWiki是由PHP开发的免费开源的维基程序,运行于...MediaWiki适合建立大型的百科网站,也有很多网站利用MediaWiki建立知识库系统,例如著名的Blog程序WordPress的官方网站的知识库体系就是基于MediaWiki搭建的。

    MediaWiki v1.23.0.zip

    MediaWiki更新说明: 添加好多新特性, 修复已知bug。   MediaWiki是全球最著名的wiki程序,运行于PHP MySQL环境。MediaWiki从2002年2月25日被作为维基百科全书的系统软件,并有大量其他应用实例。目前...

    MediaWiki Windows下一键安装包之一

    一键即可安装完成MediaWIKi,包括相关环境的配置,PHP,Mysql等。由于压缩包太大,分成两个上传。这个是第一个。

    MediaWiki相关包

    很多人都发现 MediaWiki 提供了一个可用的环境来在工作组、甚至整个组织以及在线社区之间共享信息。MediaWiki 让用户可以通过博客、wiki 以及文件来共享信息。它还允许使用一个标记云来保护所上载的文件,标记文件...

    discuzxsso:Discuz和MediaWiki SSO

    本程序是mediawiki的extension,基于mediawiki 1.24测试 本程序不仅仅是用户数据的同步,还实现了用户的同步登录和登出 新增用户权限的设计 本程序修改了官方扩展的代码,升级了uc_client为discuz3.2使用的1.6.0 本...

    如何搭建wiki--mediawiki

    this is a brief introduction of how to setup a wiki page as soon as possible. 使用mediawiki建议一个自定义wiki网站,基于windows 系统, 使用xampp 简易配置包, 英文说明

    mediawiki-EmailAddressImage-源码.rar

    mediawiki-EmailAddressImage-源码.rar

    MediaWiki v1.15.1 多国语言.rar

    MediaWiki从2002年2月25**被作为维基百科全书的系统软件,并有大量其他应用实例。目前MediaWiki的开发得到维基媒体基金会的支持。 MediaWiki是建立wiki网站的首选后台程序,目前国内的大型维客站点基本都采用这套...

    MediaWiKi linux Install package

    MediaWiKi linux Install package Version 1.18.1 mediawiki-1.18.1.tar.gz

    MediaWiki v1.20.1

    MediaWiki从2002年2月25日被作为维基百科全书的系统软件,并有大量其他应用实例。目前MediaWiki的开发得到维基媒体基金会的支持。 MediaWiki是建立wiki网站的首选后台程序,目前国内的大型维客站点基本都采用这套...

    mediawiki-lazyload:扩展以延迟在MediaWiki页面上加载图像

    一个扩展,以延迟在MediaWiki页面上加载图像。 要求 Lazyload扩展需要MediaWiki 1.25或更高版本。 安装 要安装扩展,请将整个Lazyload目录放置在MediaWiki extensions目录中,然后Lazyload添加到LocalSettings.php...

    基于PHP的mediawiki-zhconverter基于mediawiki中文简繁互转源码.zip

    基于PHP的mediawiki-zhconverter基于mediawiki中文简繁互转源码.zip

    MediaWiki语法.pdf

    向大家介绍MediaWiki 语法简介,主要包括MediaWiki 语法简介使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

    VoiceCommander:允许在MediaWiki中进行语音识别

    对于运行MediaWiki 1.24或更早版本的用户:将以下内容添加到LocalSettings.php的底部: require_once "$IP/extensions/VoiceCommander/VoiceCommander.php"; 用法 首选项 在用户首选项下,可以更改Voice Com

    mediawiki的安装.doc

    对于刚接触wiki的朋友们来说,配置一个服务器环境,安装并运行mediawiki是一件很麻烦的事情,在这里,我尽量用通俗易懂的语言,介绍mw(mediawiki,下同)在windows下的安装过程。

    mediawiki-1.13.0.tar.gz

    mediawiki-1.13.0.tar.gz。。直接安装。。一路NEXT即可

Global site tag (gtag.js) - Google Analytics