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
 * In general, one space is used on the inside of each non-empty parenthesis in function definitions and calls:

In emacs, 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

Functions

 * wf - 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 - 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)