Using template variables in extension tags with Mediawiki

Mediawiki, the wiki used by wikipedia, is a powerfull wiki.

It allows you to include self made templates which you can pass variables too. A very usefull template on wikipedia is the stub template. When you add {{stub}} at the start of an article it will insert text explaining the user that the article isn’t finished yet, but rather a stub. With {{Album_infobox|Name=Sehnsucht|Cover=Sehnsucht_cover.jpg|…}} an infobox is inserted used at wikipedia for all albums.

When the features of wikipedia don’t suffice you don’t neccessarily have to hack wikipedia to add features. You can add an extension which hooks into a part of the parser to create your own logic for your own custom tags.

I created a wiki to store the lyrics which I already got on a forum and found espacially templates usefull.

However, there was a slight annoyance when working with templates. The problem is that in several cases there is a link in a template which can be ambiguous (there are two songs called the same) but you don’t want an ugly suffix after the name (like “Deliverance (Album of Opeth)” instead of “Deliverance”). To solve this you have to pass both the name of the album and the name of the page of the album, but this is annoying to do everytime.

MediaWiki knows no logic so I thought to solve this by adding some basic logic in a custom tag which either uses the same text as the page name when no custom text is specified and otherwise uses the custom text. But that just wouldn’t work.

MediaWiki, when parsing a page, first escapes all nowiki elements, extensions, comments, links and so on by a unique ID. Then parsed the format and replaced variables with their values. After that it just replaced the unique id’s back with the proper content, which in this case was the output of my extension too, which was flawed because the variables weren’t replaced before the extension tag was escaped out and also because the wiki-link ([[pagename]]) wasn’t replaced because the extension was still escaped. So I ended up with having [[{{{album}}}]] on my page instead of a Sehnsucht link.

Trying to fetch template variables inside the extension just wouldn’t work because the extension was evaluated at the moment it was already included in the main page. Avoiding this by disabling the cache would require parsing the whole template each time it is viewed which is very extensive. (each link on a page in a wiki has to be looked up when parsed to see whether it exists)

So I googled a bit around and found that Mikesch Nepomuk submitted a patch on one of the mailinglists to fix this by escaping extensions seperately after the veriables were expanded. Apparently it hadn’t been approved or maybe because it wasn’t included in my release yet so I tried to apply it but it failed. The patch was a bit too old. So I did it by hand instead of by GNUpatch and with some tinkering I got it to work.

You can download the patch for the 1.5 beta4 here, if you are interested.

And I thought I would never touch PHP again.

8 thoughts on “Using template variables in extension tags with Mediawiki”

  1. Hi! Just wondering how this patch can be installed? I’ve got a new wiki devoted to trails, and I can’t get the template to put the longitude or latitude into the awesome GoogleMap extension that I’m using.

  2. Hi! I actually tried editing my parser.php… I figured out the way your document was set up and that the “+” meant to add that line and the numbers above the sections were the numbers that those lines should be on. However, when I made the edits, i got an error saying that it was expecting an “}” or something on line 3390. My 3390 says “class ParserOutput” only…

    I’m currently running:
    * MediaWiki: 1.5.2
    * PHP: 4.3.11 (cgi-fcgi)
    * MySQL: 4.0.24-max-log

    Any idea what I could be doing wrong?

  3. Hi,

    The patch file was generated using the GNU diff tool which compared the old and the altered version. You can apply it by the GNU patch tool, or any other diff patch tool that supports that markup…

    Either you made a typo when altering it by hand, or the version on which this patch applies is just too old.

    You can find the GNU diff tools here: gnu.org/software/diffutills

    If it’s a problem with the version I could take a look, and try to fix it.

  4. Hi Bas!
    You must be a genius to work this out mate.
    I so much hope you can help me on this.
    Bas!
    Im running Mediawiki 1.10.1 and trying to get the following to work, and I cant find anything on this anywhere, so I turn to you in desperation.

    Just wanted to know if you can use an extension as a template?

    What im trying to do is the following….

    We have an extension that we can use as follows:

    * Number:
    * Accountable:
    * Impact:

    So an example in a wiki page is:

    * Number:1
    * Accountable:Simon
    * Impact:HIGH

    The reason I want to place the extension stuff in a Template is to try and use a form (SemanticForm) where the parameters of the extension are on the form itself, and the user simply has to enter values for each one, instead of typing “*Number:1” for example. Instead, a field on the form would be called “Number” with a textbox where they would type the number in. I cant for the life of me get this to work.

    If I build the template like the following:

    ———————————————————– Template:IssuesList

    *{{{Number}}}:
    *{{{Accountable}}}:
    *{{{Impact}}}:

    This doesnt work.

    Any help greatly appreciated Bas!

    Simon

  5. Sorry, in the above I forgot to include the extension tags in the template:
    覧覧覧覧覧覧覧覧覧蘭 Template:IssuesList

    *{{{Number}}}:
    *{{{Accountable}}}:
    *{{{Impact}}}:

  6. Take 2..
    覧覧覧覧覧覧覧覧覧蘭 Template:IssuesList
    {{{}}}
    *{{{Number}}}:
    *{{{Accountable}}}:
    *{{{Impact}}}:
    {{{/Issue>}}}

  7. I would also love a version of this which works with MediaWiki 1.10.0. Any ideas?

  8. Haven’t taken a look to the new MediaWiki code. But the patch is rather small and it shouldn’t be really hard work to get something equivalent running.

Leave a Reply

Your email address will not be published.