Home  >  Article  >  Backend Development  >  mediawiki1.24 source code analysis (1)

mediawiki1.24 source code analysis (1)

WBOY
WBOYOriginal
2016-08-08 09:26:521756browse

All analysis instructions are written in light red, small size 4 italics.

Index.php

//mediawiki program entry

This is the main web entry point for MediaWiki.

Now start to look at the first line of code of the program to determine whether the PHP version is It is 5.3.2 and above. If not, an error message will be reported on the page.

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__ is PHP5.3+
  3. require dirname(__FILE__) . '/includes/PHPVersionError.php';
  4. wfPHPVersionError( 'index.php' );
  5. }

Next is the more critical code, introduce a PHP file Webstart.php.

Php code

  1. require __DIR__ . '/includes/WebStart.php';
  2. * 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 annotation part of the file is as above, Roughly speaking, the operation performed by this file is to initialize settings for a Web request: perform security checks, enable debugging, and load global variables and constants in the configuration file. Finally, if mediawiki has not been installed, call setup.php to install mediawiki. This file calls Defines.php (constants), LocalSettings.php (configuration file, global variables), and also opens the character buffer here according to the configuration. The callback method is the wfOutputHandler method of OutputHandler.php.

Php code

if

(

ini_get

(

'register_globals'
    ) ) {
  1. die( 'MediaWiki does not support installations where register_globals is enabled. .
  2. 'for help on how to disable it.'
  3. ); }
  4. If the PHP configuration item register_globals is open (ON), mediawiki cannot run.
  5. # bug 15461: Make IE8 turn off content sniffing. Everybody else should ignore this
  6. # We're adding it here so that it's *always* set, even for Alternate entry

# points and when $wgOut gets disabled or overridden.

Php code

header(

'X-Content-Type-Options: nosniff'

);

For IE8 Turn off content sniffing, everyone should ignore this

Php code

  1. $wgRequestTime = microtime( true );

function returns the current Unix timestamp and microtime Number of seconds. Php code

define(

'MEDIAWIKI'

, true );
  1. Define a constant mediawiki

# Full path to working directory.

# Makes it ludepossible to for example to have effective exc path in

apc

.
  1. # __DIR__ breaks symlinked
includes, but

realpath() returns false

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

Php code

$IP = getenv('MW_INSTALL_PATH');

if

($IP === false ) {

$IP =

realpath

(

' .'

) ?: dirname( __DIR__ );
  1. } Get the installation directory of the installation by getting the environment variable of PHP.
  2. # Load the profiler
  3. Php code
  4. require_once
  5. "$IP/includes/profiler/Profiler.php" ;

$wgRUstart = wfGetRusage() ?:

array

();

...

# Start the profiler

  1. //StartProfiler.php file only calls ProfilerStub.php. According to the context, the two main functions wfProfileIn() and wfProfileOut() defined in ProfilerStub.php should be used for debugging. Php code
  2. $wgProfiler = array();

if(

file_exists

( "$IP/StartProfiler.php" ) ) {

require

"$IP/StartProfiler.php"

;

  1. } ... NO_SETUP' ) ) {
  2. require_once"$IP/includes/Setup.php";
  3. }
  4. require_once a lot of files: Profiler.php (analyze it, Mainly used for DEBUG Debugging use), AutoLoader.php (class manager, similar to the IOC container of SPRING in JAVA), Defines.php, StartProfiler.php, DefaultSettings.php, autoload.php, NoLocalSettings.php, OutputHandler.php, Setup.php...
  5. Next comes the program business processing entrance: Php code
  6. $mediaWiki = new
  7. MediaWiki();
$mediaWiki

-> ;run();

Mediawiki.php

The MediaWiki class is defined in mediawiki.php. It includes many methods of wiki objects. Then open up memory space for the $mediaWiki object.

Php code

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

Obtain the request request object and configuration information through the construction method.

