Search code examples
phpmediawiki

How to get Current Page URL in MediaWiki


I am trying to get the current page's url and store it to a variable in my custom skin template. I am trying to do this so I can use this url for other purposes. I am trying to do something like this

function currentpageurl() //Some Custom function
{
    $url= [something that can get current page's url in mediawiki and store it to this variable]
    .....use the $url variable for other purposes....
    ......
    .....
}

Does Mediawiki has a way that can identify the current page's url or is my only option to find the current page is by using this method?

$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

Solution

  • If you're writing a QuickTemplate subclass for a skin, you can get a Title object for the current page using $this->getSkin()->getTitle().

    Once you have the Title object, you can call getLinkURL() on it to get the URL of the page. (You probably don't want to use getPrefixedURL() as Ilya suggests, as that just gives you an URL-encoded version of the page name.) Or you can pass the Title object to Linker::link() if you want to generate a standard wikilink-style link without having to mess with URLs yourself.

    In fact, $this-getSkin() is the general way to gain access to "request-global" MediaWiki objects like the current Title, WebRequest, User, Language, OutputPage, etc. from a skin template. Specifically, the Skin class implements the IContextSource interface, which provides access to all those objects.