/**
* 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' );
}
// 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;
}
[ 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 ],
];
}
$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() );
[ NS_MAIN, 5, 'fragment', '' ],
[ NS_MAIN, null, 'fragment', '' ],
[ NS_USER, '', 'fragment', '' ],
- [ NS_MAIN, 'foo bar', '', '' ],
[ NS_MAIN, 'bar_', '', '' ],
[ NS_MAIN, '_foo', '', '' ],
[ NS_MAIN, ' eek ', '', '' ],