From d4a45f9ea8b9e68893f1742a2ff4db19e3725acd Mon Sep 17 00:00:00 2001 From: Alex Ezell Date: Tue, 9 Oct 2018 11:51:04 -0400 Subject: [PATCH] WikiPage: Fix viewing of wiki redirects to NS_MEDIA 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 | 10 +++++++++- tests/phpunit/includes/page/WikiPageDbTestBase.php | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/includes/page/WikiPage.php b/includes/page/WikiPage.php index 7c97465389..34b779eda0 100644 --- a/includes/page/WikiPage.php +++ b/includes/page/WikiPage.php @@ -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; diff --git a/tests/phpunit/includes/page/WikiPageDbTestBase.php b/tests/phpunit/includes/page/WikiPageDbTestBase.php index 67cbf58ff4..c8cb1378ef 100644 --- a/tests/phpunit/includes/page/WikiPageDbTestBase.php +++ b/tests/phpunit/includes/page/WikiPageDbTestBase.php @@ -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" + ], ]; } -- 2.20.1