globals.txt 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. globals.txt
  2. Globals are evil. The original MediaWiki code relied on globals for processing
  3. context far too often. MediaWiki development since then has been a story of
  4. slowly moving context out of global variables and into objects. Storing
  5. processing context in object member variables allows those objects to be reused
  6. in a much more flexible way. Consider the elegance of:
  7. # Generate the article HTML as if viewed by a web request
  8. $article = new Article( Title::newFromText( $t ) );
  9. $article->view();
  10. versus
  11. # Save current globals
  12. $oldTitle = $wgTitle;
  13. $oldArticle = $wgArticle;
  14. # Generate the HTML
  15. $wgTitle = Title::newFromText( $t );
  16. $wgArticle = new Article;
  17. $wgArticle->view();
  18. # Restore globals
  19. $wgTitle = $oldTitle
  20. $wgArticle = $oldArticle
  21. Some of the current MediaWiki developers have an idle fantasy that some day,
  22. globals will be eliminated from MediaWiki entirely, replaced by an application
  23. object which would be passed to constructors. Whether that would be an
  24. efficient, convenient solution remains to be seen, but certainly PHP 5 makes
  25. such object-oriented programming models easier than they were in previous
  26. versions.
  27. For the time being though, MediaWiki programmers will have to work in an
  28. environment with some global context. At the time of writing, 418 globals were
  29. initialised on startup by MediaWiki. 304 of these were configuration settings,
  30. which are documented in DefaultSettings.php. There is no comprehensive
  31. documentation for the remaining 114 globals, however some of the most important
  32. ones are listed below. They are typically initialised either in index.php or in
  33. Setup.php.
  34. $wgTitle
  35. Title object created from the request URL.
  36. $wgOut
  37. OutputPage object for HTTP response.
  38. $wgUser
  39. User object for the user associated with the current request.
  40. $wgLang
  41. Language object selected by user preferences.
  42. $wgContLang
  43. Language object associated with the wiki being viewed.
  44. $wgParser
  45. Parser object. Parser extensions register their hooks here.
  46. $wgRequest
  47. WebRequest object, to get request data
  48. $wgMemc, $messageMemc
  49. Object caches