magicword.txt 3.0 KB

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