Replace Media namespace redirects with File namespace
authorAlex Ezell <aezell@wikimedia.org>
Fri, 5 Oct 2018 21:32:00 +0000 (17:32 -0400)
committerAlex Ezell <aezell@wikimedia.org>
Fri, 5 Oct 2018 22:04:24 +0000 (18:04 -0400)
If a user creates a redirect that goes to a [[Media:example.jpg]]
page, then an exception is thrown because NS_MEDIA is a virtual
namespace. This change catches this case and changes the namespace
to an NS_FILE namespace and the redirect works correctly. This
change only happens when we are dealing with a redirect so other
uses of the NS_MEDIA namespace shouldn't be affected.

Bug: T203942
Change-Id: Ia744059650e16510732a65d51b138b11cbd43eb4

includes/MediaWiki.php
tests/phpunit/includes/MediaWikiTest.php

index 4636ba3..e10a530 100644 (file)
@@ -369,6 +369,11 @@ class MediaWiki {
                        }
                        throw new HttpError( 500, $message );
                }
+               // Protect against redirects to NS_MEDIA namespace
+               // when the user probably wants NS_FILE
+               if ( $title->inNamespace( NS_MEDIA ) ) {
+                       $title->mNamespace = NS_FILE;
+               }
                $output->setCdnMaxage( 1200 );
                $output->redirect( $targetUrl, '301' );
                return true;
index d79d2cf..916a6eb 100644 (file)
@@ -134,6 +134,13 @@ class MediaWikiTest extends MediaWikiTestCase {
                                'title' => 'Double_slash',
                                'redirect' => false,
                        ],
+                       [
+                               // View: Media namespace redirect (T203942)
+                               'url' => 'http://example.org/w/index.php?title=Media:Foo_Bar',
+                               'query' => [ 'title' => 'Foo_Bar' ],
+                               'title' => 'File:Foo_Bar',
+                               'redirect' => 'http://example.org/wiki/File:Foo_Bar',
+                       ],
                ];
        }