]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - docs/magicword.txt
MediaWiki 1.16.5
[autoinstalls/mediawiki.git] / docs / magicword.txt
1 magicword.txt
2
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.
8
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.
13
14 They can be added in several arrays:
15 * LanguageGetMagic hook, by adding a new key in $magicWords array. You can get
16   language code in the $lang parameter. Use both the localized name and the 
17   English name.
18 * By adding a file to $wgExtensionMessagesFiles and defining there $magicWords.
19   This array is associative with the language code in the first dimension key
20   and then a "normal" array of magic words.
21 * Localized arrays (languages/messages/LanguageXX.php) include their different 
22   names to be used by the users.
23
24 To add a new variable, you should use the "MagicWordwgVariableIDs" hook to add
25 the internal name to the $magicWords array. You'll need to define the value of
26 the variable with the "ParserGetVariableValueSwitch" hook.
27
28 For example to add a new variable:
29
30 $wgHooks['MagicWordwgVariableIDs'][] = 'wfAddCustomMagicWordID';
31 $wgHooks['LanguageGetMagic'][] = 'wfAddCustomMagicWordLang';
32 $wgHooks['ParserGetVariableValueSwitch'][] = 'wfGetCustomMagicWordValue';
33
34 function wfAddCustomMagicWordID( &$magicWords ) {
35         $magicWords[] = 'mag_custom';
36         return true;
37 }
38
39 function wfAddCustomMagicWordLang( &$magicWords, $langCode ) {
40         switch ( $langCode ) {
41                 case 'es':
42                         $magicWords['mag_custom'] = array( 1, "ADUANERO", "CUSTOM" );
43                         break;
44                 default:
45                         $magicWords['mag_custom'] = array( 1, "CUSTOM" );
46         }
47         return true;
48 }
49
50 function wfGetCustomMagicWordValue( &$parser, &$varCache, &$index, &$ret ){
51         if( $index == 'mag_custom' ){
52                 $ret = $varCache['mag_custom'] = "Custom value";
53         }
54         return true;
55 }
56
57 And to add a new parser function:
58
59 $wgHooks['LanguageGetMagic'][] = 'wfAddCustomMagicWordLang';
60 $wgHooks['ParserFirstCallInit'][] = 'wfRegisterCustomMagicWord';
61
62 function wfAddCustomMagicWordLang( &$magicWords, $langCode ) {
63         switch ( $langCode ) {
64                 case 'es':
65                         $magicWords['mag_custom'] = array( 0, "aduanero", "custom" );
66                         break;
67                 default:
68                         $magicWords['mag_custom'] = array( 0, "custom" );
69         }
70         return true;
71 }
72
73 function wfRegisterCustomMagicWord( &$parser ){
74         $parser->setFunctionHook( 'mag_custom', 'wfGetCustomMagicWordValue' );
75         return true;
76 }
77
78 function wfGetCustomMagicWordValue( &$parser, $var1, $var2 ){
79         return "custom: var1 is $var1, var2 is $var2";
80 }
81
82 Note: the 'ParserFirstCallInit' hook is only aviable since 1.12. To work with
83 an older version, you'll need to use an extension function.
84
85 Online documentation (contains more informations):
86 Magic words: http://www.mediawiki.org/wiki/Manual:Magic_words
87 Variables: http://www.mediawiki.org/wiki/Manual:Variable
88 Parser functions: http://www.mediawiki.org/wiki/Manual:Parser_functions