Merge "MediaWiki.php: Redirect non-standard title urls to canonical"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 30 Jun 2015 20:22:54 +0000 (20:22 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 30 Jun 2015 20:22:54 +0000 (20:22 +0000)
includes/MediaWiki.php
tests/phpunit/includes/MediaWikiTest.php

index 932dea2..5510d35 100644 (file)
@@ -279,6 +279,8 @@ class MediaWiki {
         * - Normalise empty title:
         *   /wiki/ -> /wiki/Main
         *   /w/index.php?title= -> /wiki/Main
+        * - Normalise non-standard title urls:
+        *   /w/index.php?title=Foo_Bar -> /wiki/Foo_Bar
         * - Don't redirect anything with query parameters other than 'title' or 'action=view'.
         *
         * @return bool True if a redirect was set.
@@ -289,8 +291,6 @@ class MediaWiki {
 
                if ( $request->getVal( 'action', 'view' ) != 'view'
                        || $request->wasPosted()
-                       || ( $request->getVal( 'title' ) !== null
-                               && $title->getPrefixedDBkey() == $request->getVal( 'title' ) )
                        || count( $request->getValueNames( array( 'action', 'title' ) ) )
                        || !Hooks::run( 'TestCanonicalRedirect', array( $request, $title, $output ) )
                ) {
@@ -305,7 +305,19 @@ class MediaWiki {
                }
                // Redirect to canonical url, make it a 301 to allow caching
                $targetUrl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
-               if ( $targetUrl == $request->getFullRequestURL() ) {
+
+               if ( $targetUrl != $request->getFullRequestURL() ) {
+                       $output->setSquidMaxage( 1200 );
+                       $output->redirect( $targetUrl, '301' );
+                       return true;
+               }
+
+               // If there is no title, or the title is in a non-standard encoding, we demand
+               // a redirect. If cgi somehow changed the 'title' query to be non-standard while
+               // the url is standard, the server is misconfigured.
+               if ( $request->getVal( 'title' ) === null
+                       || $title->getPrefixedDBkey() != $request->getVal( 'title' )
+               ) {
                        $message = "Redirect loop detected!\n\n" .
                                "This means the wiki got confused about what page was " .
                                "requested; this sometimes happens when moving a wiki " .
@@ -327,9 +339,7 @@ class MediaWiki {
                        }
                        throw new HttpError( 500, $message );
                }
-               $output->setSquidMaxage( 1200 );
-               $output->redirect( $targetUrl, '301' );
-               return true;
+               return false;
        }
 
        /**
index df94d3e..e196243 100644 (file)
@@ -34,7 +34,7 @@ class MediaWikiTest extends MediaWikiTestCase {
                                'url' => 'http://example.org/w/index.php?title=Foo_Bar',
                                'query' => array( 'title' => 'Foo_Bar' ),
                                'title' => 'Foo_Bar',
-                               'redirect' => false,
+                               'redirect' => 'http://example.org/wiki/Foo_Bar',
                        ),
                        array(
                                // View: Script path with implicit title from page id
@@ -76,21 +76,21 @@ class MediaWikiTest extends MediaWikiTestCase {
                                'url' => 'http://example.org/w/?title=Foo_Bar',
                                'query' => array( 'title' => 'Foo_Bar' ),
                                'title' => 'Foo_Bar',
-                               'redirect' => false,
+                               'redirect' => 'http://example.org/wiki/Foo_Bar',
                        ),
                        array(
                                // View: Root path with escaped title
                                'url' => 'http://example.org/?title=Foo_Bar',
                                'query' => array( 'title' => 'Foo_Bar' ),
                                'title' => 'Foo_Bar',
-                               'redirect' => false,
+                               'redirect' => 'http://example.org/wiki/Foo_Bar',
                        ),
                        array(
                                // View: Canonical with redundant query
                                'url' => 'http://example.org/wiki/Foo_Bar?action=view',
                                'query' => array( 'action' => 'view' ),
                                'title' => 'Foo_Bar',
-                               'redirect' => false,
+                               'redirect' => 'http://example.org/wiki/Foo_Bar',
                        ),
                        array(
                                // Edit: Canonical view url with action query
@@ -104,7 +104,7 @@ class MediaWikiTest extends MediaWikiTestCase {
                                'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=view',
                                'query' => array( 'title' => 'Foo_Bar', 'action' => 'view' ),
                                'title' => 'Foo_Bar',
-                               'redirect' => false,
+                               'redirect' => 'http://example.org/wiki/Foo_Bar',
                        ),
                        array(
                                // Edit: Index with action query