Allow fragment-only TitleValues
authorAryeh Gregor <ayg@aryeh.name>
Mon, 15 Apr 2019 12:44:07 +0000 (15:44 +0300)
committerAryeh Gregor <ayg@aryeh.name>
Mon, 15 Apr 2019 14:15:01 +0000 (17:15 +0300)
This matches behavior of Title, and is meaningful for creating links.
However, in other contexts such a TitleValue doesn't make sense, so
perhaps we want to write special cases in linking code instead.

Change-Id: I812ad9090463766f38ef677de318c006912b4cca

includes/title/MediaWikiTitleCodec.php
includes/title/TitleValue.php
tests/phpunit/includes/title/TitleValueTest.php

index b2c8521..599ca3a 100644 (file)
@@ -157,8 +157,9 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                // be refactored to avoid this.
                $parts = $this->splitTitleString( $filteredText, $defaultNamespace );
 
-               // Relative fragment links are not supported by TitleValue
-               if ( $parts['dbkey'] === '' ) {
+               // Fragment-only is okay, but only with no namespace
+               if ( $parts['dbkey'] === '' &&
+               ( $parts['fragment'] === '' || $parts['namespace'] !== NS_MAIN ) ) {
                        throw new MalformedTitleException( 'title-invalid-empty', $text );
                }
 
index 698bc4f..722e5ef 100644 (file)
@@ -103,7 +103,8 @@ class TitleValue implements LinkTarget {
                // Sanity check, no full validation or normalization applied here!
                Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey',
                        "invalid DB key '$dbkey'" );
-               Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' );
+               Assert::parameter( $dbkey !== '' || ( $fragment !== '' && $namespace === NS_MAIN ),
+                       '$dbkey', 'should not be empty unless namespace is main and fragment is non-empty' );
 
                $this->namespace = $namespace;
                $this->dbkey = $dbkey;
index d221b43..bbeb068 100644 (file)
@@ -28,6 +28,7 @@ class TitleValueTest extends MediaWikiTestCase {
 
        public function goodConstructorProvider() {
                return [
+                       [ NS_MAIN, '', 'fragment', '', true, false ],
                        [ NS_USER, 'TestThis', 'stuff', '', true, false ],
                        [ NS_USER, 'TestThis', '', 'baz', false, true ],
                ];
@@ -58,7 +59,7 @@ class TitleValueTest extends MediaWikiTestCase {
 
                        [ NS_MAIN, 5, 'fragment', '' ],
                        [ NS_MAIN, null, 'fragment', '' ],
-                       [ NS_MAIN, '', 'fragment', '' ],
+                       [ NS_USER, '', 'fragment', '' ],
                        [ NS_MAIN, 'foo bar', '', '' ],
                        [ NS_MAIN, 'bar_', '', '' ],
                        [ NS_MAIN, '_foo', '', '' ],