MediaWiki extensions

InfoDabble > Tech Notes > MediaWiki > MediaWiki extensions
Jump to: navigation, search
This project is still under development.

[edit] Creating an XML-style extension

<?php
/**
 * Example.php
 * This Extension does .......
 * written by John Doe
 * http://www.johndoe.com
 * To activate the functionality of this extension include the following in your
 * LocalSettings.php file:
 * require_once('$IP/extensions/Example.php');
 */
if(! defined( 'MEDIAWIKI' ) ) {
   echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
   die( -1 );
} else {
 
   /* Replace 'validextensionclass' with one of the following:
      'specialpage' -- reserved for additions to Mediawiki Special Pages; 
      'parserhook' -- used if your extension modifies, complements, or replaces the parser functions in MediaWiki; 
      'variable' -- extension that add multiple functionality to MediaWiki; 
      'other' -- all other extensions. 
 */
   $wgExtensionCredits['validextensionclass'][] = array(
       'name' => 'Example',
       'author' =>'John Doe', 
       'url' => 'http://www.mediawiki.org/wiki/User:JDoe', www.JohnDoe.com',
       'description' => 'This Extension is an example and performs no discernable function'
       );
}
 
# Example MediaWiki extension
# with MediaWiki's extension mechanism it is possible to define
# new tags of the form
# <TAGNAME> some text </TAGNAME>
# the function registered by the extension gets the text between the
# tags as input and can transform it into arbitrary HTML code.
# Note: The output is not interpreted as WikiText but directly
#       included in the HTML output. So Wiki markup is not supported.
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/YourExtensionName.php");
 
$wgExtensionFunctions[] = "wfExampleExtension";
 
function wfExampleExtension() {
    global $wgParser;
    # register the extension with the WikiText parser
    # the first parameter is the name of the new tag.
    # In this case it defines the tag <example> ... </example>
    # the second parameter is the callback function for
    # processing the text between the tags
    $wgParser->setHook( "example", "renderExample" );
}
 
# The callback function for converting the input text to HTML output
function renderExample( $input, $argv, &$parser ) {
    # $argv is an array containing any arguments passed to the
    # extension like <example argument="foo" bar>..
    # Put this on the sandbox page:  (works in MediaWiki 1.5.5)
    #   <example argument="foo" argument2="bar">Testing text **example** in between the new tags</example>
    #  XML-style parameters in the extension tag are passed as a second parameter on your hook function,
	#  which will be an associative array of attribute => value pairs. The value strings have already had 
	#  HTML character entities decoded, so use htmlspecialchars() if you emit them back to HTML
 
    $output = "Text passed into example extension: <br/>$input";
    $output .= " <br/> and the value for the arg 'argument' is " . $argv["argument"];
    $output .= " <br/> and the value for the arg 'argument2' is: " . $argv["argument2"];
    return $output;
}
 
?>


[edit] References