Extension:DynamicFunctions

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
DynamicFunctions

Release status: unknown

Implementation Parser function
Description This extension defines an additional set of parser functions.
Author(s) User:Algorithm
Version 1.0
Download see below

This extension defines an additional set of parser functions that provide dynamic functionality and cannot be cached.

CAUTION: Due to bugzilla:5683 these functions may not work properly in early versions of 1.7 when the page on which they are invoked has recently been edited. To restore expected behavior, upgrade to the latest version, or install the DisableCache hack.

Contents

[edit] Functions

This module defines four functions: arg, ip, rand, and skin. All of these functions operate in constant time.

[edit] #arg:

The #arg function returns the value of the given URL argument. The syntax is:

{{#arg:parameter name|default}}.

The default parameter is optional; if defined, it is returned when the argument does not exist.

Thus, instead of including a page for given parameter values, this allows linking to a page for given parameter values. Syntax for linking: external link style with zero or more times "&parameter name=parameter value" added to the URL. The link can be to the same page with different parameter values, or to a different page. In the former case the new values can depend on the old ones.

[edit] #ip:

The #ip function returns the IP address of the current user. No arguments are required. This should correspond to the IP in the last line of Special:Version.

[edit] #rand:

The #rand function returns a random integer value. The syntax is:

{{#rand:a|b}}

#rand returns a value between a and b, inclusive. If b is omitted, #rand returns a value between 1 and a (hence, {{#rand:6}} emulates a dice roll).

The function is PHP function mt-rand.

[edit] #skin:

The #skin function returns the name of the current skin. No arguments are required.

[edit] Code

This code has been tested on MediaWiki 1.6.5 and above.

History:

  • 21 May 2006 -- v1.0 -- First stable release.
<?php
/*
 
 Defines a subset of parser functions that must clear the cache to be useful.
 
 {{#arg:name}} Returns the value of the given URL argument.  Can also be called
               with a default value, which is returned if the given argument is
               undefined or blank: {{#arg:name|default}}
 
 {{#ip:}}      Returns the current user IP.
 
 {{#rand:a|b}} Returns a random value between a and b, inclusive.  Can
               also be called with a single value; {{#rand:6}} returns a
               random value between 1 and 6 (equivalent to a dice roll).
 
 {{#skin:}}    Returns the name of the current skin.
 
 Author: Algorithm [http://meta.wikimedia.org/wiki/User:Algorithm]
 Version 1.0 (5/21/06)
 
*/
 
$wgExtensionFunctions[] = 'wfDynamicFunctions';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'DynamicFunctions v1.0',
'url' => 'http://www.mediawiki.org/wiki/Extension:DynamicFunctions',
'author' => 'Ross McClure',   
'description' => 'Defines an additional set of parser functions.'
);
 
function wfDynamicFunctions() {
    global $wgParser, $wgExtDynamicFunctions;
 
    $wgExtDynamicFunctions = new ExtDynamicFunctions();
 
    $wgParser->setFunctionHook( 'arg', array( &$wgExtDynamicFunctions, 'arg' ) );
    $wgParser->setFunctionHook( 'ip', array( &$wgExtDynamicFunctions, 'ip' ) );
    $wgParser->setFunctionHook( 'rand', array( &$wgExtDynamicFunctions, 'rand' ) );
    $wgParser->setFunctionHook( 'skin', array( &$wgExtDynamicFunctions, 'skin' ) );
}
 
class ExtDynamicFunctions
{
    function arg( &$parser, $name = '', $default = '' )
    {
        global $wgRequest;
        $parser->disableCache();
        return $wgRequest->getVal($name, $default);
    }
 
    function ip( &$parser )
    {
        $parser->disableCache();
        return wfGetIP();
    }
 
    function rand( &$parser, $a = 0, $b = 1 )
    {
        $parser->disableCache();
        return mt_rand( intval($a), intval($b) );
    }
 
    function skin( &$parser )
    {
        global $wgUser, $wgRequest;
        $parser->disableCache();
        return $wgRequest->getVal('useskin', $wgUser->getOption('skin'));
    }
}
 
?>

[edit] Update for 1.8

In 1.8 magic words have to be localisable, so the new code is:

<?php
/*
 
 Defines a subset of parser functions that must clear the cache to be useful.
 
 {{#arg:name}} Returns the value of the given URL argument.  Can also be called
               with a default value, which is returned if the given argument is
               undefined or blank: {{#arg:name|default}}
 
 {{#ip:}}      Returns the current user IP.
 
 {{#rand:a|b}} Returns a random value between a and b, inclusive.  Can
               also be called with a single value; {{#rand:6}} returns a
               random value between 1 and 6 (equivalent to a dice roll).
 
 {{#skin:}}    Returns the name of the current skin.
 
 Author: Algorithm [http://meta.wikimedia.org/wiki/User:Algorithm]
 Version 1.0 (5/21/06)
 
*/
 
$wgExtensionFunctions[] = 'wfDynamicFunctions';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'DynamicFunctions v1.0',
'url' => 'http://www.mediawiki.org/wiki/Extension:DynamicFunctions',
'author' => 'Ross McClure',   
'description' => 'Defines an additional set of parser functions.'
);
 
$wgHooks['LanguageGetMagic'][] = 'wfDynamicFunctionsLanguageGetMagic';
 
function wfDynamicFunctions() {
    global $wgParser, $wgExtDynamicFunctions;
 
    $wgExtDynamicFunctions = new ExtDynamicFunctions();
 
    $wgParser->setFunctionHook( 'arg', array( &$wgExtDynamicFunctions, 'arg' ) );
    $wgParser->setFunctionHook( 'ip', array( &$wgExtDynamicFunctions, 'ip' ) );
    $wgParser->setFunctionHook( 'rand', array( &$wgExtDynamicFunctions, 'rand' ) );
    $wgParser->setFunctionHook( 'skin', array( &$wgExtDynamicFunctions, 'skin' ) );
}
 
function wfDynamicFunctionsLanguageGetMagic( &$magicWords, $langCode ) {
    switch ( $langCode ) {
    default:
            $magicWords['arg']    = array( 0, 'arg' );
            $magicWords['ip']     = array( 0, 'ip' );
            $magicWords['rand']   = array( 0, 'rand' );
            $magicWords['skin']   = array( 0, 'skin' );
    }
    return true;
}
 
class ExtDynamicFunctions
{
 
    function arg( &$parser, $name = '', $default = '' )
    {
        global $wgRequest;
        $parser->disableCache();
        return $wgRequest->getVal($name, $default);
    }
 
    function ip( &$parser )
    {
        $parser->disableCache();
        return wfGetIP();
    }
 
    function rand( &$parser, $a = 0, $b = 1 )
    {
        $parser->disableCache();
        return mt_rand( intval($a), intval($b) );
    }
 
    function skin( &$parser )
    {
        global $wgUser, $wgRequest;
        $parser->disableCache();
        return $wgRequest->getVal('useskin', $wgUser->getOption('skin'));
    }
}
 
?>

[edit] See also

Personal tools