Posted By

tiw on 10/11/08


Tagged

textmate mw


Versions (?)

mw magic word


 / Published in: Other
 

  1. #--------------------------------------------------
  2. # Step 1: choose a magic word id
  3. #--------------------------------------------------
  4.  
  5. # storing the chosen id in a constant is not required
  6. # but still good programming practice - it makes
  7. # searching for all occurrences of the magic word id a
  8. # bit easier - note that the the name of the constant
  9. # and the value it is assigned don't have to have anthing
  10. # to do with each other.
  11.  
  12. define('MAG_NIFTYVAR', 'mycustomvar1');
  13.  
  14. #---------------------------------------------------
  15. # Step 2: define some words to use in wiki markup
  16. #---------------------------------------------------
  17.  
  18. $wgHooks['LanguageGetMagic'][] = 'wfMyWikiWords';
  19. function wfMyWikiWords(&$aWikiWords, &$langID) {
  20.  
  21. #tell MediaWiki that all {{NiftyVar}}, {{NIFTYVAR}},
  22. #{{CoolVar}}, {{COOLVAR}} and all case variants found
  23. #in wiki text should be mapped to magic id 'mycustomvar1'
  24. # (0 means case-insensitive)
  25. $aWikiWords[MAG_NIFTYVAR] = array(0, 'NiftyVar','CoolVar');
  26.  
  27. #must do this or you will silence every LanguageGetMagic
  28. #hook after this!
  29. return true;
  30. }
  31.  
  32. #---------------------------------------------------
  33. # Step 3: assign a value to our variable
  34. #---------------------------------------------------
  35.  
  36. $wgHooks['ParserGetVariableValueSwitch'][] = 'wfMyAssignAValue';
  37. function wfMyAssignAValue(&$parser, &$cache, &$magicWordId, &$ret) {
  38. if (MAG_NIFTYVAR == $magicWordId) {
  39. // We found a value
  40. $ret='This is a really silly value';
  41. }
  42. // We must return true for two separate reasons:
  43. // 1. To permit further callbacks to run for this hook.
  44. // They might override our value but that's life.
  45. // Returning false would prevent these future callbacks from running.
  46. // 2. At the same time, "true" indicates we found a value.
  47. // Returning false would the set variable value to null.
  48. //
  49. // In other words, true means "we found a value AND other
  50. // callbacks will run," and false means "we didn't find a value
  51. // AND abort future callbacks." It's a shame these two meanings
  52. // are mixed in the same return value. So as a rule, return
  53. // true whether we found a value or not.
  54. return true;
  55. }
  56.  
  57. #---------------------------------------------------
  58. # Step 4: register the custom variables so that it
  59. # shows up in Special:Version under the
  60. # listing of custom variables
  61. #---------------------------------------------------
  62.  
  63. $wgExtensionCredits['variable'][] = array(
  64. 'name' => 'NiftyVar',
  65. 'author' =>'John Doe',
  66. 'url' => 'http://www.mediawiki.org/wiki/Extension:NiftyVar',
  67. 'description' => 'This variable is an example and performs no discernable function'
  68. );
  69.  
  70. #---------------------------------------------------
  71. # Step 5: register wiki markup words associated with
  72. # MAG_NIFTYVAR as a variable and not some
  73. # other type of magic word
  74. #---------------------------------------------------
  75.  
  76. $wgHooks['MagicWordwgVariableIDs'][] = 'wfMyDeclareVarIds';
  77. function wfMyDeclareVarIds(&$aCustomVariableIds) {
  78.  
  79. #aCustomVariableIds is where MediaWiki wants to store its
  80. #list of custom variable ids. We oblige by adding ours:
  81. $aCustomVariableIds[] = MAG_NIFTYVAR;
  82.  
  83. #must do this or you will silence every MagicWordwgVariableIds
  84. #registered after this!
  85. return true;
  86. }
  87.  

Report this snippet  

You need to login to post a comment.