Php code

  1. publicfunction run() {
  2. try
  3. //If the request contains a delayed request, it will be the last time the system Operation time comparison. If the last operation time is greater than the maximum request delay, a timeout will be prompted.
  4. $this
  5. ->checkMaxLag(); try {
  6. //Key method, mainly perform operations related to business flow.
  7. $ this&- & gt; main (); } catch (ErrorPageerror
  8. $ e
  9. ) {
  10. // bug 62091: while exceptions are connected to bubble up gui Error,
  11. // they are not internal application faults. As with normal requests, this
  12. // should commit, print the output, do deferred updates, jobs, and profiling.
  13. wfGetLBFactory()->commitMasterChanges();
  14. $e->report();
  15. // display the GUI error
  16. } if ( function_exists(
  17. ' fastcgi_finish_request'
  18. ) ) {
  19. fastcgi_finish_request(); }
  20. $this->triggerJobs();
  21. $this
  22. ->restInPeace(); } catch (Exception
  23. $e
  24. ) { MWExceptionHandler::handle(
  25. $e
  26. ); }
  27. }
  28. Now enter the key method main() method
  29. // Send Ajax requests to the Ajax dispatcher.
  30. if
  31. ( $this->config->get( 'UseAjax' ) &&
  32. $request
  33. -> ;getVal('action', 'view') == 'ajax') { // Set a dummy title, because $wgTitle == null might break things
  34. $title
  35. = Title::makeTitle( NS_MAIN, 'AJAX' );
  36. $this->context->setTitle($title);
  37. $wgTitle = $title;
  38. $dispatcher =
  39. new
  40. AjaxDispatcher($this->config);
  41. $dispatcher->performAction ($this->context->getUser());
  42. wfProfileOut(__METHOD__);

    Determine whether AJAX requests are enabled, and if the $action value in the request is ajax, then send the Ajax request 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 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 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, redirection processing

    Php code

    1. if ( $this->config->get( 'UseFileCache' ) && $title->getNamespace() >= 0 ) {
    2. wfProfileIn( 'main-try-filecache' );
    3. ...
    4. wfProfileOut( 'main-try-filecache' );
    5. Determine whether the configuration is enabled File caching function, and if the namespace is greater than or equal to 1, use the file caching mechanism related functions.

    Namespace value

    User:                                            ser_talk:                                                                                                         Test_talk: Image:​​​​​​Image_talk: MediaWiki: MediaWiki_talk: Template :       Template_talk: ​

    Namespace value meaning

    -1

    Special:                                  

    1

    Talk:                          

    2

    5

    7

    8

    10

    11

    Help: 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();

    Processing Request job and build output. This method will generate an output object $output. This object has corresponding methods to set different output results.

    Php code

    1. wfProfileIn( __METHOD__ );

    The first sentence of the method, I found that the basic method entry in mediawiki has this sentence, and wfProfileOut( __ME appears after it THOD__ ) tracking It is found that after starting the DEBUG mode, print the corresponding data. Enable the printing method and set the $wgDebugLogFile=d:a.txt value in LocationSettings.php. Note: wfProfileIn and wfProfileOut need to appear in pairs, otherwise an error will occur. Moreover, the output sequence of debugging information is: first output the debugging information of the matched pair of wfProfileIn and wfProfileOut, that is, only output the debugging information when wfProfileOut is encountered, not wfProfileIn. .

    Php code

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

    Determine whether the request has a print request. If so, mark it in the output object.

    Php code

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

    performs checks before initialization through the request object. This is a system hook program and should require a plug-in to implement the BeforeInitialize method. My full-text search found no specific practical examples of this method.

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

    1. $permErrors = $title->isSpecial('RunJobs')
    2. ? array () // relies on HMAC key signature alone
    3. : $title->getUserPermissionsErrors('read', $user);
    4. if(count($perm Errors ) ) {

    Determine whether the user has read permission to access the page based on the title. If the permissions are insufficient, construct the item page and return.

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

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

    Php code

    1. ignore_user_abort( true );

    Function provided by PHP, if set to true, ignores disconnection from the user. PHP will not detect if the user has disconnected until trying to send information to the client.

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

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

    Php code

    1. wfGetLBFactory()- >commitMasterChanges();

    Things are submitted and rolled back if there is an error.

    //Output everything!

    Php code

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

    Page output Go to the front page. All data before this sentence does not carry styles. Word code execution will add different skins according to the return data type.

    Php code

    1. wfProfileOut(__METHOD__);

    The above introduces the mediawiki1.24 source code analysis (1), including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn