From 147ec4cad8b591831eec2c84a73f60e66abf0847 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Thu, 12 May 2016 11:25:51 -0700 Subject: [PATCH] TitleFormatter: Match Title behavior for non-existent namespaces In TitleFormatter::getPrefixedDBkey(), match the Title::getPrefixedDBkey() behavior for non-existent namespaces by using an empty string for the namespace and including a leading colon. Change-Id: I195c36df69963c7409711dd97bece078f61faf77 --- includes/title/MediaWikiTitleCodec.php | 16 +++++++++++----- .../includes/title/MediaWikiTitleCodecTest.php | 5 ++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/includes/title/MediaWikiTitleCodec.php b/includes/title/MediaWikiTitleCodec.php index 5c504f3513..38e9ecdacf 100644 --- a/includes/title/MediaWikiTitleCodec.php +++ b/includes/title/MediaWikiTitleCodec.php @@ -192,12 +192,18 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { if ( $target->isExternal() ) { $key .= $target->getInterwiki() . ':'; } - $nsName = $this->getNamespaceName( - $target->getNamespace(), - $target->getText() - ); + // Try to get a namespace name, but fallback + // to empty string if it doesn't exist + try { + $nsName = $this->getNamespaceName( + $target->getNamespace(), + $target->getText() + ); + } catch ( InvalidArgumentException $e ) { + $nsName = ''; + } - if ( $nsName !== '' ) { + if ( $target->getNamespace() !== 0 ) { $key .= $nsName . ':'; } diff --git a/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php b/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php index 40065f559e..e55a3a4438 100644 --- a/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php +++ b/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php @@ -191,7 +191,10 @@ class MediaWikiTitleCodecTest extends MediaWikiTestCase { // names ending in "a" to be female. [ NS_USER, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa_Müller' ], - [ NS_MAIN, 'Remote_page', '', 'remotetestiw', 'en', 'remotetestiw:Remote_page' ] + [ NS_MAIN, 'Remote_page', '', 'remotetestiw', 'en', 'remotetestiw:Remote_page' ], + + // non-existent namespace + [ 10000000, 'Foobar', '', '', 'en', ':Foobar' ], ]; } -- 2.20.1