Manual:Code

This page describes the primary source files (classes) / objects of the MediaWiki code.

index.php
(main directory)

Main script. It creates the necessary global objects and parses the URL to determine what to do, which it then generally passes off to scripts in other files (depending on the action to be taken).

It applies inclusion to WebStart.php, which in turn applies inclusion to LocalSettings.php.

Next it defines class MediaWiki through inclusion of Wiki.php, and creates an object $mediaWiki which is an instance of that class.

It calls function checkInitialQueries in Wiki.php, which calls function newFromURL in Title.php (see below) to extract the title and action from the URL.

Many functions do their job by sending content to the $wgOut object. It is created by function initialize (where the name $output is used), and flushed out by function finalCleanup, both in Wiki.php (see below), which flushes that out. If there are any changes that need to be made to the database that can be deferred until after page display, those happen at the end.

Note that the order in the includes is touchy; Language uses some global functions, etc. Likewise with the creation of the global variables. Don't move them around without some forethought.

Wiki.php
(directory "includes")

This file consists of the definition of the class MediaWiki. Among other things this class contains:
 * function performAction which performs most actions, including creating the HTML. For example, in the case of viewing a page, function "view" in Article.php is called.
 * function finalCleanup (called by index.php) which calls function output in Output.php, which flushes out all HTML.

Setup.php
This file contains, among other things: With or without using function StubObject in Stubobject.php, this creates an instance $wgOut of class Outputpage in OutputPage.php, etc.

User.php
(directory "includes")

Encapsulates the state of the user viewing/using the site. Can be queried for things like the user's settings, name, etc. Handles the details of getting and saving to the user table of the database, and dealing with sessions and cookies.

OutputPage.php
(directory "includes")

This file contains function addPrimaryWikiText which:
 * applies function parse in Parser.php to the wikitext $text, resulting in the HTML text $text
 * applies function addParserOutput to the HTML text $text, which:
 * applies function addHTML to $text, which appends $text to $mBodytext.

Contains:
 * function output, called from function finalCleanup in Wiki.php. It calls function outputPage in Skin.php.
 * function out, called from function outputPage in Skin.php a number of times, to send the HTML to the user: $mBodytext and HTML before and after it.

Parser.php
(directory "includes")

This file defines the parser object used to convert a string $text from wikitext to HTML. It contains function parse, which performs the parsing in several stages:
 * 1) It calls function strip, which strips and renders nowiki, pre, math, and hiero. This function is preceded by the ParserBeforeStrip hook and followed by the ParserAfterStrip hook.
 * 2) It calls function internalParse, which converts the string $text from wikitext to HTML. It expands variables and templates, replaces wiki markup such as header codes and double and triple quotation marks with their HTML equivalents, converts double-bracketed phrases into A HREF links to internal wiki pages, and converts single-bracketed URLs into A HREF links to external pages.
 * 3) It calls function tidy, which does some HTML cleanup. This function is preceded by the ParserBeforeTidy hook and followed by the ParserAfterTidy hook.

Title.php
(directory "includes") Represents the title of an article, and does all the work of translating among various forms such as plain text, URL, database key, etc. For convenience, and for historical reasons, it also represents a few features of articles that don't involve their text, such as access rights.

The MediaWiki software's "Title" class represents article titles, which are used for many purposes: as the human-readable text title of the article, in the URL used to access the article, the wikitext link to the article, the key into the article database, and so on. The class in instantiated from one of these forms and can be queried for the others, and for other attributes of the title. This is intended to be an immutable "value" class, so there are no mutator functions.

To get a new instance, call one of the static factory methods Title::newFromURL, Title::newFromDBKey, or Title::newFromText. Once instantiated, the other non-static accessor methods can be used, such as getText, getDBKey, getNamespace, etc.

The prefix rules: a title consists of an optional Interwiki prefix (such as "m:" for meta or "de:" for German), followed by an optional namespace, followed by the remainder of the title. Both Interwiki prefixes and namespace prefixes have the same rules: they contain only letters, digits, space, and underscore, must start with a letter, are case insensitive, and spaces and underscores are interchangeable. Prefixes end with a ":". A prefix is only recognized if it is one of those specifically allowed by the software. For example, "de:name" is a link to the article "name" in the German Wikipedia, because "de" is recognized as one of the allowable interwikis. The title "talk:name" is a link to the article "name" in the "talk" namespace of the current wiki, because "talk" is a recognized namespace. Both may be present, and if so, the interwiki must come first, for example, "m:talk:name". If a title begins with a colon as its first character, no prefixes are scanned for, and the colon is just removed. Note that because of these rules, it is possible to have articles with colons in their names. "E. Coli 0157:H7" is a valid title, as is "2001: A Space Odyssey", because "E. Coli 0157" and "2001" are not valid interwikis or namespaces.

Character mapping rules: Once prefixes have been stripped, the rest of the title processed this way: spaces and underscores are treated as equivalent and each is converted to the other in the appropriate context (underscore in URL and database keys, spaces in plain text). "Extended" characters in the 0x80..0xFF range are allowed in all places, and are valid characters. They are encoded in URLs. Other characters may be ASCII letters, digits, hyphen, comma, period, apostrophe, parentheses, and colon. No other ASCII characters are allowed, and will be deleted if found (they will probably cause a browser to misinterpret the URL). Extended characters are _not_ urlencoded when used as text or database keys.

Canonical forms: the canonical form of a title will always be returned by the object. In this form, the first (and only the first) character of the namespace and title will be uppercased; the rest of the namespace will be lowercased, while the title will be left as is. The text form will use spaces, the URL and DBkey forms will use underscores. Interwiki prefixes are all lowercase. The namespace will use underscores when returned alone; it will use spaces only when attached to the text title.

getArticleID needs some explanation: for "internal" articles, it should return the "cur_id" field if the article exists, else it returns 0. For all external articles it returns 0. All of the IDs for all instances of Title created during a request are cached, so they can be looked up quickly while rendering wikitext with lots of internal links.

Article.php
(directory "includes")

Calls function parse in wgParse, which is an instance of class Parser in Parser.php.

Encapsulates access to the revision table etc. of the database. The object represents an article, and maintains state such as text (in wikitext format), flags, etc.

Skin.php
(directory "includes")

Encapsulates a "look and feel" for the wiki. All of the functions that render HTML, and make choices about how to render it, are here, and called from various other places when needed.

Contains function outputPage, called from function output in OutputPage.php. Function outputPage calls function out in OutputPage.php a number of times, to send the HTML to the user: $mBodytext and HTML before and after it.

The StandardSkin object is a complete implementation, and is meant to be subclassed with other skins that may override some of its functions. The User object contains a reference to a skin (according to that user's preference), and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser->getSkin.

Language.php
(directory "languages")

Represents the language used for incidental text, and also has some character encoding functions and other locale stuff. A global one is allocated in $wgLang.

See also internationalisation.

LinkCache.php
(directory "includes")

Keeps information on existence of articles. See LINKCACHE.TXT.

API
The api code has an external access point /api.php, but can also be used internally by other code. See api page for in depth description.