User:Hillgentleman/MAG userLang
From MediaWiki.org
An elementary exercise
yue: 想整個新嘅魔術字 'Template:Userlanguage',出 $wgUserLang,就加下面段嘢入LocalSettings.php。 en: To create a magicword 'Template:Userlang' to show the global variable $wgUserLang, append the following code to the end of your LocalSettings.php:
################################################################################## # modified from http://www.mediawiki.org/wiki/Manual:Variable#Example #-------------------------------------------------- # Step 1: choose a magic word id #-------------------------------------------------- define('MAG_userLang', 'mycustomvar1'); #--------------------------------------------------- # Step 2: define some words to use in wiki markup #--------------------------------------------------- $wgHooks['LanguageGetMagic'][] = 'wfUserLang'; function wfUserLang(&$aWikiWords, &$langID) { $aWikiWords[MAG_userLang] = array(0, 'userLang','userLanguage'); return true; } #--------------------------------------------------- # Step 3: assign a value to our variable #--------------------------------------------------- $wgHooks['ParserGetVariableValueSwitch'][] = 'wfShowUserLang'; function wfShowUserLang(&$parser, &$cache, &$magicWordId, &$ret) { global $wgLang ; if (MAG_userLang == $magicWordId) { $ret=$wgLang->getCode(); return true; #tell MediaWiki it can stop searching - we found a value } else { return false; #go onto the next hook assigned to ParserGetVariableValueSwitch } } #--------------------------------------------------- # Step 4: register the custom variables so that it # shows up in Special:Version under the # listing of custom variables #--------------------------------------------------- $wgExtensionCredits['variable'][] = array( 'name' => 'userLang', 'author' =>'K', 'url' => 'http://www.mediawiki.org/wiki/user:hillgentleman/MAG_userLang', 'description' => 'magicword for $wgUserLang' ); #--------------------------------------------------- # Step 5: register wiki markup words associated with # MAG_NIFTYVAR as a variable and not some # other type of magic word #--------------------------------------------------- $wgHooks['MagicWordwgVariableIDs'][] = 'wfMyDeclareVarIds'; function wfMyDeclareVarIds(&$aCustomVariableIds) { #aCustomVariableIds is where MediaWiki wants to store its #list of custom variable ids. We oblige by adding ours: $aCustomVariableIds[] = MAG_userLang; return true; #must do this or you will silence every MagicWordwgVariableIds registered after this! }