Add LinkTarget::createFragmentTarget() function
authorKunal Mehta <legoktm@member.fsf.org>
Wed, 20 Apr 2016 08:09:23 +0000 (01:09 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Fri, 22 Apr 2016 00:19:04 +0000 (17:19 -0700)
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

includes/LinkTarget.php
includes/Title.php
includes/title/TitleValue.php
tests/phpunit/includes/TitleTest.php
tests/phpunit/includes/title/TitleValueTest.php

index 8246f7d..b7132a8 100644 (file)
@@ -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 );
 }
index 3fd4631..7368bb0 100644 (file)
@@ -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
index 18a06ea..a0a7b1d 100644 (file)
@@ -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 );
        }
 
index b3465e1..7d025d2 100644 (file)
@@ -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() );
+       }
 }
index 013bbc1..913253b 100644 (file)
@@ -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() );