Extension:Variables
From MediaWiki.org
|
Variables Release status: stable |
|
|---|---|
| Implementation | Example |
| Description | Shows how to add new built in variables |
| Author(s) | User:Nad |
| Last Version | 2007-05-19 |
| MediaWiki | 1.6.x and above |
| License | No license specified |
| Download | no link |
|
check usage (experimental) |
|
This extension is a small example snippet of code that goes into the LocalSettings.php script, and allows the addition of new built in variables. The code snippet adds two example variables, {{CURRENTUSER}} and {{LOGO}}, 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 environment. This code is based on the method described in Manual:Magic words. To add a new magic word, add its name into the array on the first line, and add a case for it in the last function (wfGetCustomVariable) to calculate and return its value when it's requested.
<?php $wgCustomVariables = array('CURRENTUSER','LOGO'); $wgHooks['MagicWordMagicWords'][] = 'wfAddCustomVariable'; $wgHooks['MagicWordwgVariableIDs'][] = 'wfAddCustomVariableID'; $wgHooks['LanguageGetMagic'][] = 'wfAddCustomVariableLang'; $wgHooks['ParserGetVariableValueSwitch'][] = 'wfGetCustomVariable'; 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; } function wfGetCustomVariable(&$parser,&$cache,&$index,&$ret) { switch ($index) { case MAG_CURRENTUSER: $parser->disableCache(); # Mark this content as uncacheable $ret = $GLOBALS['wgUser']->mName; break; case MAG_LOGO: $ret = $GLOBALS['wgLogo']; break; } return true; }
[edit] Notes
- This extension should not be confused with VariablesExtension, which allows the use of variables in the computer programming sense.
- Alternatively, it may be more orderly to put it in a separate extension file and include in LocalSettings.php, such as:
- require_once( "$IP/extensions/MyVariables/MyVariables.php" );
[edit] Testing
- It works on all 1.6.x's
- Works fine on 1.8.4 --Nad 05:14, 6 April 2007 (UTC)
- Tested and working on 1.9.3 --Nad 05:08, 6 April 2007 (UTC)
- Tested and working on 1.11.1 --DavidBiesack 16:18, 30 October 2008 (UTC)
- Tested and working on 1.13.x --80.81.171.49 10:12, 19 November 2008 (UTC)
- Tested and working on 1.14.0 --Subfader 20:17, 26 February 2009 (UTC)

