more public accessors for Title class
authorDaniel Friesen <pub-github@nadir-seen-fire.com>
Thu, 30 Aug 2012 20:04:42 +0000 (13:04 -0700)
committerAntoine Musso <hashar@free.fr>
Mon, 1 Oct 2012 18:52:03 +0000 (20:52 +0200)
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
tests/phpunit/includes/TitleTest.php

index 838d932..3573198 100644 (file)
@@ -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 <a> tags.
index f61652d..c74daae 100644 (file)
@@ -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' ),
+               );
+       }
+
 }