API: Use message-per-value for apihelp-query+langbacklinks-param-prop
[mediawiki.git] / docs / magicword.txt
blob6b4d37ee3e2505e3b72cbf523495fe5322419d9c
1 magicword.txt
3 Magic Words are some phrases used in the wikitext. They are used for two things:
4 * Variables (like {{PAGENAME}}, {{SERVER}}, ...): part of wikitext, that looks
5   like templates but that don't accept any parameter.
6 * Parser functions (like {{fullurl:...}}, {{#special:...}}): behaves like 
7   functions and accepts parameters.
9 The localized arrays keys are the internal name, and the values are an array, 
10 whose include their case-sensitivity and their alias forms. The first form 
11 defined is used by the program, for example, when moving a page and its old name
12 should include #REDIRECT.
14 They can be added in several arrays:
15 * By adding a file to $wgExtensionMessagesFiles and defining there $magicWords.
16   This array is associative with the language code in the first dimension key
17   and then a "normal" array of magic words.
18 * Localized arrays (languages/messages/LanguageXX.php) include their different 
19   names to be used by the users.
21 To add a new variable, you should use the "MagicWordwgVariableIDs" hook to add
22 the internal name to the $magicWords array. You'll need to define the value of
23 the variable with the "ParserGetVariableValueSwitch" hook.
25 For example to add a new variable:
27 Create a file called ExtensionName.i18n.magic.php with the following contents:
28 ----
29 <?php
31 $magicWords = array();
33 $magicWords['en'] = array(
34         // Case sensitive.
35         'mag_custom' => array( 1, 'CUSTOM' ),
38 $magicWords['es'] = array(
39         'mag_custom' => array( 1, 'ADUANERO' ),
41 ----
43 $wgExtensionMessagesFiles['ExtensionNameMagic'] = __DIR__ . '/ExtensionName.i18n.magic.php';
44 $wgHooks['MagicWordwgVariableIDs'][] = 'wfAddCustomMagicWordID';
45 $wgHooks['ParserGetVariableValueSwitch'][] = 'wfGetCustomMagicWordValue';
47 function wfAddCustomMagicWordID( &$magicWords ) {
48         $magicWords[] = 'mag_custom';
49         return true;
52 function wfGetCustomMagicWordValue( &$parser, &$varCache, &$index, &$ret ){
53         if( $index == 'mag_custom' ){
54                 $ret = $varCache['mag_custom'] = "Custom value";
55         }
56         return true;
59 And to add a new parser function:
61 Create a file called ExtensionName.i18n.magic.php with the following contents:
62 ----
63 <?php
65 $magicWords = array();
67 $magicWords['en'] = array(
68         // Case insensitive.
69         'mag_custom' => array( 0, 'custom' ),
72 $magicWords['es'] = array(
73         'mag_custom' => array( 0, 'aduanero' ),
75 ----
77 $wgExtensionMessagesFiles['ExtensionNameMagic'] = __DIR__ . '/ExtensionName.i18n.magic.php';
78 $wgHooks['ParserFirstCallInit'][] = 'wfRegisterCustomMagicWord';
80 function wfRegisterCustomMagicWord( &$parser ){
81         $parser->setFunctionHook( 'mag_custom', 'wfGetCustomMagicWordValue' );
82         return true;
85 function wfGetCustomMagicWordValue( &$parser, $var1, $var2 ){
86         return "custom: var1 is $var1, var2 is $var2";
89 Note: the 'ParserFirstCallInit' hook is only available since 1.12. To work with
90 an older version, you'll need to use an extension function.
92 Online documentation (contains more informations):
93 Magic words: https://www.mediawiki.org/wiki/Manual:Magic_words
94 Variables: https://www.mediawiki.org/wiki/Manual:Variable
95 Parser functions: https://www.mediawiki.org/wiki/Manual:Parser_functions