Manual:Magic words

From Campaigns
Revision as of 21:58, 3 November 2021 by Lithl (talk | contribs) (1 revision imported)
Jump to navigation Jump to search

<languages/>

<translate> MediaWiki extensions</translate>

<translate> Magic words are a technique for mapping a variety of wiki text strings to a single ID that is associated with a function.</translate> <translate> Both [[<tvar name=1>Special:MyLanguage/Manual:Variables</tvar>|variables]] and [[<tvar name=2>Special:MyLanguage/Manual:Parser functions</tvar>|parser functions]] use this technique.</translate> <translate> All text mapped to that ID will be replaced with the return value of the function.</translate> <translate> The mapping between the text strings and the ID is stored in the variable <tvar name=magicWords>$magicWords</tvar> in a file that can be loaded using <tvar name=1>$wgExtensionMessagesFiles[] </tvar>.</translate>

<translate> The default magic words are implemented in <tvar name=CoreParserFunctions>CoreParserFunctions.php </tvar>.

How magic words work

Whenever MediaWiki finds text between double braces ({{XXX ...}}) it must decide whether XXX is a [[<tvar name=1>Special:MyLanguage/Manual:Variables</tvar>|variable]], [[<tvar name=2>Special:MyLanguage/Manual:Parser functions</tvar>|parser function]], or [[<tvar name=3>Special:MyLanguage/Help:Templates</tvar>|template]]. To do so, it asks a series of questions: </translate>

  1. <translate> Does it have an associated magic word ID? As a first step in resolving markup of the form {{XXX...}}, MediaWiki attempts to translate XXX to a magic word ID. The translation table is defined by $magicWords.</translate>
    • <translate> If no magic word ID is associated with XXX, XXX is presumed to be a [[<tvar name=1>Special:MyLanguage/Help:Templates</tvar>|template]].</translate>

  2. <translate> Is it a variable? If a magic word ID is found, MediaWiki next checks to see if it has any parameters.</translate>
    • <translate> If no parameters are found, MediaWiki checks to see if the magic word ID has been declared as a variable ID.</translate> <translate> To check this, it retrieves the list of magic words serving by calling MagicWord::getVariableIDs().</translate> <translate> This method gets its list of variable IDs from a hard coded list of variable IDs (see <tvar name=1>Help:Variables </tvar>) and from a list of custom variable IDs provided by all functions attached to the hook <tvar name=2>MagicWordwgVariableIDs </tvar>.</translate>
      • <translate> If the magic word ID has been classified as a variable, hooks MediaWiki calls the functions associated with the event name <tvar name=1>ParserGetVariableValueSwitch </tvar> until one is found that recognizes the magic word and can return its value.</translate>

  3. <translate> 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 [[<tvar name=1>Special:MyLanguage/Manual:Parser functions</tvar>|parser function]] or [[<tvar name=2>Special:MyLanguage/Help:Templates</tvar>|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.</translate>

<translate>

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.

Mapping wiki text to magic word IDs

</translate> <translate> The variable $magicWords is used to associate each magic word ID with a language-dependent array that describes all the text strings that mapped to the magic word ID.</translate> <translate> Important: This only sets up the back end i18n mapping, you still have to write other code to make MediaWiki use the magic word for anything</translate>

<translate> The first element of this array is an integer 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.</translate> <translate> Thus the format is <tvar name=1>$magicWords['en'] = [ 'InternalName' => [ 0, 'NameUserTypes', 'AdditionalAliasUserCanType' ] ];</tvar></translate>

<translate> This association is created by $magicWords in a file registered using <tvar name=1>$wgExtensionMessagesFiles[] </tvar>.

In the example below, a Spanish MediaWiki installation will associate the magic word ID 'MAG_CUSTOM' with "personalizado", "custom", "PERSONALIZADO", "CUSTOM" and all other case variants. In an English MediaWiki only "custom" in various case combinations will be mapped to 'MAG_CUSTOM':

File Example.i18n.magic.php: </translate>

<?php

$magicWords = [];

$magicWords['en'] = [
	'MAG_CUSTOM' => [ 0, 'custom' ],
];

$magicWords['es'] = [
	'MAG_CUSTOM' => [ 0, 'personalizado' ],
];

<translate> In part of the <tvar name=1>extension.json</tvar> file: </translate>

"ExtensionMessagesFiles": {
	"ExampleMagic": "Example.i18n.magic.php"
}

<translate> Note that "ExampleMagic" is a different to the key you would use for a plain internationalization file (normally just the title of the extension, i.e. "Example"). "Magic" has been appended deliberately so one does not overwrite the other.

In inline PHP

</translate> <translate> You can associate magic words inline in PHP rather than through a i18n file.</translate> <translate> This is useful when defining hooks in <tvar name=1>LocalSettings.php</tvar></translate>

MediaWiki\MediaWikiServices::getInstance()->getContentLanguage()->mMagicExtensions['wikicodeToHtml'] = ['MAG_CUSTOM', 'custom'];

<translate>

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 [[<tvar name=1>Special:MyLanguage/Manual:Parser functions</tvar>|parser function]] or a [[<tvar name=2>Special:MyLanguage/Manual:Variables</tvar>|variable]]. For more information, please see: </translate>

<translate>

Registering magic words

There is no explicit requirement to register magic word IDs. Registering the parser function or variables that use them is sufficient.

Localisation

</translate>

<translate> See [[<tvar name=1>Special:MyLanguage/Help:Magic words#Localisation</tvar>|Help:Magic words#Localisation]] for help.</translate>

<translate> You can read more on definition and usage of magic words for localisation at [[<tvar name=i18nJS>Special:MyLanguage/Localisation#PLURAL and GENDER support in JavaScript</tvar>|Localisation#PLURAL and GENDER support in JavaScript]], [[<tvar name=i18nNs>Special:MyLanguage/Localisation#Localizing namespaces and special page aliases</tvar>|Localisation#Localizing namespaces and special page aliases]], [[<tvar name=i18nSwitches>Special:MyLanguage/Localisation#Switches in messages…</tvar>|Localisation#Switches in messages…]]; [[<tvar name=i18nPlural>Special:MyLanguage/Localisation#Be aware of PLURAL use on all numbers</tvar>|Localisation#Be aware of PLURAL use on all numbers]], [[<tvar name=i18nGender>Special:MyLanguage/Localisation#Users have grammatical genders</tvar>|Localisation#Users have grammatical genders]], [[<tvar name=i18nSitename>Special:MyLanguage/Localisation#Avoid_.7B.7BSITENAME.7D.7D_in_messages</tvar>|Avoid {{SITENAME}} in messages]].

Behavior switches (double underscore magic words)

Behavior switches are a special type of magic word. They can be recognized by their use of double underscores (rather than double braces). For example, __NOTOC__. </translate>

<translate> These magic words typically do not output any content, but instead change the behavior of a page and/or set a page property.</translate> <translate> These magic words are listed in MagicWord::mDoubleUnderscoreIDs and also at [[<tvar name=behaviorSwitches>Special:MyLanguage/Help:Magic words#Behavior switches</tvar>|Help:Magic words#Behavior switches]].</translate> <translate> The effect of each behavior switch is defined in Parser::doDoubleUnderscore().</translate> <translate> If no specific effect is defined, the magic word will simply set a page property in the page_props table.</translate>

<translate>

See also

</translate>

  • Help:Magic words - <translate> List of Variables like {{PAGENAME}} and {{SERVER}}</translate>

[[Category:Customization techniques{{#translation:}}|Magic words]] [[Category:Magic words{{#translation:}}|Magic words]]