/ Published in: Other
Expand |
Embed | Plain Text
#-------------------------------------------------- # Step 1: choose a magic word id #-------------------------------------------------- # storing the chosen id in a constant is not required # but still good programming practice - it makes # searching for all occurrences of the magic word id a # bit easier - note that the the name of the constant # and the value it is assigned don't have to have anthing # to do with each other. define('MAG_NIFTYVAR', 'mycustomvar1'); #--------------------------------------------------- # Step 2: define some words to use in wiki markup #--------------------------------------------------- $wgHooks['LanguageGetMagic'][] = 'wfMyWikiWords'; function wfMyWikiWords(&$aWikiWords, &$langID) { #tell MediaWiki that all {{NiftyVar}}, {{NIFTYVAR}}, #{{CoolVar}}, {{COOLVAR}} and all case variants found #in wiki text should be mapped to magic id 'mycustomvar1' # (0 means case-insensitive) $aWikiWords[MAG_NIFTYVAR] = array(0, 'NiftyVar','CoolVar'); #must do this or you will silence every LanguageGetMagic #hook after this! return true; } #--------------------------------------------------- # Step 3: assign a value to our variable #--------------------------------------------------- $wgHooks['ParserGetVariableValueSwitch'][] = 'wfMyAssignAValue'; function wfMyAssignAValue(&$parser, &$cache, &$magicWordId, &$ret) { if (MAG_NIFTYVAR == $magicWordId) { // We found a value $ret='This is a really silly value'; } // We must return true for two separate reasons: // 1. To permit further callbacks to run for this hook. // They might override our value but that's life. // Returning false would prevent these future callbacks from running. // 2. At the same time, "true" indicates we found a value. // Returning false would the set variable value to null. // // In other words, true means "we found a value AND other // callbacks will run," and false means "we didn't find a value // AND abort future callbacks." It's a shame these two meanings // are mixed in the same return value. So as a rule, return // true whether we found a value or not. return true; } #--------------------------------------------------- # Step 4: register the custom variables so that it # shows up in Special:Version under the # listing of custom variables #--------------------------------------------------- $wgExtensionCredits['variable'][] = array( 'name' => 'NiftyVar', 'author' =>'John Doe', 'url' => 'http://www.mediawiki.org/wiki/Extension:NiftyVar', 'description' => 'This variable is an example and performs no discernable function' ); #--------------------------------------------------- # 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_NIFTYVAR; #must do this or you will silence every MagicWordwgVariableIds #registered after this! return true; }
You need to login to post a comment.
