Monday, April 24, 2006

 

Extend MediaWiki with custom extensions

By Vincent Danen, Tech Republic
MediaWiki is a great PHP-based wiki implementation that is used to power many sites, including Wikipedia. It's quite flexible, very secure, and extremely easy to use. It's also extremely simple to write plugins for it.
Assume for a moment you want to include the contents of an external file in your wiki. The file changes often and the wiki must reflect the current state of the underlying file. You could write a quick extension to handle this. Extensions live in the extensions/ subdirectory and while there are many extensions you can download, if you have a basic understanding of PHP, you can write your own custom extensions that will look seamless on the wiki.

The extension in Listing A will display the contents of the file passed to it via the wiki page (more on that in a moment); the file must be readable by the Web server user.

Keep in mind that this extension could be dangerous if you don't filter the input at all. In this code, we explicitly test to see if $input is one of two files: /var/lib/foo or /tmp/status. If it is neither, the $file variable is not set and remains NULL; If the passed filename is either of the two allowed files, then it is retrieved and returned to MediaWiki to display.

Add the following to LocalSettings.php to include the extension file (assuming it's called plugin_file.php):

include("./extensions/plugin_file.php");
Finally, to use this plugin in MediaWiki itself, create or edit a page in the wiki with the following code:

/tmp/status
We defined as the code to use in the plugin_file.php via the $wgParser->setHook call; we defined it as file with the associated function being renderFile, which we then defined.

As you can see, this is extremely simple, and being able to add your own custom PHP code to a site gives you limitless possibilities of extending your wiki.

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?