From: Kunal Mehta Date: Mon, 25 Apr 2016 05:41:32 +0000 (-0700) Subject: Add TitleParser and TitleFormatter to MediaWikiServices X-Git-Tag: 1.31.0-rc.0~7019^2 X-Git-Url: http://git.cyclocoop.org/?a=commitdiff_plain;h=f1e8d27a914be360281c5960a85abc1eaca9636a;p=lhc%2Fweb%2Fwiklou.git Add TitleParser and TitleFormatter to MediaWikiServices Depends-On: Ibfd0a7f98f50506cd8402f966682f320bf715c8a Change-Id: I81d48616afd1ab2bde1a5f1d12f4aefb1c866d43 --- diff --git a/includes/MediaWikiServices.php b/includes/MediaWikiServices.php index 9e18fd14d1..5bb5597b78 100644 --- a/includes/MediaWikiServices.php +++ b/includes/MediaWikiServices.php @@ -20,6 +20,8 @@ use SiteLookup; use SiteStore; use WatchedItemStore; use SkinFactory; +use TitleFormatter; +use TitleParser; /** * Service locator for MediaWiki core services. @@ -458,6 +460,21 @@ class MediaWikiServices extends ServiceContainer { public function getGenderCache() { return $this->getService( 'GenderCache' ); } + /** + * @since 1.28 + * @return TitleFormatter + */ + public function getTitleFormatter() { + return $this->getService( 'TitleFormatter' ); + } + + /** + * @since 1.28 + * @return TitleParser + */ + public function getTitleParser() { + return $this->getService( 'TitleParser' ); + } /////////////////////////////////////////////////////////////////////////// // NOTE: When adding a service getter here, don't forget to add a test diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index e282bdaa44..aa99a71315 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -143,6 +143,24 @@ return [ return new GenderCache(); }, + '_MediaWikiTitleCodec' => function( MediaWikiServices $services ) { + global $wgContLang; + + return new MediaWikiTitleCodec( + $wgContLang, + $services->getGenderCache(), + $services->getMainConfig()->get( 'LocalInterwikis' ) + ); + }, + + 'TitleFormatter' => function( MediaWikiServices $services ) { + return $services->getService( '_MediaWikiTitleCodec' ); + }, + + 'TitleParser' => function( MediaWikiServices $services ) { + return $services->getService( '_MediaWikiTitleCodec' ); + }, + /////////////////////////////////////////////////////////////////////////// // NOTE: When adding a service here, don't forget to add a getter function // in the MediaWikiServices class. The convenience getter should just call diff --git a/includes/Title.php b/includes/Title.php index 65b2d3a2f2..d070609aa3 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -22,7 +22,6 @@ * @file */ use MediaWiki\Linker\LinkTarget; - use MediaWiki\MediaWikiServices; /** @@ -159,42 +158,6 @@ class Title implements LinkTarget { private $mIsBigDeletion = null; // @} - /** - * B/C kludge: provide a TitleParser for use by Title. - * Ideally, Title would have no methods that need this. - * Avoid usage of this singleton by using TitleValue - * and the associated services when possible. - * - * @return MediaWikiTitleCodec - */ - private static function getMediaWikiTitleCodec() { - global $wgContLang, $wgLocalInterwikis; - - static $titleCodec = null; - static $titleCodecFingerprint = null; - - // $wgContLang and $wgLocalInterwikis may change (especially while testing), - // make sure we are using the right one. To detect changes over the course - // of a request, we remember a fingerprint of the config used to create the - // codec singleton, and re-create it if the fingerprint doesn't match. - $fingerprint = spl_object_hash( $wgContLang ) . '|' . implode( '+', $wgLocalInterwikis ); - - if ( $fingerprint !== $titleCodecFingerprint ) { - $titleCodec = null; - } - - if ( !$titleCodec ) { - $titleCodec = new MediaWikiTitleCodec( - $wgContLang, - GenderCache::singleton(), - $wgLocalInterwikis - ); - $titleCodecFingerprint = $fingerprint; - } - - return $titleCodec; - } - /** * B/C kludge: provide a TitleParser for use by Title. * Ideally, Title would have no methods that need this. @@ -204,9 +167,7 @@ class Title implements LinkTarget { * @return TitleFormatter */ private static function getTitleFormatter() { - // NOTE: we know that getMediaWikiTitleCodec() returns a MediaWikiTitleCodec, - // which implements TitleFormatter. - return self::getMediaWikiTitleCodec(); + return MediaWikiServices::getInstance()->getTitleFormatter(); } function __construct() { @@ -3334,9 +3295,11 @@ class Title implements LinkTarget { // @note: splitTitleString() is a temporary hack to allow MediaWikiTitleCodec to share // the parsing code with Title, while avoiding massive refactoring. // @todo: get rid of secureAndSplit, refactor parsing code. - $titleParser = self::getMediaWikiTitleCodec(); + // @note: getTitleParser() returns a TitleParser implementation which does not have a + // splitTitleString method, but the only implementation (MediaWikiTitleCodec) does + $titleCodec = MediaWikiServices::getInstance()->getTitleParser(); // MalformedTitleException can be thrown here - $parts = $titleParser->splitTitleString( $dbkey, $this->getDefaultNamespace() ); + $parts = $titleCodec->splitTitleString( $dbkey, $this->getDefaultNamespace() ); # Fill fields $this->setFragment( '#' . $parts['fragment'] ); diff --git a/tests/parser/parserTest.inc b/tests/parser/parserTest.inc index 78e5f6f9c4..ccb86d0efe 100644 --- a/tests/parser/parserTest.inc +++ b/tests/parser/parserTest.inc @@ -27,6 +27,7 @@ * @file * @ingroup Testing */ +use MediaWiki\MediaWikiServices; /** * @ingroup Testing @@ -331,6 +332,18 @@ class ParserTest { Hooks::clear( 'InterwikiLoadPrefix' ); } + /** + * Reset the Title-related services that need resetting + * for each test + */ + public static function resetTitleServices() { + $services = MediaWikiServices::getInstance(); + $services->resetServiceForTesting( 'TitleFormatter' ); + $services->resetServiceForTesting( 'TitleParser' ); + $services->resetServiceForTesting( '_MediaWikiTitleCodec' ); + + } + public function setupRecorder( $options ) { if ( isset( $options['record'] ) ) { $this->recorder = new DbTestRecorder( $this ); @@ -958,6 +971,8 @@ class ParserTest { MWTidy::destroySingleton(); RepoGroup::destroySingleton(); + self::resetTitleServices(); + return $context; } diff --git a/tests/phpunit/includes/MediaWikiServicesTest.php b/tests/phpunit/includes/MediaWikiServicesTest.php index 4f917a5f5f..467a2adff6 100644 --- a/tests/phpunit/includes/MediaWikiServicesTest.php +++ b/tests/phpunit/includes/MediaWikiServicesTest.php @@ -200,6 +200,10 @@ class MediaWikiServicesTest extends PHPUnit_Framework_TestCase { // All getters should be named just like the service, with "get" added. foreach ( $getServiceCases as $name => $case ) { + if ( $name[0] === '_' ) { + // Internal service, no getter + continue; + } list( $service, $class ) = $case; $getterCases[$name] = [ 'get' . $service, @@ -239,6 +243,9 @@ class MediaWikiServicesTest extends PHPUnit_Framework_TestCase { 'DBLoadBalancer' => [ 'DBLoadBalancer', 'LoadBalancer' ], 'WatchedItemStore' => [ 'WatchedItemStore', WatchedItemStore::class ], 'GenderCache' => [ 'GenderCache', GenderCache::class ], + '_MediaWikiTitleCodec' => [ '_MediaWikiTitleCodec', MediaWikiTitleCodec::class ], + 'TitleFormatter' => [ 'TitleFormatter', TitleFormatter::class ], + 'TitleParser' => [ 'TitleParser', TitleParser::class ], ]; } diff --git a/tests/phpunit/includes/TitleTest.php b/tests/phpunit/includes/TitleTest.php index 7d025d288f..496f18f796 100644 --- a/tests/phpunit/includes/TitleTest.php +++ b/tests/phpunit/includes/TitleTest.php @@ -144,6 +144,13 @@ class TitleTest extends MediaWikiTestCase { ] ] ] ); + + // Reset TitleParser since we modified $wgLocalInterwikis + $this->setService( 'TitleParser', new MediaWikiTitleCodec( + Language::factory( 'en' ), + new GenderCache(), + [ 'localtestiw' ] + ) ); } /** diff --git a/tests/phpunit/includes/parser/NewParserTest.php b/tests/phpunit/includes/parser/NewParserTest.php index 22bb23784f..4c973e58d8 100644 --- a/tests/phpunit/includes/parser/NewParserTest.php +++ b/tests/phpunit/includes/parser/NewParserTest.php @@ -1,5 +1,4 @@ resetNamespaces(); # reset namespace cache + ParserTest::resetTitleServices(); } protected function tearDown() {