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. $wgHooks['MagicWordMagicWords'][]   = 'wfAddCustomMagicWord'; $wgHooks['MagicWordwgVariableIDs'][] = 'wfAddCustomMagicWordID'; $wgHooks['LanguageGetMagic'][]      = 'wfAddCustomMagicWordLang';

function wfAddCustomMagicWord(&$magicWords) { $magicWords[] = 'MAG_CURRENTUSER'; $magicWords[] = 'MAG_LOGO'; return true; }

function wfAddCustomMagicWordID(&$magicWords) { $magicWords[] = MAG_CURRENTUSER; $magicWords[] = MAG_LOGO; return true; }

function wfAddCustomMagicWordLang(&$magicWords, $langCode = 0) { $magicWords[MAG_CURRENTUSER] = array(0,'CURRENTUSER'); $magicWords[MAG_LOGO]       = array(0,'LOGO'); 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'][] = 'GetVariable'; function GetVariable(&$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
 * Tested and working on 1.9.3