简体   繁体   English

如何在MediaWiki中获取当前页面URL

[英]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. 我试图获取当前页面的URL并将其存储到我的自定义皮肤模板中的变量。 I am trying to do this so I can use this url for other purposes. 我试图这样做,所以我可以将此URL用于其他目的。 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? Mediawiki是否有一种方法可以识别当前页面的URL,或者我是唯一一个使用此方法查找当前页面的选项?

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

If you're writing a QuickTemplate subclass for a skin, you can get a Title object for the current page using $this->getSkin()->getTitle() . 如果您正在为皮肤编写QuickTemplate子类,则可以使用$this->getSkin()->getTitle()获取当前页面的Title对象。

Once you have the Title object, you can call getLinkURL() on it to get the URL of the page. 获得Title对象后,可以在其上调用getLinkURL()以获取页面的URL。 (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. (你可能想像Ilya建议的那样使用getPrefixedURL() ,因为它只是给你一个URL编码版本的页面名称。)或者你可以将Title对象传递给Linker::link()如果你想生成标准的wikilink风格链接,而不必自己弄乱URL。

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. 实际上, $this-getSkin()是从皮肤模板访问“请求全局”MediaWiki对象(如当前Title, WebRequestUserLanguageOutputPage$this-getSkin()的一般方法。 Specifically, the Skin class implements the IContextSource interface, which provides access to all those objects. 具体来说, Skin类实现了IContextSource接口,该接口提供对所有这些对象的访问。

Since your know current page name ($name), you can use MediaWiki Title (see http://www.mediawiki.org/wiki/Manual:Title.php ). 由于您知道当前页面名称($ name),因此可以使用MediaWiki Title(请参阅http://www.mediawiki.org/wiki/Manual:Title.php )。 As I understand, it must looks like this: 据我了解,它必须如下所示:

$title = Title::newFromText($name);
$url = $title->getPrefixedUrl();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM