}
/**
- * 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
*/
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() {
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.
array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
);
}
-
-
+
/**
* @dataProvider provideCasesForGetpageviewlanguage
*/
);
}
+
+ /**
+ * @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' ),
+ );
+ }
+
}