From 55a52d59156e026d225f3c78b09abbcd3085810f Mon Sep 17 00:00:00 2001 From: Daniel Friesen Date: Thu, 30 Aug 2012 13:04:42 -0700 Subject: [PATCH] more public accessors for Title class This patch adds new public accessor to the Title class. They are merely returning the already existing conventions. Added tests for the four new methods: - Title::getBaseText() - Title::getRootText() - Title::getRootTitle() - Title::getSubpageText() The later does not test with $wgNamespacesWithSubpages variants. Change-Id: I4f7230c1a5487b82d06c78a45c50436085df57be --- includes/Title.php | 83 +++++++++++++++++++++++++++- tests/phpunit/includes/TitleTest.php | 62 ++++++++++++++++++++- 2 files changed, 142 insertions(+), 3 deletions(-) diff --git a/includes/Title.php b/includes/Title.php index 838d9321c7..3573198b2c 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -1161,7 +1161,49 @@ class Title { } /** - * Get the base page name, i.e. the leftmost part before any slashes + * Get the root page name text without a namespace, i.e. the leftmost part before any slashes + * + * @par Example: + * @code + * Title::newFromText('User:Foo/Bar/Baz')->getRootText(); + * # returns: 'Foo' + * @endcode + * + * @return String Root name + * @since 1.20 + */ + public function getRootText() { + if ( !MWNamespace::hasSubpages( $this->mNamespace ) ) { + return $this->getText(); + } + + return strtok( $this->getText(), '/' ); + } + + /** + * Get the root page name title, i.e. the leftmost part before any slashes + * + * @par Example: + * @code + * Title::newFromText('User:Foo/Bar/Baz')->getRootTitle(); + * # returns: Title{User:Foo} + * @endcode + * + * @return Title Root title + * @since 1.20 + */ + public function getRootTitle() { + return Title::makeTitle( $this->getNamespace(), $this->getRootText() ); + } + + /** + * Get the base page name without a namespace, i.e. the part before the subpage name + * + * @par Example: + * @code + * Title::newFromText('User:Foo/Bar/Baz')->getBaseText(); + * # returns: 'Foo/Bar' + * @endcode * * @return String Base name */ @@ -1178,9 +1220,31 @@ class Title { return implode( '/', $parts ); } + /** + * Get the base page name title, i.e. the part before the subpage name + * + * @par Example: + * @code + * Title::newFromText('User:Foo/Bar/Baz')->getBaseTitle(); + * # returns: Title{User:Foo/Bar} + * @endcode + * + * @return Title Base title + * @since 1.20 + */ + public function getBaseTitle() { + return Title::makeTitle( $this->getNamespace(), $this->getBaseText() ); + } + /** * Get the lowest-level subpage name, i.e. the rightmost part after any slashes * + * @par Example: + * @code + * Title::newFromText('User:Foo/Bar/Baz')->getSubpageText(); + * # returns: "Baz" + * @endcode + * * @return String Subpage name */ public function getSubpageText() { @@ -1191,6 +1255,23 @@ class Title { return( $parts[count( $parts ) - 1] ); } + /** + * Get the title for a subpage of the current page + * + * @par Example: + * @code + * Title::newFromText('User:Foo/Bar/Baz')->getSubpage("Asdf"); + * # returns: Title{User:Foo/Bar/Baz/Asdf} + * @endcode + * + * @param $text String The subpage name to add to the title + * @return Title Subpage title + * @since 1.20 + */ + public function getSubpage( $text ) { + return Title::makeTitleSafe( $this->getNamespace(), $this->getText() . '/' . $text ); + } + /** * Get the HTML-escaped displayable text form. * Used for the title field in tags. diff --git a/tests/phpunit/includes/TitleTest.php b/tests/phpunit/includes/TitleTest.php index f61652df19..c74daae1df 100644 --- a/tests/phpunit/includes/TitleTest.php +++ b/tests/phpunit/includes/TitleTest.php @@ -75,8 +75,7 @@ class TitleTest extends MediaWikiTestCase { array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ) ); } - - + /** * @dataProvider provideCasesForGetpageviewlanguage */ @@ -152,4 +151,63 @@ class TitleTest extends MediaWikiTestCase { ); } + + /** + * @dataProvider provideBaseTitleCases + */ + function testExtractingBaseTextFromTitle( $title, $expected, $msg='' ) { + $title = Title::newFromText( $title ); + $this->assertEquals( $expected, + $title->getBaseText(), + $msg + ); + } + + function provideBaseTitleCases() { + return array( + # Title, expected base, optional message + array('User:John_Doe/subOne/subTwo', 'John Doe/subOne' ), + array('User:Foo/Bar/Baz', 'Foo/Bar' ), + ); + } + + /** + * @dataProvider provideRootTitleCases + */ + function testExtractingRootTextFromTitle( $title, $expected, $msg='' ) { + $title = Title::newFromText( $title ); + $this->assertEquals( $expected, + $title->getRootText(), + $msg + ); + } + + function provideRootTitleCases() { + return array( + # Title, expected base, optional message + array('User:John_Doe/subOne/subTwo', 'John Doe' ), + array('User:Foo/Bar/Baz', 'Foo' ), + ); + } + + /** + * @todo Handle $wgNamespacesWithSubpages cases + * @dataProvider provideSubpageTitleCases + */ + function testExtractingSubpageTextFromTitle( $title, $expected, $msg='' ) { + $title = Title::newFromText( $title ); + $this->assertEquals( $expected, + $title->getSubpageText(), + $msg + ); + } + + function provideSubpageTitleCases() { + return array( + # Title, expected base, optional message + array('User:John_Doe/subOne/subTwo', 'subTwo' ), + array('User:John_Doe/subOne', 'subOne' ), + ); + } + } -- 2.20.1