mediawiki1.24 Source Analysis (i)

Source: Internet
Author: User
Tags autoloader class manager mediawiki
all the analysis instructions use the text using light red, small fourth number in italics.

index.php

MediaWiki program entry

This is the main web entry point for MediaWiki.

Now start to look at the first code of the program, determine whether the PHP version is 5.3.2 and above, if not on the page error prompt.

PHP code

    1. if (!function_exists ( ' version_compare ' ) | | version_compare (php_version, ' 5.3.2 ' ) < 0) {
    2. We need to use DirName (__file__) Here cause __dir__ are php5.3+
    3. require dirname ( __file__ ). '/includes/phpversionerror.php ';
    4. Wfphpversionerror ( ' index.php ' );
    5. }

Next is the more critical code, introducing a PHP file webstart.php.

PHP code

    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 the file annotations section above, presumably saying that this file performs an initialization setting for a Web request: Security checks, debugging on, loading the global variables and constants in the configuration file. Finally, if MediaWiki is not installed, call setup.php to perform the installation MediaWiki operation. This file calls the defines.php (constant), localsettings.php (config file, global variable), and also here the character buffer is opened according to the configuration, and the callback method is the Wfoutputhandler method of outputhandler.php.

PHP code

    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. }

If the PHP configuration item register_globals is open (on), MediaWiki cannot run.

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

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

# points and when $wgOut gets disabled or overridden.

PHP code

    1. Header ( ' X-content-type-options:nosniff ' );

Shut down the content for IE8 sniffing, you should ignore this

PHP code

    1. $wgRequestTime = Microtime (true);

The function returns the current Unix timestamp and the number of microseconds.

PHP code

    1. unset ( $IP );

Unregister a variable that defines $IP

PHP code

    1. Define ( ' MEDIAWIKI ', true);

Defines a constant MediaWiki

# Full path to working directory.

# makes it possible to a example to having effective exclude path in APC .

# __dir__ Breaks symlinked includes, but Realpath () returns false

# if we don ' t has permissions on the parent directories.

PHP code

    1. $IP = getenv( ' Mw_install_path ' );
    2. if ( $IP = = = False) {
    3. $IP = realpath( '. ' )?: DirName (__DIR__);
    4. }

Get the installed directory by getting the environment variable for PHP.

# Load The profiler

PHP code

    1. require_once "$IP/includes/profiler/profiler.php";
    2. $wgRUstart = Wfgetrusage ()?: array();

...

# Start The profiler

Only profilerstub.php is called in the startprofiler.php file. According to the context, the two main functions defined in profilerstub.php Wfprofilein (), wfprofileout () should be used for debug.

PHP code

    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 a whole bunch of files: profiler.php (analyzing it, used primarily for debug debugging), Autoloader.php (class manager, like the IOC container in Java), defines.php, startprofiler.php, defaultsettings.php, autoload.php, nolocalsettings.php, outputhandler.php, Setup.php ...

Next to the Program Business Processing Portal:

PHP code

    1. $mediaWiki = new mediaWiki ();
    2. $mediaWiki->run ();

mediawiki.php

The MediaWiki class is defined in mediawiki.php. This includes a number of methods for Wiki objects. It then opens up memory space for the $mediawiki object.

PHP code

    1. Public function __construct (icontextsource $context = null) {
    2. if ( ! $context ) {
    3. $context = Requestcontext::getmain ();
    4. }
    5. $this->context = $context;
    6. $this->config = $context->getconfig ();
    7. }

The Request object, configuration information is obtained by constructing the method.

