Extend MediaWiki with custom extensions
Takeaway: Learn to write your own custom extensions for the PHP-based MediaWiki.
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:
<file>/tmp/status</file>
We defined <file> 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.
Delivered each Tuesday, TechRepublic's free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!
SponsoredWhite Papers, Webcasts, and Downloads
- Economist Intelligence Unit whitepaper: "Enterprise Knowledge Workers: Understanding Risks and Opportunities" SAP
- Accelerating Network-based Backup Riverbed
- ShoreTel Ergonomic Phones ShoreTel
- 5 Steps to Successful IT Consolidation Riverbed
- The Road Ahead for Business Process Management SAP
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET

