Difference between revisions of "Writing mediawiki extensions"

From ThorstensHome
Jump to: navigation, search
Line 27: Line 27:
 
  </circle>
 
  </circle>
 
* And you will see a circle on the resulting wiki page. Note the circle may not be complete.
 
* And you will see a circle on the resulting wiki page. Note the circle may not be complete.
 +
 +
= Version 2 =
 +
Version 2 would look like this:
 +
<pre>
 +
<?php
 +
 +
$wgExtensionFunctions[] = "wfcirclextension";
 +
 +
function wfcirclextension()
 +
{
 +
  global $wgParser;
 +
  $wgParser->setHook("circle", "circlehtml");
 +
}
 +
 +
function circlehtml($code, $argv)
 +
{
 +
  $param=reset($argv); # get first value of array
 +
  if (!is_numeric($param)) {$param=20;};
 +
  $result="
 +
<svg>
 +
  <circle r=$param />
 +
</svg>";
 +
  $result=preg_replace("/\n/","",$result);
 +
  return $result;
 +
}
 +
</pre>

Revision as of 17:04, 10 January 2015

Here is the simplest mediawiki extension you can write:

  • in LocalSettings.php, add
include("extensions/circlextension.php");
  • create a file extensions/circlextension.php:
<?php

$wgExtensionFunctions[] = "wfcirclextension";

function wfcirclextension()
{
  global $wgParser;
  $wgParser->setHook("circle", "circlehtml");
}

function circlehtml($code, $argv)
{
  $result="<svg><circle r=50 /></svg>";
  return $result;
}
  • Now you can create a mediawiki page with the content
This is mediawiki-syntax
<circle "this goes into argv">
this goes into code
</circle>
  • And you will see a circle on the resulting wiki page. Note the circle may not be complete.

Version 2

Version 2 would look like this:

<?php

$wgExtensionFunctions[] = "wfcirclextension";

function wfcirclextension()
{
  global $wgParser;
  $wgParser->setHook("circle", "circlehtml");
}

function circlehtml($code, $argv)
{
  $param=reset($argv); # get first value of array
  if (!is_numeric($param)) {$param=20;};
  $result="
<svg>
  <circle r=$param />
</svg>";
  $result=preg_replace("/\n/","",$result);
  return $result;
}