Petunjuk: Kata pencarian

This page is a translated version of the page Manual:Magic words and the translation is 28% complete.
Outdated translations are marked like this.
Ekstensi MediaWiki

Kata ajaib adalah teknik untuk memetakan beragam untai teks wiki ke satu kode yang terkait dengan suatu fungsi. Variabel dan fungsi parser menggunakan teknik ini. Semua teks yang dipetakan terhadap kode tersebut akan digantikan dengan nilai balikan fungsi. Pemetaan antara untai teks dan ID disimpan dalam variabel $magicWords di dalam satu berkas yang dapat dimuat menggunakan $wgExtensionMessagesFiles[] .

Kata ajaib bawaan diimplementasikan di CoreParserFunctions.php .

Bagaimana kata pencarian bekerja

Setiap kali MediaWiki menemukan teks di antara tanda kurung kurawal ganda ({{XXX ...}}), MediaWiki harus menyimpulkan apakah XXX adalah sebuah variabel, fungsi parser, atau templat. Untuk melakukannya, MediaWiki melihat serangkaian pertanyaan:

  1. Apakah hal itu berkaitan dengan kata kunci ID?" Sebagai langkah awal dalam memecahkan kode yang ditandai {{XXX...}}. MediaWiki berupaya untuk menerjemahkan "XXX" ke kata kunci ID. Tabel terjemahan didefiniskan dengan $magicWords.
    • Apabila tidak ada kata kunci ID yang terdaftar di dalam "XXX". "XXX" dianggap sebagai template.

  2. Apakah itu sebuah variabel? Jika ID kata ajaib ditemukan, MediaWiki lalu memeriksa apakah itu punya parameter.
    • Jika tidak ditemukan parameter, MediaWiki memeriksa apakah ID kata ajaib telah dideklarasikan sebagai ID variabel. Untuk memeriksa ini, MediaWiki melihat daftar kata ajaib dengan memanggil MagicWord::getVariableIDs(). Metode ini mendapatkan daftar ID variabelnya dari daftar ID variabel yang dikodekan secara keras (lihat Help:Variables ) dan dari daftar ID variabel khusus yang disediakan oleh semua fungsi yang dilampirkan ke hook MagicWordwgVariableIDs
      • Jika ID kata ajaib telah diklasifikasikan sebagai sebuah variabel, kaitan MediaWiki memanggil fungsi-fungsi yang berkaitan dengan nama event ParserGetVariableValueSwitch sampai didapatkan satu yang mengeni kata Jib dan dapat mengembalikan nilainya

  3. Apakah itu fungsi parser? Jika ada parameter atau jika ID kata ajaib hilang dari daftar ID kata ajaib variabel, maka MediaWiki mengasumsikan bahwa kata ajaib itu adalah [[Special:MyLanguage/Manual:Parser functions|fungsi parser] ] atau templat. Jika ID kata ajaib ditemukan dalam daftar fungsi parser yang dideklarasikan melalui panggilan ke $wgParser->setFunctionHook($magicWordId, $renderingFunctionName), itu akan diperlakukan sebagai fungsi parser dan dirender menggunakan fungsi bernama $renderingFunctionName. Jika tidak, itu dianggap sebagai template.

By convention:

  • The magic words called variables are capitalised, case-sensitive and do not have space characters.
  • Parserfunctions are prefixed with a hash sign ({{#), are case insensitive and do not include space characters.

This is however a convention and one not consistently applied (for historic reasons).

  • Variables do not have space characters, but some translations of variables in other languages DO have spaces
  • Variables generally are capitalised and case-sensitive, but some parser functions also use this convention.
  • Some parser functions start with a hash sign, but some do not.

Where possible you should follow the conventions when defining or translating magic words. Magic words are higher in priority than templates, so any magic word defined, will block the usage of that defined name as a template.

Following the conventions avoids adding more and more potential collisions.

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 Manual:Messages API, Manual:Language#Namespaces; 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). 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 MagicWordFactory::mDoubleUnderscoreIDs and also at Help:Magic words#Behavior switches. The effect of most standard behavior switches is defined in Parser::handleDoubleUnderscore(). If no specific effect is defined, the magic word will simply set a page property in the page_props table.

Custom behavior switch

To implement a custom behavior switch.

// __CUSTOM__ behavior switch in LocalSettings.php
$wgHooks['GetDoubleUnderscoreIDs'][] = function ( &$ids ) 
{ 
	$ids[] = 'custom';
	MediaWiki\MediaWikiServices::getInstance()->getContentLanguage()->mMagicExtensions['custom'] = ['custom', 'custom'];
};
$wgHooks['ParserAfterParse'][] = function ( Parser $parser, &$text, StripState $stripState )
{
	if(isset($parser->mDoubleUnderscores['custom']))
	{
		// Do behavior switching here ...
	}
};

Lihat pula