Add BeforePageRedirect hook to OutputPage, allowing extensions to override redirect...
authorBrion Vibber <brion@users.mediawiki.org>
Wed, 14 Dec 2011 00:38:21 +0000 (00:38 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Wed, 14 Dec 2011 00:38:21 +0000 (00:38 +0000)
This is needed by MobileFrontend to normalize some redirects to the mobile site, such as on login.

docs/hooks.txt
includes/OutputPage.php

index 759874b..2e9e4a7 100644 (file)
@@ -599,6 +599,13 @@ $mediaWiki: Mediawiki object
 &$out: OutputPage object
 &$skin: Skin object
 
+'BeforePageRedirect': Prior to sending an HTTP redirect. Gives a chance to
+override how the redirect is output by modifying, or by returning false and
+taking over the output.
+$out: OutputPage object
+&$redirect: URL, modifiable
+&$code: HTTP code (eg '301' or '302'), modifiable
+
 'BeforeParserFetchFileAndTitle': before an image is rendered by Parser
 $parser: Parser object
 $nt: the image title
index 4495714..1efced8 100644 (file)
@@ -1918,27 +1918,34 @@ class OutputPage extends ContextSource {
                if ( $this->mRedirect != '' ) {
                        # Standards require redirect URLs to be absolute
                        $this->mRedirect = wfExpandUrl( $this->mRedirect, PROTO_CURRENT );
-                       if( $this->mRedirectCode == '301' || $this->mRedirectCode == '303' ) {
-                               if( !$wgDebugRedirects ) {
-                                       $message = HttpStatus::getMessage( $this->mRedirectCode );
-                                       $response->header( "HTTP/1.1 {$this->mRedirectCode} $message" );
+
+                       $redirect = $this->mRedirect;
+                       $code = $this->mRedirectCode;
+
+                       if( wfRunHooks( "BeforePageRedirect", $this, &$redirect, &$code ) ) {
+                               if( $code == '301' || $code == '303' ) {
+                                       if( !$wgDebugRedirects ) {
+                                               $message = HttpStatus::getMessage( $code );
+                                               $response->header( "HTTP/1.1 $code $message" );
+                                       }
+                                       $this->mLastModified = wfTimestamp( TS_RFC2822 );
+                               }
+                               if ( $wgVaryOnXFP ) {
+                                       $this->addVaryHeader( 'X-Forwarded-Proto' );
+                               }
+                               $this->sendCacheControl();
+
+                               $response->header( "Content-Type: text/html; charset=utf-8" );
+                               if( $wgDebugRedirects ) {
+                                       $url = htmlspecialchars( $redirect );
+                                       print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
+                                       print "<p>Location: <a href=\"$url\">$url</a></p>\n";
+                                       print "</body>\n</html>\n";
+                               } else {
+                                       $response->header( 'Location: ' . $redirect );
                                }
-                               $this->mLastModified = wfTimestamp( TS_RFC2822 );
-                       }
-                       if ( $wgVaryOnXFP ) {
-                               $this->addVaryHeader( 'X-Forwarded-Proto' );
-                       }
-                       $this->sendCacheControl();
-
-                       $response->header( "Content-Type: text/html; charset=utf-8" );
-                       if( $wgDebugRedirects ) {
-                               $url = htmlspecialchars( $this->mRedirect );
-                               print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
-                               print "<p>Location: <a href=\"$url\">$url</a></p>\n";
-                               print "</body>\n</html>\n";
-                       } else {
-                               $response->header( 'Location: ' . $this->mRedirect );
                        }
+
                        wfProfileOut( __METHOD__ );
                        return;
                } elseif ( $this->mStatusCode ) {