手册:魔術字

From MediaWiki.org
Jump to navigation Jump to search
This page is a translated version of the page Manual:Magic words and the translation is 50% complete.

Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Lëtzebuergesch • ‎Türkçe • ‎dansk • ‎español • ‎français • ‎galego • ‎italiano • ‎magyar • ‎polski • ‎português do Brasil • ‎svenska • ‎русский • ‎العربية • ‎ไทย • ‎中文 • ‎日本語 • ‎한국어
OOjs UI icon puzzle-ltr.svg 扩展: 开发 标签扩展 手册:解析器函数 钩子 特殊页面 手册:皮肤 手册:魔術字 API Content models
MediaWiki擴展

魔术词是一种将各种wiki文本字符串映射到与函数关联的单个ID的技术。 变量解析器功能使用这种技术。映射到该ID的所有文本都将替换为函数的返回值。文本字符串和ID之间的映射存储在变量$magicWords中的文件中,该文件可以使用$wgExtensionMessagesFiles[]加载。

默认魔术字在CoreParserFunctions.php 中实现。

魔术字是怎样工作的

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:

  1. 它是否有关联的魔术字ID?作为解决{{XXX...}}形式标记的第一步,MediaWiki尝试将XXX翻译成魔术字ID。翻译表由$magicWords定义。
    • 如果没有魔术单词ID与“XXX”相关联,则“XXX”被假定为模板

  2. 它是变量吗?如果魔术字ID找到,MediaWiki接下来会检查它是否有任何参数。
    • 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.

  3. 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.


定义魔术字

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.

将维基文本映射到魔术字ID

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' ],
];

在extension.json文件的一部分:

"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.

将魔术字ID与PHP函数关联

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:

定义魔术字

定义魔术字ID没有明确要求。定义解析器函数或使用它们的变量就足够了。

本地化

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 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.

参见