重定向到 MediaWiki 中的 wiki 页面

发布于 2025-01-05 20:33:34 字数 93 浏览 3 评论 0原文

MediaWiki 是否有任何 API 可以使用其标题重定向到页面?

我正在编写一个 MediaWiki 扩展,它会重定向到可用的页面。谁能告诉我该怎么做?

Does MediaWiki have any API to redirect to a page using its title?

I am writing a MediaWiki extension which redirects to a page if it's available. Can anyone tell me how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2025-01-12 20:33:34

从 MediaWiki 发出 HTTP 重定向 的推荐方法是调用 redirect()< OutputPage 实例上的 /code> 方法(或者无论您传递的是哪个实例,还是全局实例$wgOut)。此方法采用 URL 和可选的 HTTP 状态代码作为参数(默认为 302)。

(您也可以直接调用 PHP header() 函数,但使用 OutputPage方法不太可能干扰其他可能也想设置特殊 HTTP 标头的代码。)

如果您拥有的是 标题对象,您可以通过调用getFullURL()来获取相应的URL。如果您只有页面名称,请将其传递给 Title::newFromText() (或在适当的情况下传递给 Title 类中的其他静态工厂方法之一)以获取 Title 对象它,如下所示:

$title = Title::newFromText( $pageName );

if ( $title ) {
    global $wgOut;
    $wgOut->redirect( $title->getFullURL() );
}
else {
    // we've got a bogus page name, deal with it somehow
}

请注意,调用 redirect() 不会中止请求,甚至不会立即发送响应代码 - 它只是设置一个导致 OutputPage 发出的内部标志适当的 HTTP 标头调用output()方法。根据您在扩展中使用的挂钩,您可能需要设置它们的返回值(和/或任何特定于挂钩的标志)来告诉 MediaWiki 无需为页面呈现任何实际内容。


诗。虽然上面的示例使用已弃用的 $wgOut 全局变量来获取 OutputPage 实例,在现代 MediaWiki 代码中,您应该从当前的 RequestContext 获取它。许多 MediaWiki 类都实现了 IContextSource 接口,包括 SpecialPage、Skin、Title、WebRequest 以及 OutputPage 本身,因此您可以从其中任何一个获取 RequestContext。 (当然,如果您已经拥有一个OutputPage对象,则应该直接使用它。)

The recommended way to issue an HTTP redirect from MediaWiki is to call the redirect() method on an OutputPage instance (either whichever instance you were passed, or the global instance $wgOut). This method takes as parameters a URL and, optionally, an HTTP status code (the default is 302).

(You could also just call the PHP header() function directly, but using the OutputPage method is less likely to interfere with other code that may also want to set special HTTP headers.)

If what you have is a Title object, you can get the corresponding URL by calling getFullURL() on it. If you just have the name of the page, pass it to Title::newFromText() (or to one of the other static factory methods in the Title class, where appropriate) to get a Title object for it, like this:

$title = Title::newFromText( $pageName );

if ( $title ) {
    global $wgOut;
    $wgOut->redirect( $title->getFullURL() );
}
else {
    // we've got a bogus page name, deal with it somehow
}

Note that calling redirect() does not abort the request, or even send the response code immediately — it just sets an internal flag that causes OutputPage to emit the appropriate HTTP headers when the output() method is called. Depending on exactly which hooks you're using in your extension, you may want to set their return value (and/or any hook-specific flags) to tell MediaWiki that there's no need to render any actual content for the page.


Ps. While the example above uses the deprecated $wgOut global variable to obtain an OutputPage instance, in modern MediaWiki code you should obtain it from the current RequestContext instead. Many MediaWiki classes implement the IContextSource interface, including SpecialPage, Skin, Title, WebRequest and also OutputPage itself, so you can obtain the RequestContext from any of those. (Of course, if you already have an OutputPage object, you should just use it directly.)

南城追梦 2025-01-12 20:33:34

只要你不离开你的维基使用

#REDIRECT [[foo]]

,而不是这个页面,页面 foo 就会显示。

as long as you do not leave your wiki use

#REDIRECT [[foo]]

and instead of this page, the page foo will show up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文