In: , , , ,
On: 2006 / 05 / 30 Viewed: 203772 times
Shorter URL for this post:

Recently, I wanted to add some dynamic content to a Mediawiki page : I wanted a project page to show how many lines of code the project currently had, and therefore I wanted something like <linecount> to be converted into something special.

Piece of cake ? Piece of cake. Adding custom tags or custom complex functions to your wiki is really easy, and here is how it can be done.

Adding a custom tag

For example, let's create the HTML tag <ozh>. Declaring a custom HTML tags in Mediawiki is done in 3 steps :

  1. Register a new function :
    1. $wgExtensionFunctions[] = 'registerSampleTag';
    2. $wgExtensionCredits['parserhook'][] = array(
    3.     'name' => 'SampleTag',
    4.     'author' => 'Ozh',
    5.     'url' => '',
    6. );
  2. Tell the wiki that this function will be triggered by tag <ozh>
    1. function registerSampleTag() {
    2.     global $wgParser;
    3.     $wgParser->setHook('ozh', 'printSampleTag');
    4. }
  3. Define your special function that will take care of text enclosed in your new tag
    1. function printSampleTag($input,$params) {
    2.     foreach ($params as $key=>$value) {
    3.         $args .= "<li>$key = *$value*</li>\n";
    4.     }
    5.     return('Text : '.$input.'<br/>Arguments :<ul>'.$args.'</ul>');
    6. }

Put these 3 functions in a file named for example sampleExtension.php in directory mediawiki/extensions, then edit mediawiki/LocalSettings.php and add the following at the end of the file, before the closing PHP tag ?> :

  1. include("extensions/sampleExtension.php");

That's it. Now, whenever your write something like

  1. <ozh stuff=1 thing="someparam">some text</ozh>

it will output

  1. Text : some text
  2. Arguments :
  3.     stuff = 1
  4.     thing = someparam

Ok, not the most useful stuff, but you get the idea :)

Adding a custom complex function

Now, you want to create something more complex, for example you'd like a custom function adequately named ozh that would convert "{{ozh:34|Quake|PHP}}" converted into "is 34 years old, plays Quake and writes poor PHP".

Your custom function declaration goes through 3 similar steps :

  1. // Step 1 : add a new function to register
  2. $wgExtensionFunctions[] = 'registerSampleFunction';
  3. $wgExtensionCredits['parserhook'][] = array(
  4.     'name' => 'SampleFunction',
  5.     'author' => 'Ozh',
  6.     'url' => '',
  7. );
  9. // Step 2 : tell the Wiki that this function will be
  10. // triggered by function tag {{ozh}}
  11. function registerSampleFunction() {
  12.     global $wgParser;
  13.     $wgParser->setFunctionHook('ozh', 'printSampleFunction');
  14. }
  16. // Step 3 : now define our function
  17. function printSampleFunction(&$parser, $arg1, $arg2, $arg3) {
  18.     return("is $arg1 years old, plays $arg2 and writes poor $arg3");
  19. }

So …

How simple was it ?

For the curious readers, adding a line count from a subversion project was done with a bash script like :

  1. svn export --username joe --password 123456 --force /home/ozh/project
  2. find /home/ozh/project -name '*.*' | xargs wc -l | grep total | sed -e 's/ total//' > /home/ozh/project/line.count

The script is run periodically via cron, and a custom tag <linecount> simply calls a function that returns file_get_content('/home/ozh/project/line.count');

Shorter URL

Want to share or tweet this post? Please use this short URL:


This entry "How to Add a Custom Function to MediaWiki" was posted on 30/05/2006 at 11:58 pm and is tagged with , , , ,

3 Blablas

  1. Vinu says:

    If I want to add a RSS feed to each page of the wiki how do I do that?? Are there any plugins available?

  2. m2 says:

    This is how you can add an rss feed to your special pages:

    Find the corresponding Special page i.e. include/SpecialWantedpages.php

    Open this file and at the bottom where: function wfSpecial… is located, add on the next line: global $wgRequest;

    At the end of the function add just above the doQuery:
    if ( ! $pp->doFeed( $wgRequest->getVal( 'feed' ), $limit ) )

  3. I just spent an hour to find out how I can use wiki tags in $input. Therefore I'm adding it here to avoid that another user has to waste another hour ;-)

    My method probably works with Mediawiki >= 1.8, I tested with 1.10.

    Instead of

    1. function printSampleTag($input,$params)


    1. function printSampleTag($input,$params,$parser)

    and then call

    1. $ret = $parser->recursiveTagParse($input);

    $ret will then contain HTML code with wiki tags expanded, clickable links etc. – just as every other wiki page.

Read more ?