Extension:RuntimeVariables

MediaWiki extensions manual
OOjs UI icon advanced.svg
RuntimeVariables
Release status: unmaintained
Implementation Parser function
Description Allows request dependent storage of variables to speed up processing and make editing complex templates easier to modify.
Author(s) Rabbitfangtalk
Latest version 1.0 (2010-07-27)
MediaWiki 1.15
License None specified
Download see below
$wgRuntimeVariables
Check usage and version matrix.

What can this extension do?Edit

Allow setting, retrieving and checking set status of variables settable in wikitext.

UsageEdit

Function Use
{{ #set: variable | value }} Assign value to the given variable. Variables are case sensitive and may be anything but an empty string or whitespace. All parameters are trimmed of trailing and leading whitespace. It is possible to make any string (or numeric value) into a variable. For example, {{ #set: The quick brown fox jumped over the lazy dog and did a 360<hello>| Another randomly long string }} is completely valid.
{{ #get: variable | value if not set (optional) }} Get the value stored in the given variable. If the given variable is not set, it will return the second parameter passed to it, if anything.
{{ #isset: variable | value if set | value if not set }} Determines if the given variable is set. A variable is "set" if it has been given a value using #set: that has not been unset by #unset:. It returns the second parameter if it is set and the third parameter if it is not. The second and third parameter default to 1 and 0 respectively.
{{ #unset: variable}} Unsets the given variable.

Download instructionsEdit

Please cut and paste the code found below and place it in $IP/extensions/RuntimeVariables/RuntimeVariables.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php .

InstallationEdit

To install this extension, add the following to the end of LocalSettings.php :

require_once("$IP/extensions/RuntimeVariables/RuntimeVariables.php");

Configuration parametersEdit

  • $wgRuntimeVariables - defaults to array(). Can be set directly in LocalSettings.php to set default variables. NOTE: Set AFTER including the script or else whatever you add will be erased.

BugsEdit

This extension has not been extensively tested for bugs which is why it has beta status. If any bugs are found, please send me a message on my Wikipedia talk page and mention this extension.

CodeEdit

<?php
 
$wgHooks['ParserFirstCallInit'][] = 'rtvRuntimeVariable_Setup';
$wgHooks['LanguageGetMagic'][]       = 'rtvRuntimeVariable_Magic';
$wgRuntimeVariables = array();
$wgExtensionCredits['parserhook'][] = array(
       'name' => 'RuntimeVariables',
       'author' =>'Rabbitfang', 
       'url' => 'http://www.mediawiki.org/wiki/User:Rabbitfang', 
       'description' => 'Adds functionality to save values calculated at runtime of a page.'
       );

function rtvRuntimeVariable_Setup( &$parser ) {
	# Set a function hook associating the "example" magic word with our function
	$parser->setFunctionHook( 'get', 'rtvRuntimeVariable_Render_Get' );
	$parser->setFunctionHook( 'set', 'rtvRuntimeVariable_Render_Set' );
	$parser->setFunctionHook( 'isset', 'rtvRuntimeVariable_Render_IsSet' );
	$parser->setFunctionHook( 'unset', 'rtvRuntimeVariable_Render_UnSet' );
	return true;
}
 
function rtvRuntimeVariable_Magic( &$magicWords, $langCode ) {
        $magicWords['get'] = array( 0, 'get');
	$magicWords['set'] = array( 0, 'set');
	$magicWords['isset'] = array( 0, 'isset');
	$magicWords['unset'] = array( 0, 'unset');
        return true;
}
 
function rtvRuntimeVariable_Render_Get( $parser, $key = '',  $param2 = '') {
	global $wgRuntimeVariables;		
	$key = trim($key);
	$param2 = trim($param2);
	if ($key == '') {
		return array($param2, 'noparse' => false);
	}
	if (isset($wgRuntimeVariables[$key])) {
		return array($wgRuntimeVariables[$key], 'noparse' => false);
	} else {
		return array($param2, 'noparse' => false);
	}
}

function rtvRuntimeVariable_Render_Set( $parser, $key = '', $param2 = '') {
	global $wgRuntimeVariables;
	$key = trim($key);
	$param2 = trim($param2);
	if ($key != '')
		$wgRuntimeVariables[$key] = $param2;
        return array("", 'noparse' => true, 'isHTML' => false);
}

function rtvRuntimeVariable_Render_IsSet( $parser, $key = '', $param2 = 1, $param3 = 0) {
	global $wgRuntimeVariables;
        $key = trim($key);
	$param2 = trim($param2);
	$param3 = trim($param3);
	if (isset($wgRuntimeVariables[$key])) {
		return array($param2, 'noparse' => false);
	} else {
		return array($param3, 'noparse' => false);
	}
}

function rtvRuntimeVariable_Render_UnSet( $parser, $key = '') {
	global $wgRuntimeVariables;
        $key = trim($key);
	if ($key != '')
		unset($GLOBALS['wgRuntimeVariables'][$key]);
	return array("", 'noparse' => true, 'isHTML' => false);
}

See alsoEdit