Extending MediaWiki
MediaWiki is the software that drives Wikipedia, a huge online collaborative encyclopedia that is at times at the heart of a fair amount of controvery having to do with matters of the site’s reliability, given that it’s authored by Joe Public. When I’ve installed wikis before, I’ve usually gone with phpwiki, which is very simple and somewhat less intimidating than Mediawiki. My new company decided to use a wiki as the starting point for our intranet, and I decided to take the plunge and try out MediaWiki.
As it turns out, the software’s a breeze to install and a dream to use. Its interface is crisp and simple to navigate, and it’s got loads of formatting options. Among the things we wanted to add to the wiki to make it more extranet-like was our company del.icio.us feed, which contains links relevant to our plans. Naturally, rather than just showing the raw RSS feed, we wanted to parse the feed and display it as a list of links. Which presented a bit of a problem, as MediaWiki out of the box doesn’t allow parsing of code. So I couldn’t very well just put in a PHP block that’d parse the feed and convert it to a list of links.
I read around a little and confirmed my suspicion that you can write extensions for MediaWiki. It’s actually very simple. Because MediaWiki’s schtick is to parse tags, the extensions API assumes that you’re going to define a new tag to be parsed, and once you register the extension, any time you use the newly defined html-style tag in an entry, it passes the content between the tags to the extension and prints the return value to the screen in place of the tag and its contents. So for example, you could define a tag “<name>” and have its extension just return your name. Whenever the wiki’s parsing a page and runs across the <name> tag, it’ll substitute your name. In my case, I needed a tag that’d parse a PHP block. The extension I wrote is as follows:
//Add the hook function call to an array defined earlier in the wiki code execution.
$wgExtensionFunctions[] = “wfPhpParse”;//This is the hook function. It adds the tag to the wiki parser and tells it what callback function to use.
function wfPhpParse() {
global $wgParser;
# register the extension with the WikiText parser
$wgParser->setHook( “phpcode”, “renderPhpParse” );
}# The callback function for converting the input text to HTML output
function renderPhpParse( $input ) {
//Eval the code between the tags.
return eval($input);
}
Once you’ve written the extension, you add a line to LocalSettings.php in the root that includes the extension file. And that’s it. To use the extension within a wiki entry, I’d type something like the following:
<phpcode>
$txt="The date is " . date('M d, Y') . ".";
return $txt;
</phpcode>
Note that I’m building my text and returning the value. This is because a PHP eval (used in my extension) will execute the code in line unless it finds a return at the end. So if you want the eval to return its value to the extension function to be returned to the main wiki code rather than just executing in line (which will give you weird results), you need to add any output to a variable and return the variable. This extension probably wouldn’t be the best in the world to use for a public wiki because it will indiscriminately execute PHP code submitted by users, and a malicious user could, for example, add to the main page an infinite loop that’d bring your site crashing to its knees. In my case, the wiki I’m administering is for private corporate use, and all users are trusted, so I feel ok about letting them add PHP code if necessary. I provide the example merely to demonstrate how absurdly easy it is to extend an already very cool piece of software.
Torsten said,
July 15, 2007 @ 6:33 pm
I like this little extension. My Wiki, however, is more public. So I’ll just play around a bit and only allow the administrator to add PHP code.
Eric said,
July 15, 2007 @ 6:33 pm
I noticed we are both using Media Wiki and Word Press. I was sent to your site by someone in the Media Wiki forums and noticed you are running Word Press as well. I just upgraded to 1.5, it’s really neat. How’s you get your amazon wishlist on your sidebar? Come check out my wordpress blog.
RaynerApe said,
July 15, 2007 @ 6:33 pm
I have tried your extension but I am getting a Fatal Error, saying:
Fatal error: Call to undefined function: �wfphpparse�() in /home/rpgbgne/public_html/wiki/includes/Setup.php on line 251
I have followed the instruction at Meta-Wiki for adding extensions and have copied verbatim the text, but still no matter what I do, I get this error. Can you please help me? My email is raynerape @ gmail.com. Thank you in advance.
blog.anime.org.za » Extending WikiMedia said,
July 15, 2007 @ 6:33 pm
[...] ..! | Home
March 24, 2005 Extending WikiMedia
Interesting read on extending the WikiMedia code. I really thought that this function would be better docume [...]
Elad said,
July 15, 2007 @ 6:33 pm
hi there,
i tried your code and got this back:
Fatal error: Call to undefined function: �wfphpparse�() in /home/hph/public_html/b/carbonbase/wiki/includes/Setup.php on line 417
what have i done wrong ?
Epistemographer said,
July 15, 2007 @ 6:33 pm
Page-by-page authentication in MediaWiki
In the interests of centralizing information, we’ve started an internal wiki at the Center that will eventually function as a sort of dynamic handbook to our facilities, staff and projects. The choice of a wiki was ideal, because each staff…
Crit said,
July 15, 2007 @ 6:33 pm
Hello,
I would like to enable javascript for administators. How can that be done?
Crit
lawrence said,
July 15, 2007 @ 6:33 pm
RaynerApe and Elad: I’m guessing you copied and pasted the sample directly. The quotation marks in the sample aren’t normal ones; you have to delete them and replace them with normal quotation marks.
Sam said,
July 15, 2007 @ 6:33 pm
Hi, I installed this, but how can I get the PHP code to show inside the box?
If I do an echo it’ll just show at the top of the page.
Thanks.
MK said,
July 15, 2007 @ 6:33 pm
I have the same question as Sam. How can I make it appear inside the box? My idea is to display a predefined dynamic DHTML menu on certain content pages using HTML_TreeMenuXL. Can’t get it to work…
Like Your Work » Blog Archive » links for 2006-0 said,
July 15, 2007 @ 6:33 pm
[...] Two Ells » Extending MediaWiki (tags: wiki) [...]
Christian Hammers said,
July 15, 2007 @ 6:33 pm
Wow, this actually works! Thanks a lot, you saved my day
Christian Hammers said,
July 15, 2007 @ 6:33 pm
You should add a link to the following page as recent MediaWiki versions seem to try being clever
and simply caching the page as long as the PHP code does not change
http://meta.wikimedia.org/wiki/MediaWiki_extensions_FAQ#How_do_I_disable_caching_for_pages_using_my_extension.3F