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