TitleFormatter: Match Title behavior for non-existent namespaces
authorKunal Mehta <legoktm@member.fsf.org>
Thu, 12 May 2016 18:25:51 +0000 (11:25 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Thu, 12 May 2016 18:36:30 +0000 (11:36 -0700)
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
tests/phpunit/includes/title/MediaWikiTitleCodecTest.php

index 5c504f3..38e9ecd 100644 (file)
@@ -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 . ':';
                }
 
index 40065f5..e55a3a4 100644 (file)
@@ -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' ],
                ];
        }