*/
public function getText();
+ /**
+ * Creates a new LinkTarget for a different fragment of the same page.
+ * It is expected that the same type of object will be returned, but the
+ * only requirement is that it is a LinkTarget.
+ *
+ * @param string $fragment The fragment name, or "" for the entire page.
+ *
+ * @return LinkTarget
+ */
+ public function createFragmentTarget( $fragment );
}
* specified fragment before setting, so it assumes you're passing it with
* an initial "#".
*
- * Deprecated for public use, use Title::makeTitle() with fragment parameter.
+ * Deprecated for public use, use Title::makeTitle() with fragment parameter,
+ * or Title::createFragmentTarget().
* Still in active use privately.
*
* @private
$this->mFragment = strtr( substr( $fragment, 1 ), '_', ' ' );
}
+ /**
+ * Creates a new Title for a different fragment of the same page.
+ *
+ * @since 1.27
+ * @param string $fragment
+ * @return Title
+ */
+ public function createFragmentTarget( $fragment ) {
+ return self::makeTitle(
+ $this->getNamespace(),
+ $this->getText(),
+ $fragment,
+ $this->getInterwiki()
+ );
+
+ }
+
/**
* Prefix some arbitrary text with the namespace or interwiki prefix
* of this object
/**
* Creates a new TitleValue for a different fragment of the same page.
*
+ * @since 1.27
* @param string $fragment The fragment name, or "" for the entire page.
*
* @return TitleValue
*/
- public function createFragmentTitle( $fragment ) {
+ public function createFragmentTarget( $fragment ) {
return new TitleValue( $this->namespace, $this->dbkey, $fragment );
}
'exists() should re-query database when GAID_FOR_UPDATE is used'
);
}
+
+ public function provideCreateFragmentTitle() {
+ return [
+ [ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ],
+ [ Title::makeTitle( NS_TALK, 'Test', 'foo' ), '' ],
+ [ Title::makeTitle( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
+ [ Title::makeTitle( NS_MAIN, 'Test1', '', 'interwiki' ), 'baz' ]
+ ];
+ }
+
+ /**
+ * @covers Title::createFragmentTarget
+ * @dataProvider provideCreateFragmentTitle
+ */
+ public function testCreateFragmentTitle( Title $title, $fragment ) {
+ $this->mergeMwGlobalArrayValue( 'wgHooks', [
+ 'InterwikiLoadPrefix' => [
+ function ( $prefix, &$iwdata ) {
+ if ( $prefix === 'interwiki' ) {
+ $iwdata = [
+ 'iw_url' => 'http://example.com/',
+ 'iw_local' => 0,
+ 'iw_trans' => 0,
+ ];
+ return false;
+ }
+ },
+ ],
+ ] );
+
+ $fragmentTitle = $title->createFragmentTarget( $fragment );
+
+ $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
+ $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
+ $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
+ $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
+ }
}
* @dataProvider fragmentTitleProvider
*/
public function testCreateFragmentTitle( TitleValue $title, $fragment ) {
- $fragmentTitle = $title->createFragmentTitle( $fragment );
+ $fragmentTitle = $title->createFragmentTarget( $fragment );
$this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
$this->assertEquals( $title->getText(), $fragmentTitle->getText() );