PHP code

  1. Public function Run () {
  2. try {
  3. The request contains a delay request, compared to the last operating time of the system. If the last operation time is greater than the maximum request delay, the prompt times out.
  4. $this->checkmaxlag ();
  5. try {
  6. Key methods, mainly to do business turnover related operations.
  7. $this->main ();
  8. } catch (Errorpageerror $e ) {
  9. Bug 62091:while exceptions is convenient to bubble up GUI errors,
  10. They is 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. Now enter the key method of the main () method
  25. Send Ajax requests to the Ajax dispatcher.
  26. if ( $this->config->get ( ' Useajax ' ) && $request->getval ( ' Action ', ' view ' ) = = ' ajax ' ) {
  27. Set a dummy title, because $wgTitle = = null might break things
  28. $title = Title::maketitle (Ns_main, ' AJAX ' );
  29. $this->context->settitle ( $title );
  30. $wgTitle = $title;
  31. $dispatcher = new ajaxdispatcher ( $this->config);
  32. $dispatcher->performaction ( $this->context->getuser ());
  33. Wfprofileout ( __method__ );
  34. return;
  35. }

If the AJAX request is enabled and the $action value in the request is Ajax, the AJAX request is sent to the Ajax Dispather processor.

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

is in a group requiring HTTPS, or if they has the HTTPS

Preference set, redirect them to HTTPS.

Note:do This $wgTitle are setup, otherwise the hooks run from

isLoggedIn () would do all sorts of weird stuff.

PHP code

    1. if (
    2. $request->getprotocol () = = ' http ' &&
    3. (
    4. ...
    5. Wfprofileout ( __method__ );
    6. return;
    7. }
    8. }

If Forcehttps is set to True, and HTTPS access is used, the re-processing

PHP code

    1. if ( $this->config->get ( ' Usefilecache ' ) && $title->getnamespace () >= 0) {
    2. Wfprofilein ( ' Main-try-filecache ' );
    3. ...
    4. Wfprofileout ( ' Main-try-filecache ' );
    5. }

Use the file caching mechanism to determine whether the configuration turns on the file cache feature and if the namespace is greater than or equal to 1 .

Namespace value

Namespace value meaning

-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 code

    1. $this->performrequest ();

Handles the work of the request and establishes the output. This method will process an output object $output, and this object has a method to set different output results.

PHP code

    1. Wfprofilein ( __method__ );

method The first sentence, found that the basic method of MediaWiki in the entrance to such a sentence, his back appears wfprofileout (__method__) trace found to start debug mode, the corresponding data printing. Open the Print method locationsettings.php set $wgDebugLogFile =d:\a.txt value. Note: Wfprofilein and wfprofileout need to appear in pairs, or they will go wrong. and debug information output order is: First output has been matched a good pair of Wfprofilein and wfprofileout debug information, that is, encountered wfprofileout output the debug information, rather than Wfprofilein.

PHP code

    1. if ( $request->getval ( ' printable ' ) = = = = ' yes ' ) {
    2. $output->setprintable ();
    3. }

Determines whether the request has a print request. If any, the label is in the output object.

PHP code

    1. $unused = null; //To pass it by reference
    2. Wfrunhooks ( ' beforeinitialize ', array(&$title, &$unused, &$ Output, &$user, $request, $this ));

The check works before initialization by requesting an object. This belongs to the system hook program, should need plug-in implementation beforeinitialize method, my full-text search does not have this method of concrete practical.

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

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

We'll check again in Article::view ().

PHP code

    1. $permErrors = $title->isspecial ( ' runjobs ' )
    2. ? Array () //relies on HMAC key signature alone
    3. : $title->getuserpermissionserrors ( ' read ', $user );
    4. if ( Count( $permErrors )) {

According to the title to determine whether the user has access to the page Read permission. If the permissions are insufficient, construct the item page to return.

Either all DB and deferred updates should happen or none.

The later should not being cancelled due to client disconnect.

PHP code

    1. Ignore_user_abort (TRUE);

PHP provides a function that, if set to true, ignores disconnects from the user. PHP does not detect whether a user has been disconnected until an attempt is made to send a message to the client.

Now commits any transactions, so, unreported errors after

Output () don ' t roll back the whole DB transaction

PHP code

    1. Wfgetlbfactory ()->commitmasterchanges ();

The thing commits, there is an error to rollback.

Output everything!

PHP code

    1. $this->context->getoutput ()->output ();

Page output to the foreground page, before this sentence all data does not carry the style. Sentence code execution adds a different skin to the return data type.

PHP code

    1. Wfprofileout ( __method__ );

The above describes the mediawiki1.24 source code Analysis (a), including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.