Manual:Magic words/ru



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

Стандартные волшебные слова реализованы в CoreParserFunctions.php.

Как работают волшебные слова
Всякий раз, когда MediaWiki находит текст между двойными скобками, необходимо решить, является ли XXX переменной, функций парсера или шаблоном. Чтобы сделать это, задается несколько вопросов:


 * 1) Есть ли связанное ID волшебного слова? В качестве первого шага разбора разметки в виде, MediaWiki пытается перевести XXX в ID волшебного слова. Таблица переводов определяется в $magicWords.
 * 2) *Если нет связанного с XXX ID волшебного слова, предполагается что XXX это шаблон.
 * 3) Переменная ли это? Если найдено ID волшебного слова, MediaWiki далее проверяет есть ли у него какие-либо параметры.
 * 4) *Если параметры не найдены, MediaWiki далее проверяет было ли ID волшебного слова определено как ID переменной. Чтобы проверить это, с помощью вызова  извлекается список волшебных слов. Этот метод получает список ID переменных из списка, который определен в исходном коде (смотрите Справка:Переменные), а также из списка с пользовательскими ID переменных, из всех функций, которые подключены к хуку (hook) MagicWordwgVariableIDs.
 * 5) **Если ID волшебного слова было классифицировано как переменная, хуки MediaWiki будут вызывать функции, связанные с именем события 'ParserGetVariableValueSwitch', пока не будет найдена та, которая распознает волшебное слово и сможет вернуть его значение.
 * 6) 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 , it is treated as a parser function and rendered using the function named  .  Otherwise, it is presumed to be a template.
 * 1) 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 , it is treated as a parser function and rendered using the function named  .  Otherwise, it is presumed to be a template.
 * 1) 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 , it is treated as a parser function and rendered using the function named  .  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.

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

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 :

Файл инициализации расширения :

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.

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:


 * Manual:Parser functions
 * Manual:Variables

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.

Локализация

 * ''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  in messages.

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

 

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

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  and also at Help:Magic words#Behavior switches. The effect of each behavior switch is defined in. If no specific effect is defined, the magic word will simply set a page property in the page_props table.

См. также

 * Help:Magic words - List of Variables like and