Manual:Magic words
From MediaWiki.org
| Tag Extensions | Parser Functions | Hooks | Special Pages | Skins | Magic Words |
Magic words are a technique for mapping a variety of wiki text strings to a single id that is associated with a function. Both variables and parser functions use this technique. All text mapped to that id will be replaced with the return value of the function. The mapping between the text strings and the id is stored in an array passed to each function attached to the LanguageGetMagic hook.
Contents |
[edit] How magic words work
Whenever MediaWiki finds text between double curly braces ({{XXX ...}}) it must decide whether XXX is a variable, parser function, or template. To do so, it asks a series of questions:
- Does it have an associated magic word id? As a first step in resolving markup of the form
{{XXX...}}, MediaWiki attempts to translates the name XXX to a magic word id. The translation table is defined by functions attached to the Manual:Hooks/LanguageGetMagic hook.- If no magic word id is associated with XXX, XXX is presumed to be a template.
- Is it a variable? If a magic word id is found, MediaWiki next checks to see if it has any parameters.
- If no parameters are found, MediaWiki checks to see if the magic word id has been declared as a variable id. To check this, it retrieves the list of magic words serving by calling
MagicWord::getVariableIDs(). This method gets its list of variable ids from a hard coded list of variable ids (see Help:Variables) and from a list of custom variable ids provided by all functions attached to the hook MagicWordwgVariableIDs.- If the magic word id has been classified as a variable, hooks MediaWiki calls the functions associated with the event name 'ParserGetVariableValueSwitch' until one is found that recognizes the magic word and can return its value.
- If no parameters are found, MediaWiki checks to see if the magic word id has been declared as a variable id. To check this, it retrieves the list of magic words serving by calling
- Is it a parser function? If there are any parameters or if the magic word id is missing from the list of variable magic word ids, then MediaWiki assumes that the magic word is a parser function or template. If the magic word id is found in the list of parser functions declared via a call to
$wgParser->setFunctionHook($magicWordId, $renderingFunctionName), it is treated as a parser function and rendered using the function named$renderingFunctionName. Otherwise, it is presumed to be a template.
[edit] Defining magic words
For magic words to do their magic we must define two things:
- a mapping between wiki text and a magic word id
- a mapping between a magic word id and some php function that interprets the magic word.
[edit] Mapping wiki text to magic word ids
The LanguageGetMagic hook has been used to associate each magic word id with a language-dependent array that described all the text strings that mapped to the magic word id. For more information on defining hook functions, please see Manual:Hooks.
The first element of this array is a flag indicating whether or not the magic word is case sensitive. The remaining elements are a list of text that should be associated with the magic word id. If the case sensitive flag is 0, any case variant of the names in the array will match. If the case sensitive flag is 1, only exact case matches will be associated with the magic word id.
This association is created by a set of functions attached to $wgHooks['LanguageGetMagic']. Each of these functions expects two parameters:(&$magicWords, $langCode). $magicWords is an array to populate. $langCode is a w:ISO-369 2 letter language code. Implementers check the requested language, and add an element to $magicWords. The key is always the magic word id. The value is an array as described above. The return value of this method must always be true so that hooks that we registered after this one don't get ignored.
In the example below, a Spanish MediaWiki installation will associate the magic word id 'MAG_CUSTOM' with "#aduanero", "#custom", "#ADUANERO", "#CUSTOM" and all other case variants. In an English MediaWiki only "#custom" in various case combinations will be mapped to 'MAG_CUSTOM':
$wgHooks['LanguageGetMagic'][] = 'wfAddCustomMagicWordLang'; function wfAddCustomMagicWordLang( &$magicWords, $langCode ) { #choose the text to id mapping based on language switch ( $langCode ) { case 'es': $magicWords['MAG_CUSTOM'] = array( 0, "#aduanero", "#custom" ); break; default: $magicWords['MAG_CUSTOM'] = array( 0, "#custom" ); } #tell MediaWiki to go onto the next hook return true; }
Note: In MediaWiki versions prior to 1.8, Manual:Hooks/LanguageGetMagic did not pass a language parameter. To simulate this parameter, extensions backported to 1.7 or earlier can extract the language of $wgContLang on their own and proceed as normal and/or offer a language independent process for selecting the id-text mapping.
[edit] Associating a magic word id with a php function
The mechanism for associating magic word ids with rendering functions depends on whether the magic word will be used as a parser function or a variable. For more information, please see:
[edit] Registering magic words
In MediaWiki 1.8 and beyond there is no explicit requirement to register magic word ids. Registering the parser function or variables that use them is sufficient. For versions prior to 1.8, see below.
[edit] Magic words in MediaWiki versions before 1.8
MediaWiki versions prior to 1.8 differed in the following ways:
- Manual:Hooks/LanguageGetMagic did not pass a language parameter. To simulate this paramter, extensions backported to 1.7 or earlier can extract the language of $wgContLang on their own and proceed as normal and/or offer a language independent process for selecting the id-text mapping.
- Extensions that used magic words had to explicitly register their magic word ids using the hook MagicWordMagicWords. This method simply asked the implementer to supply the id of the magic word.
$wgHooks['MagicWordMagicWords'][] = 'wfAddCustomMagicWord'; function wfAddCustomMagicWord( &$magicWords ) { $magicWords[] = 'MAG_CUSTOM'; #magic word id return true; }
[edit] See also
- m:Help:Magic words
- Help:Variables - List of Variables like {{PAGENAME}} and {{SERVER}}

