| 1 | Magic Words {#magicword} |
| 2 | ==================================== |
| 3 | |
| 4 | Magic words are localizable keywords used in wikitext. They are used for many |
| 5 | small fragments of text, including: |
| 6 | |
| 7 | * The names of parser functions e.g. `{{urlencode:...}}` |
| 8 | * The names of variables, e.g. `{{CURRENTDAY}}` |
| 9 | * Double-underscore behavior switches, e.g. `__NOTOC__` |
| 10 | * Image link parameter names |
| 11 | |
| 12 | Magic words have a synonym list, with the canonical English word always present, |
| 13 | and a case sensitivity flag. The MagicWord class provides facilities for |
| 14 | matching a magic word by converting it to a regex. |
| 15 | |
| 16 | A magic word has a unique ID. Often, the ID is the canonical English synonym in |
| 17 | lowercase. |
| 18 | |
| 19 | To add a magic word in an extension, add a file to the **ExtensionMessagesFiles** |
| 20 | attribute in extension.json, |
| 21 | and in that file, set a variable called **$magicWords**. This array is associative |
| 22 | with the language code in the first dimension key and an ID in the second key. The |
| 23 | third level array is numerically indexed: the element with key 0 contains the case |
| 24 | sensitivity flag, with 0 for case-insensitive and 1 for case-sensitive. The |
| 25 | subsequent elements of the array are the synonyms in the relevant language. |
| 26 | |
| 27 | To add a magic word in core, add it to $magicWords in MessagesEn.php, following the |
| 28 | comment there. |
| 29 | |
| 30 | For example, to add a new parser function in an extension: create a file called |
| 31 | **ExtensionName.i18n.magic.php** with the following contents: |
| 32 | |
| 33 | ```php |
| 34 | <?php |
| 35 | |
| 36 | $magicWords = []; |
| 37 | |
| 38 | $magicWords['en'] = [ |
| 39 | // Case insensitive. |
| 40 | 'mag_custom' => [ 0, 'custom' ], |
| 41 | ]; |
| 42 | |
| 43 | $magicWords['es'] = [ |
| 44 | 'mag_custom' => [ 0, 'aduanero' ], |
| 45 | ]; |
| 46 | ``` |
| 47 | |
| 48 | Then in extension.json: |
| 49 | |
| 50 | ```json |
| 51 | { |
| 52 | "ExtensionMessagesFiles": { |
| 53 | "ExtensionNameMagic": "ExtensionName.i18n.magic.php" |
| 54 | }, |
| 55 | "Hooks": { |
| 56 | "ParserFirstCallInit": "MyExtensionHooks::onParserFirstCallInit" |
| 57 | } |
| 58 | } |
| 59 | ``` |
| 60 | |
| 61 | It is important that the key "ExtensionNameMagic" is unique. It must not be used |
| 62 | by another extension. |
| 63 | |
| 64 | And in the class file: |
| 65 | |
| 66 | ```php |
| 67 | <?php |
| 68 | |
| 69 | class MyExtensionHooks { |
| 70 | public static function onParserFirstCallInit( $parser ) { |
| 71 | $parser->setFunctionHook( 'mag_custom', [ self::class, 'expandCustom' ] ); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | public static function expandCustom( $parser, $var1, $var2 ) { |
| 76 | return "custom: var1 is $var1, var2 is $var2"; |
| 77 | } |
| 78 | } |
| 79 | ``` |
| 80 | |
| 81 | - Online documentation (contains more information): |
| 82 | - Magic words: <https://www.mediawiki.org/wiki/Manual:Magic_words> |
| 83 | - Variables: <https://www.mediawiki.org/wiki/Manual:Variable> |
| 84 | - Parser functions: <https://www.mediawiki.org/wiki/Manual:Parser_functions> |