From 7688e781b19e963bc1803d1ea226f4a17fa7247c Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Wed, 27 Apr 2016 16:18:15 -0700 Subject: [PATCH] Add TitleFormatter::getPrefixedDBkey() and implementation Matches the expected result of Title::getPrefixedDBkey(), and will be used in LinkCache. Change-Id: I5ca65d07e2ae28778d060208e2bfe3fc0529992a --- includes/title/MediaWikiTitleCodec.php | 25 ++++++++++++++++ includes/title/TitleFormatter.php | 12 ++++++++ .../title/MediaWikiTitleCodecTest.php | 30 +++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/includes/title/MediaWikiTitleCodec.php b/includes/title/MediaWikiTitleCodec.php index 0e291ede14..5c504f3513 100644 --- a/includes/title/MediaWikiTitleCodec.php +++ b/includes/title/MediaWikiTitleCodec.php @@ -181,6 +181,31 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { ); } + /** + * @since 1.27 + * @see TitleFormatter::getPrefixedDBkey() + * @param LinkTarget $target + * @return string + */ + public function getPrefixedDBkey( LinkTarget $target ) { + $key = ''; + if ( $target->isExternal() ) { + $key .= $target->getInterwiki() . ':'; + } + $nsName = $this->getNamespaceName( + $target->getNamespace(), + $target->getText() + ); + + if ( $nsName !== '' ) { + $key .= $nsName . ':'; + } + + $key .= $target->getText(); + + return strtr( $key, ' ', '_' ); + } + /** * @see TitleFormatter::getText() * diff --git a/includes/title/TitleFormatter.php b/includes/title/TitleFormatter.php index c081129e27..5177606fcf 100644 --- a/includes/title/TitleFormatter.php +++ b/includes/title/TitleFormatter.php @@ -68,6 +68,18 @@ interface TitleFormatter { */ public function getPrefixedText( LinkTarget $title ); + /** + * Return the title in prefixed database key form, with interwiki + * and namespace. + * + * @since 1.27 + * + * @param LinkTarget $target + * + * @return string + */ + public function getPrefixedDBkey( LinkTarget $target ); + /** * Returns the title formatted for display, with namespace and fragment. * diff --git a/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php b/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php index e321bdb252..40065f559e 100644 --- a/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php +++ b/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php @@ -179,6 +179,36 @@ class MediaWikiTitleCodecTest extends MediaWikiTestCase { $this->assertEquals( $expected, $actual ); } + public static function provideGetPrefixedDBkey() { + return [ + [ NS_MAIN, 'Foo_Bar', '', '', 'en', 'Foo_Bar' ], + [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi_Maier' ], + + // No capitalization or normalization is applied while formatting! + [ NS_USER_TALK, 'hansi__maier', '', '', 'en', 'User_talk:hansi__maier' ], + + // getGenderCache() provides a mock that considers first + // names ending in "a" to be female. + [ NS_USER, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa_Müller' ], + + [ NS_MAIN, 'Remote_page', '', 'remotetestiw', 'en', 'remotetestiw:Remote_page' ] + ]; + } + + /** + * @dataProvider provideGetPrefixedDBkey + */ + public function testGetPrefixedDBkey( $namespace, $dbkey, $fragment, + $interwiki, $lang, $expected + ) { + $codec = $this->makeCodec( $lang ); + $title = new TitleValue( $namespace, $dbkey, $fragment, $interwiki ); + + $actual = $codec->getPrefixedDBkey( $title ); + + $this->assertEquals( $expected, $actual ); + } + public static function provideGetFullText() { return [ [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ], -- 2.20.1