Make Titles with an unknown namespace ID refer to Special:Badtitle.
authordaniel <daniel.kinzler@wikimedia.de>
Fri, 9 Jun 2017 16:39:33 +0000 (18:39 +0200)
committerTim Starling <tstarling@wikimedia.org>
Wed, 14 Jun 2017 04:31:07 +0000 (04:31 +0000)
Without this patch, Title::getPrefixedText() would return ":Foo"
if the namespace was unknown, potentially creating a misleading
link to the main namespace. With this change, getPrefixedText()
will return something like "Special:Badtitle/NS12345:Foo".

Note that round trip behavior is broken either way.

Bug: T165149
Change-Id: I0d491a2b58ff45f207f83ee62ca6e7e6ffbf790a

includes/Title.php
tests/phpunit/includes/TitleTest.php

index a8cfad8..c9f09f7 100644 (file)
@@ -1419,13 +1419,22 @@ class Title implements LinkTarget {
         * @return string The prefixed text
         */
        private function prefix( $name ) {
+               global $wgContLang;
+
                $p = '';
                if ( $this->isExternal() ) {
                        $p = $this->mInterwiki . ':';
                }
 
                if ( 0 != $this->mNamespace ) {
-                       $p .= $this->getNsText() . ':';
+                       $nsText = $this->getNsText();
+
+                       if ( $nsText === false ) {
+                               // See T165149. Awkward, but better than erroneously linking to the main namespace.
+                               $nsText = $wgContLang->getNsText( NS_SPECIAL ) . ":Badtitle/NS{$this->mNamespace}";
+                       }
+
+                       $p .= $nsText . ':';
                }
                return $p . $name;
        }
index 238b65f..6c44999 100644 (file)
@@ -716,28 +716,33 @@ class TitleTest extends MediaWikiTestCase {
                return [
                        // ns = 0
                        [
-                               Title::makeTitle( NS_MAIN, 'Foobar' ),
-                               'Foobar'
+                               Title::makeTitle( NS_MAIN, 'Foo bar' ),
+                               'Foo bar'
                        ],
                        // ns = 2
                        [
-                               Title::makeTitle( NS_USER, 'Foobar' ),
-                               'User:Foobar'
+                               Title::makeTitle( NS_USER, 'Foo bar' ),
+                               'User:Foo bar'
+                       ],
+                       // ns = 3
+                       [
+                               Title::makeTitle( NS_USER_TALK, 'Foo bar' ),
+                               'User talk:Foo bar'
                        ],
                        // fragment not included
                        [
-                               Title::makeTitle( NS_MAIN, 'Foobar', 'fragment' ),
-                               'Foobar'
+                               Title::makeTitle( NS_MAIN, 'Foo bar', 'fragment' ),
+                               'Foo bar'
                        ],
                        // ns = -2
                        [
-                               Title::makeTitle( NS_MEDIA, 'Foobar' ),
-                               'Media:Foobar'
+                               Title::makeTitle( NS_MEDIA, 'Foo bar' ),
+                               'Media:Foo bar'
                        ],
                        // non-existent namespace
                        [
-                               Title::makeTitle( 100000, 'Foobar' ),
-                               ':Foobar'
+                               Title::makeTitle( 100777, 'Foo bar' ),
+                               'Special:Badtitle/NS100777:Foo bar'
                        ],
                ];
        }
@@ -749,4 +754,47 @@ class TitleTest extends MediaWikiTestCase {
        public function testGetPrefixedText( Title $title, $expected ) {
                $this->assertEquals( $expected, $title->getPrefixedText() );
        }
+
+       public function provideGetPrefixedDBKey() {
+               return [
+                       // ns = 0
+                       [
+                               Title::makeTitle( NS_MAIN, 'Foo_bar' ),
+                               'Foo_bar'
+                       ],
+                       // ns = 2
+                       [
+                               Title::makeTitle( NS_USER, 'Foo_bar' ),
+                               'User:Foo_bar'
+                       ],
+                       // ns = 3
+                       [
+                               Title::makeTitle( NS_USER_TALK, 'Foo_bar' ),
+                               'User_talk:Foo_bar'
+                       ],
+                       // fragment not included
+                       [
+                               Title::makeTitle( NS_MAIN, 'Foo_bar', 'fragment' ),
+                               'Foo_bar'
+                       ],
+                       // ns = -2
+                       [
+                               Title::makeTitle( NS_MEDIA, 'Foo_bar' ),
+                               'Media:Foo_bar'
+                       ],
+                       // non-existent namespace
+                       [
+                               Title::makeTitle( 100777, 'Foo_bar' ),
+                               'Special:Badtitle/NS100777:Foo_bar'
+                       ],
+               ];
+       }
+
+       /**
+        * @covers Title::getPrefixedDBKey
+        * @dataProvider provideGetPrefixedDBKey
+        */
+       public function testGetPrefixedDBKey( Title $title, $expected ) {
+               $this->assertEquals( $expected, $title->getPrefixedDBkey() );
+       }
 }