Reputation: 2337
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?
Upvotes: 3
Views: 1246
Reputation: 50328
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.)
Upvotes: 6
Reputation: 1156
as long as you do not leave your wiki use
#REDIRECT [[foo]]
and instead of this page, the page foo will show up.
Upvotes: -2