Manual:Palabras mágicas

Jump to navigation Jump to search
This page is a translated version of the page Manual:Magic words and the translation is 41% complete.
Extensiones de MediaWiki

Soyagic palabras' es una técnica para mapeo una variedad de wiki cuerdas de texto a un solos ID aquello está asociado con una función. 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[] .

Las palabras mágicas predeterminadas están implementadas en CoreParserFunctions.php .

Cómo funcionan las palabras mágicas

Dondequiera que MediaWiki encuentre texto entre dobles llaves ({{XXX ...}}) decide si XXX corresponde a una variable, una función del analizador sintáctico, o una plantilla. Para hacerlo, se hace las siguientes preguntas:

  1. ¿El texto está asociado a un ID de palabra mágica? Es el primer paso para resolver la forma de marcado {{XXX...}}, Mediawiki intenta traducir "XX" al ID de una palabra mágica. La traducción se realiza de acuerdo a la tabla de $magicWords.
    • Si no hay ningún ID de palabra mágica asociado con "XXX", se asume que "XXX" es una plantilla.

  2. ¿Es una variable? En caso de que se encuentre el ID de una palabra mágica, MediaWiki procede a comprobar si tiene algún parámetro.
    • Si no se encuentra ningún parámetro, MediaWiki comprueba si el ID de la palabra mágica ha sido declarado como ID de variable. Para comprobar esto, recupera la lista de las palabras mágicas en uso llamando a MagicWord::getVariableIDs(). Este método obtiene su lista de identificadores de variables de una lista en código duro (véase Variables:Variables) y de una lista de identificadores de variables personalizadas proporcionada por todas las funciones conectadas al hook MagicWordwgVariableIDs. 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 .
      • Si el identificador de la palabra mágica ha sido clasificado como una variable, los hooks de MediaWiki ejecutan las funciones asociadas con el nombre del evento ParserGetVariableValueSwitch hasta que se encuentra uno que reconozca la palabra mágica y pueda devolver su valor.

  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.

Definir palabras mágicas

Para que las palabras mágicas haga su magia debemos definir dos cosas.

  • Un mapeo entre el wiki text el ID de una palabra mágica.

Un mapeo entre el ID de una palabra mágica y alguna función PHP que intérprete la palabra mágica.

Mapeo de wikitexto a palabra mágica 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':

Archivo Example.i18n.magic.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:

Registrar palabras mágicas

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


See Help:Magic words#Localisation for help.

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.

Véase también