Merge "Allow fragment-only TitleValues"

This commit is contained in:
jenkins-bot 2019-04-15 22:15:06 +00:00 committed by Gerrit Code Review
commit 9ec91e66ea
3 changed files with 7 additions and 4 deletions

View file

@ -172,8 +172,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 );
}

View 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;

View 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', '', '' ],