WikiPage: Fix viewing of wiki redirects to NS_MEDIA
authorAlex Ezell <aezell@wikimedia.org>
Tue, 9 Oct 2018 15:51:04 +0000 (11:51 -0400)
committerKrinkle <krinklemail@gmail.com>
Thu, 11 Oct 2018 01:29:47 +0000 (01:29 +0000)
If a user creates a redirect to a Media namespace title, a fatal
error is thrown on viewing such rediect because we protect against
redirecting to virtual namespaces. This fix catches this kind of
redirect and modifies the namespace to be File before the Title object
is created.

Follow-up from 613e2699.

Bug: T203942
Change-Id: Ib211d98498f635862fea6bf3e7395f4f8718b3d8

includes/page/WikiPage.php
tests/phpunit/includes/page/WikiPageDbTestBase.php

index 7c97465..34b779e 100644 (file)
@@ -987,8 +987,16 @@ class WikiPage implements Page, IDBAccessObject {
 
                // rd_fragment and rd_interwiki were added later, populate them if empty
                if ( $row && !is_null( $row->rd_fragment ) && !is_null( $row->rd_interwiki ) ) {
+                       // (T203942) We can't redirect to Media namespace because it's virtual.
+                       // We don't want to modify Title objects farther down the
+                       // line. So, let's fix this here by changing to File namespace.
+                       if ( $row->rd_namespace == NS_MEDIA ) {
+                               $namespace = NS_FILE;
+                       } else {
+                               $namespace = $row->rd_namespace;
+                       }
                        $this->mRedirectTarget = Title::makeTitle(
-                               $row->rd_namespace, $row->rd_title,
+                               $namespace, $row->rd_title,
                                $row->rd_fragment, $row->rd_interwiki
                        );
                        return $this->mRedirectTarget;
index 67cbf58..c8cb137 100644 (file)
@@ -808,6 +808,14 @@ abstract class WikiPageDbTestBase extends MediaWikiLangTestCase {
                                "#REDIRECT [[hello world]]",
                                "Hello world"
                        ],
+                       // The below added to protect against Media namespace
+                       // redirects which throw a fatal: (T203942)
+                       [
+                               'WikiPageTest_testGetRedirectTarget_3',
+                               CONTENT_MODEL_WIKITEXT,
+                               "#REDIRECT [[Media:hello_world]]",
+                               "File:Hello world"
+                       ],
                ];
        }