From 34582db81c9418a12721df58081c02bb05d2660a Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Mon, 15 Apr 2019 15:44:07 +0300 Subject: [PATCH] Allow fragment-only TitleValues 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 | 5 +++-- includes/title/TitleValue.php | 3 ++- tests/phpunit/includes/title/TitleValueTest.php | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/includes/title/MediaWikiTitleCodec.php b/includes/title/MediaWikiTitleCodec.php index b2c8521449..599ca3afa8 100644 --- a/includes/title/MediaWikiTitleCodec.php +++ b/includes/title/MediaWikiTitleCodec.php @@ -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 ); } diff --git a/includes/title/TitleValue.php b/includes/title/TitleValue.php index 698bc4f8be..722e5ef91d 100644 --- a/includes/title/TitleValue.php +++ b/includes/title/TitleValue.php @@ -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; diff --git a/tests/phpunit/includes/title/TitleValueTest.php b/tests/phpunit/includes/title/TitleValueTest.php index d221b431a0..bbeb068007 100644 --- a/tests/phpunit/includes/title/TitleValueTest.php +++ b/tests/phpunit/includes/title/TitleValueTest.php @@ -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', '', '' ], -- 2.20.1