Custom MediaWiki functions

I run a wiki at work, and have done for a few year, but it’s only recently I’ve started getting down and dirty with customisation.

I wanted to add a link from the “Main Page” to the latest “News” item (which is what I’ve called our “Current Events” sidebar entry).  It looked as if I was going to just have to update a template or something manually every time we added a news item.  Then I came across custom extensions.

These are a cool way of adding your own (or other people’s) bits and bobs to your wiki.  The most basic sort of extension adds new tags (e.g., some text, or ), and there are a number of (mostly the same) examples and tutorials on how to add these.

They let you filter the surrounded text through some PHP code prior to page rendering, so that you could, for example, embed a comma-separated-list of data from some source between xxx and have the mytable extension convert that into a standard wiki table.

However, the big problem I found with it was that MediaWiki has a habit of caching the filter, and I was effectively abusing this process by adding a (not passing anything to filter) to the main page, getting the current latest item, but then never having it change when new things were added, because the tag (or more correctly, the text it ‘surrounded’) hadn’t changed so it continued to feed the last cached version.

Fortunately, there’s a little-mentioned (but once you know about it, quite often used) solution: parser functions.

These look a little like, and behave like, standard wiki templates.  However, they are evaluated every time they are referenced.  I shan’t bore you with a walk-through of one, but (if I can sort out the wretched

 tag in WordPress [I really must install a code-colour extension) I’ll give you one I made earlier.

This adds a tag (no arguments, although you must supply one:  use as {{#latestnews:0}}) that goes to the “News” page, fetches the first sub-section heading (“==”) and uses that as a link to the News page.  For more details, see the manual.

# Add hook
$wgExtensionFunctions[] = "buffyLatestNews";
$wgHooks['LanguageGetMagic'][] = 'buffyLatestNews_magic';
$wgExtensionCredits['parserhook'][] = array(
    'name' => 'buffyLatestNews',
    'author' => 'Neil Bird',
    'url' => 'http://framework/',
    'description' => 'Add "latestnews" function'
# Hook
function buffyLatestNews()
    global $wgParser;
    $wgParser->setFunctionHook( "latestnews", "buffyLatestNews_func" );
# Magic
function buffyLatestNews_magic( &$magicWords, $langCode )
    # Add the magic word
    # The first array element is case sensitive, in this case it is not case sensitive
    # All remaining elements are synonyms for our parser function
    $magicWords['latestnews'] = array( 0, 'latestnews' );
    # unless we return true, other parser functions extensions won't get loaded.
    return true;
# Do it
function buffyLatestNews_func( &$parser )
    # Disable browser caching
    global $wgOut;
    $return = '';
    # Get News
    $article = new Article( Title::newFromText( 'News' ) );
    if (isset($article))
        $body = $article->fetchContent();
    # Found it?  Look for first '==' item
    if (isset($body)  and  preg_match( '/==(.*?)==/', $body, $matches ))
        $text = trim($matches[1]);
        # Ensure we've got some text
        if (strlen($text) > 0)
            $return = $text;
    # Done
    return $return;
# vim: set sw=4 sts=4 et ai :


About fnx

PSN: the_phoenix
This entry was posted in Software and tagged , . Bookmark the permalink.

1 Response to Custom MediaWiki functions

  1. sandrar says:

    Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.

Leave a Reply

Your email address will not be published. Required fields are marked *