Extension:ExtraMagic
<?php
- Add some useful magic wordsTemplate:Php
$wgCustomVariables = array('CURRENTUSER', 'CURRENTLANG', 'CURRENTSKIN', 'IPADDRESS');
$wgHooks['MagicWordMagicWords'][] = 'wfAddCustomVariable'; $wgHooks['MagicWordwgVariableIDs'][] = 'wfAddCustomVariableID'; $wgHooks['LanguageGetMagic'][] = 'wfAddCustomVariableLang'; $wgHooks['ParserGetVariableValueSwitch'][] = 'wfGetCustomVariable';
function wfAddCustomVariable(&$magicWords) { global $wgCustomVariables; foreach($wgCustomVariables as $var) $magicWords[] = "MAG_$var"; return true; }
function wfAddCustomVariableID(&$variables) { global $wgCustomVariables; foreach($wgCustomVariables as $var) $variables[] = constant("MAG_$var"); return true; }
function wfAddCustomVariableLang(&$langMagic, $langCode = 0) { global $wgCustomVariables; foreach($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: global $wgUser; $parser->disableCache(); $ret = $wgUser->mName; break;
case MAG_CURRENTLANG: global $wgUser; $parser->disableCache(); $ret = $wgUser->getOption('language'); break;
case MAG_CURRENTSKIN: global $wgUser; $parser->disableCache(); $ret = $wgUser->getOption('skin'); break;
case MAG_IPADDRESS: $parser->disableCache(); $ret = $_SERVER['REMOTE_ADDR']; break;
} return true; }