Allow spaces in TitleValue constructor
authorAryeh Gregor <ayg@aryeh.name>
Tue, 16 Apr 2019 14:33:34 +0000 (17:33 +0300)
committerAryeh Gregor <ayg@aryeh.name>
Mon, 12 Aug 2019 12:41:01 +0000 (15:41 +0300)
Change-Id: I809731508036409fda72b271aac8a37f3ac1ef2d

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

index 722e5ef..b657f13 100644 (file)
@@ -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;
        }
index cd67a93..b95efdf 100644 (file)
@@ -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 ', '', '' ],