Manual:Variable
From MediaWiki.org
- This page is about creating variables. For help using default variables, see Help:Variables.
| Tag Extensions | Parser Functions | Hooks | Special Pages | Skins | Magic Words |
Variables are bits of wikitext that look like templates but have no parameters and have been assigned hard-coded values. Standard wiki markup such as {{PAGENAME}} or {{SITENAME}} are examples of variables. You can also extend wiki markup by defining your own custom variables.
The term is something of a misnomer because there is nothing variable about a variable. End users cannot change the value of a variable because it is predetermined by a bundle of php code that calculates its value. The term "variables" comes from the source of their value: a php variable or something that could be assigned to a variable, e.g. a string, a number, an expression, or a function return value.
Contents |
[edit] Defining custom variables
Variables are a special case of magic words so our first step will be to define the variable as a magic word:
- Choose a magic word id for your variable. This is only an internal identifier that is used to tie together the various parts of the variable definition: the names that appear in wiki text, and the php code that assigns a value to the variable. It is best to choose an id that will be uniquely associated with your extension and is unlikely to be confused with other magic word ids used by other extensions. A common strategy is to use something like
MAG_canonicalnamewhere canonicalname is the name under which you will register your extension (see Registering custom variables below). - Define the names that will appear in wiki text. To accomplish this, you will need to define and register a hook function with LanguageGetMagic. Your hook function will be passed an array and the hook function will add an entry whose key is the internal magic word id and whose value is an array containing all the names you want to use for your variable. Your names can be case sensitive and language dependent. See LanguageGetMagic and magic words for more information.
- Provide php code to assign a value to the variable. To accomplish this, you will need to define and register a hook function with Manual:Hooks/ParserGetVariableValueSwitch.
Note that the only difference between this process and the general process for defining magic words is the last step: defining a hook function for Manual:Hooks/ParserGetVariableValueSwitch. Parser functions have a different method for associating an id with php code. See Manual:Parser functions for more information.
[edit] Registering custom variables
This is a two step process:
- Define the variable so that it gets included in
Special:Versions. This requires adding a member to $wgExtensionCredits. For more information, please see Registering features with MediaWiki. - Declare the magic word id as a variable. To accomplish this we write and assign a hook to
MagicWordwgVariableIDs, the subject of this article. Note: this hook is only for use with variables. Do not use it to define parser functions.
[edit] Example
Note: if you wish to use this example as a coding template, please replace 'My' with something unique to your project so that there is less risk of name clashes with MediaWiki or any of its extensions. For example, if your extension was named BigExtravagantStylingTool and you were reasonably sure that no-one else had an extension with constants, variables, functions, or classes beginning 'BEST_', 'wgBEST', 'wfBEST', or even 'BEST' you might want to replace 'My' with 'BEST'.
#-------------------------------------------------- # 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 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; }
[edit] For more information
- Help:Variables - discusses default variables built into the core MediaWiki package
- Manual:Magic words - reviews the different kinds of magic words and how MediaWiki tells apart variables, parser functions, and templates.
- Markup spec