Jump to content

Manual:$wgTitle

From mediawiki.org

Details

The Title object represents the title of an article, and does all the work of translating among various forms such as plain text, URL, database key, etc. For convenience, and for historical reasons, it also represents a few features of articles that don't involve their text, such as access rights.

The Title object is initialized by the MediaWiki object, $mediaWiki.

Most places in the MediaWiki code have a title already on hand (or a sane one can be easily created). As a rule, do not use $wgTitle if it's not absolutely needed. The problem with $wgTitle is that it's not a static global like $wgRequest that always refers to the same object. Instead, $wgTitle can refer to any title at a given point, since you can't anticipate what an extension might have done with it. A good example is in SpecialPages, where you can easily call the getTitle() method, which returns a localized version of the special page's title, rather than hoping $wgTitle refers to the special page in question. In short: Do not use it. Ever.

Replacement

Generally one would use a Context object (something that either implements, or subclasses something that implements IContextSource) to get the Title object.

Which context object is available varies depending on where your code is being executed:

  • As an example, the Skin class extends ContextSource, so if your code is a method of a subclass of the Skin class, you would use $this->getTitle() inside your method to get the current title.
  • If you are in a Skin class, which extends the class BaseTemplate (e.g. the Vector skin does that), you can get the title with $this->getSkin()->getTitle();.
  • As another example, if you're writing a Tag extension, the third argument to your callback function is the Parser. This is not a ContextSource, but it does have a getTitle() method, hence the appropriate way to get the current title from such a callback is $parser->getTitle().

No replacements

Other things that you may be tempted to do but should be avoided:

  • Title::newFromText( $wgRequest->getVal( 'title' ) ) - Its entirely possible you're doing something in this request related to a Title different from the one actually requested (aka JobQueue, etc.). You're probably better off using $wgTitle than doing this.
  • RequestContext::getMain()->getTitle() - This is essentially $wgTitle, just dressed up a little, and has all the same drawbacks. If you're desperate you can use this, but you should only do it if you absolutely have to.

See also