Manual:Coding conventions

This page describes the coding conventions used within the MediaWiki codebase, including appropriate naming conventions. It is meant to be descriptive, not dictatorial. If you do choose to follow these guidelines, it will probably be easier for you to collaborate with others on the project, but if you want to contribute in your own style by all means do so (though don't be surprised if your code gets reformatted at a later date).

Indentation and spacing

 * Code is indented with tabs, not spaces.
 * We use K&R style brace matching, but braces are added the end of lines for function and class declarations, not on a new line.
 * In general, one space is used on the inside of each non-empty parenthesis in function definitions and calls:

Also:  rather than:

In emacs (see also php-mode), you can approximate this style with a custom minor mode in your .emacs file, i.e.

Classes

 * As a holdover from PHP 4.x's lack of private class members and methods, older code will be marked with comments such as /** @private */ to indicate the intention; please respect this as if it were enforced by the compiler
 * Newer code will use proper visibilities, but do not add it to existing code without first checking, testing and refactoring as required, because the above rule has been broken in several places
 * In general, member variables are named mXxx to distinguish them, which helps spot missing instances of $this, which will cause odd breakage due to PHP's silent initialisation of the variable
 * We prefix the names of global variables with wg (wiki global) in order to make it easier to distinguish them, which thus makes it easier to spot missing global declarations

Arrays

 * Arrays should be formatted with a space separating each element and assignment operator
 * If the line is longer than 80 characters, each element should be in separate line.

String concatenations

 * Always use a space between the dot and the concatenated parts to improve readability.
 * Use spaces before and after the concatenating assignment operator ('.=').

Quoting strings

 * Strings in PHP can either be quoted with single quotes ( '' ) or double quotes ("").
 * The difference between the two is that the parser will use variable-interpolation in double-quoted strings, but not with single-quoted strings.
 * If your string contains no variables, use single quotes and save the parser the trouble of attempting to interpolate the string for variables, like so:


 * Likewise, if you are passing a variable to a function, there is no need to use double quotes:


 * Finally, when using associative arrays, you should include the key within single quotes to prevent any ambiguities, especially with constants:


 * However, if you are accessing an array with a key that is stored in a variable, you can simply use:

Sql code layout

 * Capitialise all SQL keywords (SELECT, FROM, VALUES,UPDATE,DELETE AS etc.)
 * Enclose all conditions in brackets.

Naming conventions
There is a preference for lowerCamelCase when naming functions or variables. For example:

There are also some prefixes used in different places:

Functions

 * wf (wiki functions) - Top-level functions, e.g.

Variables

 * wg - global variables, e.g. ,
 * m - object member variables:
 * This is not universally observed, but try to stay consistent within a class.

Extension Functions and Variables

 * ef - extension functions : top-level functions added by user extensions
 * eg - extension globals

HTTP and session stuff

 * ws - Session variables, e.g.
 * wc - Cookie variables, e.g.
 * wp - Post variables (submitted via form fields), e.g.

Database

 * Table names are usually singular nouns: user, page, revision, etc
 * Except when they're not: pagelinks, categorylinks</tt>...
 * Column names are given a prefix derived from the table name: the name itself if it's short, or an abbreviation:
 * page -> page_id, page_namespace, page_title...
 * categorylinks -> cl_from, cl_namespace...

Common local variables
It is common to work with an instance of the Database</tt> class; we have a naming convention for these which helps keep track of the nature of the server to which we are connected. This is of particular importance in replicated environments, such as Wikimedia and other large wikis.


 * - a Database object for writing (a master connection)
 * - a Database object for non-concurrency-sensitive reading (may be a read-only slave, slightly behind master state)

Inline documentation

 * The Doxygen documentation style is used (it is very similar to PHPDoc for the subset that we use). For example: giving a description of a function or method, the parameters it takes (using ), and what the function returns (using  ), or the   or   tags. Please use "@" rather than "\" as the escape character (e.g. use  ) - both styles work in Doxygen, but the   style does not.


 * General format for parameters is such:  so make sure you don't put   before.

Messages

 * When creating a new message, use hyphens (-) where possible. So for example, "some-new-message" is a good name, while "someNewMessage" and "some_new_message" are not.
 * If the message is going to be used as a label which can have a colon after it, don't hardcode the colon; instead, put the colon inside the message text. Some languages (such as French) need to handle colons in a different way, which is impossible if the colon is hardcoded.
 * HTML class and ID names should be prefixed with "mw-". It seems most common to hyphenate them after that, like "mw-some-new-class" instead of "mw-somenewclass" or "mw-some_new_class", but there doesn't appear to be a clear convention at present.