User:Robchurch/Extension Writing Tips

From Meta, a Wikimedia project coordination wiki

This is a quick index of tips and tricks encountered when developing MediaWiki or extensions, documented here for new and experienced extension writers alike.

Database[edit]

All interaction with databases in MediaWiki needs to go through the database load balancer, using the standard database class. This is simple and actually makes using the database a no-brainer.

When working with the database

Decide if a master or slave server is needed

If the information has to be to-the-second, or you need to write to the database, you need a master, otherwise use a slave.

Use the global fetch functions

Using the wfGetDB function makes interacting with the load balancer transparent and painless.

wfGetDB( type )
Where type is one of DB_MASTER or DB_SLAVE, corresponding to the database server required.

Simple queries[edit]

MediaWiki's database class contains a number of wrappers around common queries, such as selects, and are preferred over the more complex querying functions.

The example below pulls a simple user count from the user table, the SQL of which is

SELECT COUNT(*) AS count FROM /usertable/
$dbr =& wfGetDB( DB_SLAVE );
$res = $dbr->select( 'user', 'COUNT(*) AS count' );
$row = $dbr->fetchObject( $res );
$count = $row->count;
$dbr->freeResult( $res );

$count will contain the user count. You can add conditions to the select statement, however. For instance, suppose we wanted all users without an email address set? The SQL for this would be something like

SELECT COUNT(*) AS count FROM /usertable/ WHERE user_email = ""

Using the select wrapper, it's

$res = $dbr->select( 'user', 'COUNT(*) AS count', array( 'user_email' =>  ) );

Other queries[edit]

For more complex queries, use the query() function. Beware table naming, however, and fetch the precise table name using the tableName() function. To fetch a lot, use tableNames() instead.

LinkBatch[edit]

Messages[edit]

Retrieving the text of[edit]

Using in extensions[edit]

Output[edit]

Adding wikitext to the page[edit]

Adding HTML to the page[edit]

Titles[edit]

The Title class is one of the most important within MediaWiki. A title can represent a page, it's restrictions and state, and much more.

A title consists of a namespace, and a text form; the former is stored as an integer, while the latter is escaped and stored as a string. Something like User:Robchurch is converted to namespace 2, page "Robchurch" for storage.

Constructing[edit]

Titles are constructed with one of the following functions:

Title
:makeTitle( namespace, text form )

Constructs a title object given a namespace and a text form. Doesn't do a lot of checking, so be careful about passing user-supplied data to this.

Title
:makeTitleSafe( namespace, text form )

Paranoid version of the above function. Slower, but safer; use when dealing with user-supplied data.

Title
:newFromText( text )

Accepts something like User:Foo and splits it up in a nice clean fashion.

Checking existence[edit]

There's so much that can be done with the title class, I'm going to cop out with a simple example; having constructed the title for a page, it might be useful to check its existence. So use the exists() function.

$title = Title::makeTitle( NS_USER, 'Foo' );
if( $title->exists() ) {
 $wgOut->addWikiText( "[[" . $title->getPrefixedText . "]] has a user page!" );
}

Skins[edit]

Making basic links[edit]

Making complex links[edit]