From: Kunal Mehta Date: Wed, 20 Apr 2016 08:09:23 +0000 (-0700) Subject: Add LinkTarget::createFragmentTarget() function X-Git-Tag: 1.31.0-rc.0~7159^2~4 X-Git-Url: http://git.cyclocoop.org/clavettes/images/siteon3.jpg?a=commitdiff_plain;h=59d838caeacbadf9cb2c359eec9fde7e712bc2a9;p=lhc%2Fweb%2Fwiklou.git Add LinkTarget::createFragmentTarget() function The createFragmentTarget function allows for switching the fragment on a target in an easier way. TitleValue already had a now-renamed createFragmentTitle function (no uses outside of tests), and an implementation was added for Title. This will also help with reducing the amount of public usage of Title::setFragment(), which is deprecated. Change-Id: I1e8ba2f85e748b1b4394fb2f2a1ccce69cf6e3c5 --- diff --git a/includes/LinkTarget.php b/includes/LinkTarget.php index 8246f7dfa4..b7132a8bdc 100644 --- a/includes/LinkTarget.php +++ b/includes/LinkTarget.php @@ -44,4 +44,14 @@ interface LinkTarget { */ 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 ); } diff --git a/includes/Title.php b/includes/Title.php index 3fd4631af8..7368bb09d2 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -1376,7 +1376,8 @@ class Title implements LinkTarget { * 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 @@ -1386,6 +1387,23 @@ class Title implements LinkTarget { $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 diff --git a/includes/title/TitleValue.php b/includes/title/TitleValue.php index 18a06ea01d..a0a7b1defd 100644 --- a/includes/title/TitleValue.php +++ b/includes/title/TitleValue.php @@ -131,11 +131,12 @@ class TitleValue implements LinkTarget { /** * 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 ); } diff --git a/tests/phpunit/includes/TitleTest.php b/tests/phpunit/includes/TitleTest.php index b3465e1a85..7d025d288f 100644 --- a/tests/phpunit/includes/TitleTest.php +++ b/tests/phpunit/includes/TitleTest.php @@ -665,4 +665,41 @@ class TitleTest extends MediaWikiTestCase { '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() ); + } } diff --git a/tests/phpunit/includes/title/TitleValueTest.php b/tests/phpunit/includes/title/TitleValueTest.php index 013bbc16fc..913253bc1b 100644 --- a/tests/phpunit/includes/title/TitleValueTest.php +++ b/tests/phpunit/includes/title/TitleValueTest.php @@ -85,7 +85,7 @@ class TitleValueTest extends MediaWikiTestCase { * @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() );