Extension:MyVariables

This extension is a small example snippet of code which goes into the LocalSettings.php script, and allows the addition of new built in variables. The code snippet adds two example variables, and , which serve as examples to follow when adding others for your own use.

First we have to add all our new magic words to the MediaWiki enviornment. This code is based on the method described in Manual:Magic words. The only thing you need to change here is the first line which is a list of the new magic words. $wgCustomVariables = array('CURRENTUSER','LOGO');

$wgHooks['MagicWordMagicWords'][]   = 'wfAddCustomVariable'; $wgHooks['MagicWordwgVariableIDs'][] = 'wfAddCustomVariableID'; $wgHooks['LanguageGetMagic'][]      = 'wfAddCustomVariableLang';

function wfAddCustomVariable(&$magicWords) { foreach($GLOBALS['wgCustomVariables'] as $var) $magicWords[] = "MAG_$var"; return true; }

function wfAddCustomVariableID(&$variables) { foreach($GLOBALS['wgCustomVariables'] as $var) $variables[] = constant("MAG_$var"); return true; }

function wfAddCustomVariableLang(&$langMagic, $langCode = 0) { foreach($GLOBALS['wgCustomVariables'] as $var) { $magic = "MAG_$var"; $langMagic[defined($magic) ? constant($magic) : $magic] = array(0,$var); }	return true; } Now that our new magic words are registered with the environment, we can add a function to the ParserGetVariableValueSwitch hook which can calculate and return their values when they're requested: $wgHooks['ParserGetVariableValueSwitch'][] = 'wfGetCustomVariable'; function wfGetCustomVariable(&$this,&$cache,&$index,&$ret) { switch ($index) {

case MAG_CURRENTUSER: $ret = $GLOBALS['wgUser']->mName; break;

case MAG_LOGO: $ret = $GLOBALS['wgLogo']; break;

}	return true; }

Testing

 * It works on all 1.6.x's
 * Works fine on 1.8.4
 * Tested and working on 1.9.3