From 6172b54f58b147e968bc5ab8b61d22b57044c2c4 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Tue, 16 Apr 2019 17:33:34 +0300 Subject: [PATCH] Allow spaces in TitleValue constructor Change-Id: I809731508036409fda72b271aac8a37f3ac1ef2d --- includes/title/TitleValue.php | 27 +++++++++---------- .../unit/includes/title/TitleValueTest.php | 6 +++-- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/includes/title/TitleValue.php b/includes/title/TitleValue.php index 722e5ef91d..b657f13ebd 100644 --- a/includes/title/TitleValue.php +++ b/includes/title/TitleValue.php @@ -72,26 +72,25 @@ class TitleValue implements LinkTarget { /** * Constructs a TitleValue. * - * @note TitleValue expects a valid DB key; typically, a TitleValue is constructed either - * from a database entry, or by a TitleParser. We could apply "some" normalization here, - * such as substituting spaces by underscores, but that would encourage the use of - * un-normalized text when constructing TitleValues. For constructing a TitleValue from - * user input or external sources, use a TitleParser. + * @note TitleValue expects a valid namespace and name; typically, a TitleValue is constructed + * either from a database entry, or by a TitleParser. For constructing a TitleValue from user + * input or external sources, use a TitleParser. * * @param int $namespace The namespace ID. This is not validated. - * @param string $dbkey The page title in valid DBkey form. No normalization is applied. + * @param string $title The page title in either DBkey or text form. No normalization is applied + * beyond underscore/space conversion. * @param string $fragment The fragment title. Use '' to represent the whole page. * No validation or normalization is applied. * @param string $interwiki The interwiki component * * @throws InvalidArgumentException */ - public function __construct( $namespace, $dbkey, $fragment = '', $interwiki = '' ) { + public function __construct( $namespace, $title, $fragment = '', $interwiki = '' ) { if ( !is_int( $namespace ) ) { throw new ParameterTypeException( '$namespace', 'int' ); } - if ( !is_string( $dbkey ) ) { - throw new ParameterTypeException( '$dbkey', 'string' ); + if ( !is_string( $title ) ) { + throw new ParameterTypeException( '$title', 'string' ); } if ( !is_string( $fragment ) ) { throw new ParameterTypeException( '$fragment', 'string' ); @@ -101,13 +100,13 @@ 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 !== '' || ( $fragment !== '' && $namespace === NS_MAIN ), - '$dbkey', 'should not be empty unless namespace is main and fragment is non-empty' ); + Assert::parameter( !preg_match( '/^[_ ]|[\r\n\t]|[_ ]$/', $title ), '$title', + "invalid name '$title'" ); + Assert::parameter( $title !== '' || ( $fragment !== '' && $namespace === NS_MAIN ), + '$title', 'should not be empty unless namespace is main and fragment is non-empty' ); $this->namespace = $namespace; - $this->dbkey = $dbkey; + $this->dbkey = strtr( $title, ' ', '_' ); $this->fragment = $fragment; $this->interwiki = $interwiki; } diff --git a/tests/phpunit/unit/includes/title/TitleValueTest.php b/tests/phpunit/unit/includes/title/TitleValueTest.php index cd67a9329d..b95efdfd52 100644 --- a/tests/phpunit/unit/includes/title/TitleValueTest.php +++ b/tests/phpunit/unit/includes/title/TitleValueTest.php @@ -31,6 +31,8 @@ class TitleValueTest extends \MediaWikiUnitTestCase { [ NS_MAIN, '', 'fragment', '', true, false ], [ NS_USER, 'TestThis', 'stuff', '', true, false ], [ NS_USER, 'TestThis', '', 'baz', false, true ], + [ NS_MAIN, 'foo bar', '', '', false, false ], + [ NS_MAIN, 'foo_bar', '', '', false, false ], ]; } @@ -44,7 +46,8 @@ class TitleValueTest extends \MediaWikiUnitTestCase { $this->assertEquals( $ns, $title->getNamespace() ); $this->assertTrue( $title->inNamespace( $ns ) ); - $this->assertEquals( $text, $title->getText() ); + $this->assertEquals( strtr( $text, ' ', '_' ), $title->getDbKey() ); + $this->assertEquals( strtr( $text, '_', ' ' ), $title->getText() ); $this->assertEquals( $fragment, $title->getFragment() ); $this->assertEquals( $hasFragment, $title->hasFragment() ); $this->assertEquals( $interwiki, $title->getInterwiki() ); @@ -60,7 +63,6 @@ class TitleValueTest extends \MediaWikiUnitTestCase { [ NS_MAIN, 5, 'fragment', '' ], [ NS_MAIN, null, 'fragment', '' ], [ NS_USER, '', 'fragment', '' ], - [ NS_MAIN, 'foo bar', '', '' ], [ NS_MAIN, 'bar_', '', '' ], [ NS_MAIN, '_foo', '', '' ], [ NS_MAIN, ' eek ', '', '' ], -- 2.20.1