Enabling PHP tags in MediaWiki

For internal Wiki’s for personal or small business use, it’s very useful to enable raw php. This of course, is a security nightmare so make sure you set it so that only people who have accounts can edit pages.

To prevent the creation of new accounts put the following into LocalSettings.php

$wgGroupPermissions['*']['createaccount'] = false;
$wgGroupPermissions['*']['edit'] = false;
$wgShowIPinHeader = false;

Then add this simple PHP tag parser to includes/Setup.php

function ParsePHPTag($Content)
{
 global $wgOut;
 $wgOut->enableClientCache(false);
 ob_start();
 eval($Content);
 $Result = ob_get_contents();
 ob_end_clean();
  return($Result);
}
$wgParser->setHook('php','ParsePHPTag');

That tells Mediawiki to pass the <php> tag’s contents to the function ParsePHPTag which will execute the code.

I use it for advanced page building as well as simple hacks such as getting around some of the other filters with code like

<php>echo "<iframe src='somepage.php' />"; </php>

If you want your privacy, you can disable anonymous page views with the following inside of LocalSettings.php

$wgGroupPermissions['*']['read'] = false;
$wgWhitelistRead = array ("Special:Userlogin");

That disables anonymous viewing of all pages except the login page.

And finally, if for some reason you need TeX support for math symbols or neat graphs, you can enable it with a rather complicated process.

  1. Make sure you have LaTeX, dvips, and ImageMagick installed on the server. You also need machine with GNU Make and Ocaml
  2. Run make in the /math directory of MediaWiki on the system with Ocaml. This will build texvc. If you can’t get access to a machine with a build process, you can try this one which I built on dreamhost so it should work there. Drop it into your MediaWiki’s /math directory
  3. set $wgUseTeX to true in your LocalSettings.php

This should enable <math> tags inside of your MediaWiki, allowing you to create stuff like
ax^2+bx+c=0 \Rightarrow x = \frac{-b \pm \sqrt{b^2 -4ac}}{2a}

Leave a Reply