`
qepwqnp
  • 浏览: 102723 次
  • 性别:
  • 来自: 成都
最近访客
博主相关
  • 博客
  • 微博
  • 相册
  • 收藏
  • 社区版块
    • ( 0)
    • ( 65)
    • ( 47)
    存档分类
    最新评论

    mediawiki1.24源码分析(一)

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

    mediawiki1.24源码分析(一) -凯发k8国际

    //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 mediawiki.org '  
    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__ );  
    分享到:
    评论

    相关推荐

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

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

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

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

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

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

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

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

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

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

      mediawiki-emailaddressimage-源码.rar

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

      mediawiki linux install package version 1.18.1 mediawiki-1.18.1.tar.gz

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

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

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

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

      对于运行mediawiki 1.24或更早版本的用户:将以下内容添加到localsettings.php的底部: require_once "$ip/extensions/voicecommander/voicecommander.php"; 用法 首选项 在用户首选项下,可以更改voice com

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

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

    global site tag (gtag.js) - google analytics
    网站地图