Manual:Magic words: Difference between revisions
m Reverted edits by 2601:8C0:0:CA60:D8D8:DBA2:7910:C4F2 (talk) to last version by Shirayuki |
mNo edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{ExtensionTypes}} | {{ExtensionTypes}} | ||
[[File:MediaWiki-extensions-icon.svg|125px|alt= | [[File:MediaWiki-extensions-icon.svg|125px|alt=MediaWiki extensions|right]] | ||
{{notice|1= | {{notice|1=Looking for a '''list of magic words''' and variables? See '''{{ll|Help: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 [[Manual:Variables|variables]] and [[Manual:Parser functions|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 the variable <code>$magicWords</code> in a file that can be loaded using {{ll|Manual:$wgExtensionMessagesFiles|$wgExtensionMessagesFiles[]}}. | |||
The default magic words are implemented in {{ll|Manual:CoreParserFunctions.php|CoreParserFunctions.php}}. | |||
The default magic words are implemented in | |||
== How magic words work == | == How magic words work == | ||
Whenever MediaWiki finds text between double braces (<code><nowiki>{{</nowiki>XXX ...}}</code>) it must decide whether XXX is a [[Manual:Variables|variable]], [[Manual:Parser functions|parser function]], or [[Help:Templates|template]]. To do so, it asks a series of questions: | |||
Whenever MediaWiki finds text between double braces (<code><nowiki>{{</nowiki>XXX ...}}</code>) it must decide whether XXX is a [[ | |||
# | # '''Does it have an associated magic word ID?''' As a first step in resolving markup of the form <code><nowiki>{{XXX...}}</nowiki></code>, MediaWiki attempts to translate ''XXX'' to a magic word ID. The translation table is defined by $magicWords. | ||
#* | #* If no magic word ID is associated with ''XXX'', ''XXX'' is presumed to be a [[Help:Templates|template]]. | ||
#:<br/> | #:<br/> | ||
# | # '''Is it a variable?''' If a magic word ID <em>is</em> 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 <code>MagicWord::getVariableIDs()</code>. This method gets its list of variable IDs from a hard coded list of variable IDs (see {{ll|Help:Variables}}) and from a list of custom variable IDs provided by all functions attached to the hook {{ll|Manual:Hooks/MagicWordwgVariableIDs|MagicWordwgVariableIDs}}. | ||
#** | #** If the magic word ID has been classified as a variable, hooks MediaWiki calls the functions associated with the event name {{ll|Manual:Hooks/ParserGetVariableValueSwitch|ParserGetVariableValueSwitch}} until one is found that recognizes the magic word and can return its value. | ||
#:<br/> | #:<br/> | ||
# | # '''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 [[Manual:Parser functions|parser function]] or [[Help:Templates|template]]. If the magic word ID is found in the list of parser functions declared via a call to <code>$wgParser->setFunctionHook($magicWordId, $renderingFunctionName)</code>, it is treated as a parser function and rendered using the function named <code>$renderingFunctionName</code>. Otherwise, it is presumed to be a template. | ||
== Defining magic words == | |||
== Defining magic words == | |||
For magic words to do their magic we must define two things: | 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 wiki text and a magic word ID | ||
* a mapping between a magic word ID and some php function that interprets the magic word. | * a mapping between a magic word ID and some php function that interprets the magic word. | ||
=== Mapping wiki text to magic word IDs === | === Mapping wiki text to magic word IDs === | ||
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. | |||
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 | |||
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. | |||
Thus the format is <code>$magicWords['en'] = [ 'InternalName' => [ 0, 'NameUserTypes', 'AdditionalAliasUserCanType' ] ];</code> | |||
This association is created by $magicWords in a file registered using {{ll|Manual:$wgExtensionMessagesFiles|$wgExtensionMessagesFiles[]}}. | |||
This association is created by $magicWords in a file registered using | |||
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': | 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 <code>Example.i18n.magic.php</code>: | File <code>Example.i18n.magic.php</code>: | ||
<syntaxhighlight lang="php"><?php | <syntaxhighlight lang="php"><?php | ||
$magicWords = []; | $magicWords = []; | ||
Line 68: | Line 54: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
In part of the extension.json file: | |||
In part of the | |||
<syntaxhighlight lang="json"> | <syntaxhighlight lang="json"> | ||
"ExtensionMessagesFiles": { | "ExtensionMessagesFiles": { | ||
Line 79: | Line 61: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
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. | 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 ==== | ==== In inline PHP ==== | ||
You can associate magic words inline in PHP rather than through a i18n file. | |||
This is useful when defining hooks in <code>LocalSettings.php</code> | |||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
Line 92: | Line 71: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Associating a magic word ID with a PHP function === | |||
=== 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 [[Manual:Parser functions|parser function]] or a [[Manual:Variables|variable]]. For more information, please see: | ||
The mechanism for associating magic word IDs with rendering functions depends on whether the magic word will be used as a [[ | |||
* {{ll|Manual:Parser functions}} | * {{ll|Manual:Parser functions}} | ||
* {{ll|Manual:Variables}} | * {{ll|Manual:Variables}} | ||
=== Registering magic words === | |||
=== Registering magic words === | |||
There is no explicit requirement to register magic word IDs. Registering the parser function or variables that use them is sufficient. | There is no explicit requirement to register magic word IDs. Registering the parser function or variables that use them is sufficient. | ||
=== Localisation === | === Localisation === | ||
:''See [[Help:Magic words#Localisation|Help:Magic words#Localisation]] for help.'' | |||
:'' | |||
You can read more on definition and usage of magic words for localisation at [[Localisation#PLURAL and GENDER support in JavaScript|Localisation#PLURAL and GENDER support in JavaScript]], [[Localisation#Localizing namespaces and special page aliases|Localisation#Localizing namespaces and special page aliases]], [[Localisation#Switches in messages…|Localisation#Switches in messages…]]; [[Localisation#Be aware of PLURAL use on all numbers|Localisation#Be aware of PLURAL use on all numbers]], [[Localisation#Users have grammatical genders|Localisation#Users have grammatical genders]], [[Localisation#Avoid_.7B.7BSITENAME.7D.7D_in_messages|<nowiki>Avoid {{SITENAME}} in messages</nowiki>]]. | |||
==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, <code><nowiki>__NOTOC__</nowiki></code>. | 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, <code><nowiki>__NOTOC__</nowiki></code>. | ||
These magic words typically do not output any content, but instead change the behavior of a page and/or set a page property. | |||
These magic words are listed in <code>MagicWord::mDoubleUnderscoreIDs</code> and also at [[Help:Magic words#Behavior switches|Help:Magic words#Behavior switches]]. | |||
The effect of each behavior switch is defined in <code>Parser::doDoubleUnderscore()</code>. | |||
If no specific effect is defined, the magic word will simply set a page property in the page_props table. | |||
==See also== | |||
==See also== | *{{ll|Help:Magic words}} - List of Variables like <nowiki>{{PAGENAME}}</nowiki> and <nowiki>{{SERVER}}</nowiki> | ||
*{{ll|Help:Magic words}} - | |||
[[Category:Customization techniques | [[Category:Customization techniques|{{PAGENAME}}]] | ||
[[Category:Magic words | [[Category:Magic words|{{PAGENAME}}]] |
Latest revision as of 16:27, 7 August 2025
Looking for a list of magic words and variables? See Help: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 the variable $magicWords
in a file that can be loaded using $wgExtensionMessagesFiles[] .
The default magic words are implemented in CoreParserFunctions.php .
How magic words work
Whenever MediaWiki finds text between double 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 translate XXX to a magic word ID. The translation table is defined by $magicWords.- 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.
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
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. 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
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.
Thus the format is $magicWords['en'] = [ 'InternalName' => [ 0, 'NameUserTypes', 'AdditionalAliasUserCanType' ] ];
This association is created by $magicWords in a file registered using $wgExtensionMessagesFiles[] .
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
:
<?php
$magicWords = [];
$magicWords['en'] = [
'MAG_CUSTOM' => [ 0, 'custom' ],
];
$magicWords['es'] = [
'MAG_CUSTOM' => [ 0, 'personalizado' ],
];
In part of the extension.json file:
"ExtensionMessagesFiles": {
"ExampleMagic": "Example.i18n.magic.php"
}
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
You can associate magic words inline in PHP rather than through a i18n file.
This is useful when defining hooks in LocalSettings.php
MediaWiki\MediaWikiServices::getInstance()->getContentLanguage()->mMagicExtensions['wikicodeToHtml'] = ['MAG_CUSTOM', 'custom'];
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:
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
- See Help:Magic words#Localisation for help.
You can read more on definition and usage of magic words for localisation at Localisation#PLURAL and GENDER support in JavaScript, Localisation#Localizing namespaces and special page aliases, Localisation#Switches in messages…; Localisation#Be aware of PLURAL use on all numbers, Localisation#Users have grammatical genders, 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__
.
These magic words typically do not output any content, but instead change the behavior of a page and/or set a page property.
These magic words are listed in MagicWord::mDoubleUnderscoreIDs
and also at Help:Magic words#Behavior switches.
The effect of each behavior switch is defined in Parser::doDoubleUnderscore()
.
If no specific effect is defined, the magic word will simply set a page property in the page_props table.
See also
- Help:Magic words - List of Variables like {{PAGENAME}} and {{SERVER}}