diff --git a/includes/MediaWikiServices.php b/includes/MediaWikiServices.php index 5bb5597b787..891f426d742 100644 --- a/includes/MediaWikiServices.php +++ b/includes/MediaWikiServices.php @@ -8,6 +8,7 @@ use GenderCache; use GlobalVarConfig; use Hooks; use LBFactory; +use LinkCache; use Liuggio\StatsdClient\Factory\StatsdDataFactory; use LoadBalancer; use MediaWiki\Services\ServiceContainer; @@ -460,6 +461,15 @@ class MediaWikiServices extends ServiceContainer { public function getGenderCache() { return $this->getService( 'GenderCache' ); } + + /** + * @since 1.28 + * @return LinkCache + */ + public function getLinkCache() { + return $this->getService( 'LinkCache' ); + } + /** * @since 1.28 * @return TitleFormatter diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index aa99a713150..293e6eb176b 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -139,6 +139,12 @@ return [ return $store; }, + 'LinkCache' => function( MediaWikiServices $services ) { + return new LinkCache( + $services->getTitleFormatter() + ); + }, + 'GenderCache' => function( MediaWikiServices $services ) { return new GenderCache(); }, diff --git a/includes/cache/LinkCache.php b/includes/cache/LinkCache.php index bb2242b225a..1dfefdfdac8 100644 --- a/includes/cache/LinkCache.php +++ b/includes/cache/LinkCache.php @@ -65,39 +65,10 @@ class LinkCache { * Get an instance of this class. * * @return LinkCache + * @deprecated since 1.28, use MediaWikiServices instead */ public static function singleton() { - if ( !self::$instance ) { - self::$instance = new LinkCache( - MediaWikiServices::getInstance()->getTitleFormatter() - ); - } - - return self::$instance; - } - - /** - * Destroy the singleton instance - * - * A new one will be created next time singleton() is called. - * - * @since 1.22 - */ - public static function destroySingleton() { - self::$instance = null; - } - - /** - * Set the singleton instance to a given object. - * - * Since we do not have an interface for LinkCache, you have to be sure the - * given object implements all the LinkCache public methods. - * - * @param LinkCache $instance - * @since 1.22 - */ - public static function setSingleton( LinkCache $instance ) { - self::$instance = $instance; + return MediaWikiServices::getInstance()->getLinkCache(); } /** diff --git a/tests/phpunit/includes/MediaWikiServicesTest.php b/tests/phpunit/includes/MediaWikiServicesTest.php index 467a2adff6c..51ef9d7ae15 100644 --- a/tests/phpunit/includes/MediaWikiServicesTest.php +++ b/tests/phpunit/includes/MediaWikiServicesTest.php @@ -243,6 +243,7 @@ class MediaWikiServicesTest extends PHPUnit_Framework_TestCase { 'DBLoadBalancer' => [ 'DBLoadBalancer', 'LoadBalancer' ], 'WatchedItemStore' => [ 'WatchedItemStore', WatchedItemStore::class ], 'GenderCache' => [ 'GenderCache', GenderCache::class ], + 'LinkCache' => [ 'LinkCache', LinkCache::class ], '_MediaWikiTitleCodec' => [ '_MediaWikiTitleCodec', MediaWikiTitleCodec::class ], 'TitleFormatter' => [ 'TitleFormatter', TitleFormatter::class ], 'TitleParser' => [ 'TitleParser', TitleParser::class ], diff --git a/tests/phpunit/includes/content/ContentHandlerTest.php b/tests/phpunit/includes/content/ContentHandlerTest.php index 91f27fbe71c..545b964fcb7 100644 --- a/tests/phpunit/includes/content/ContentHandlerTest.php +++ b/tests/phpunit/includes/content/ContentHandlerTest.php @@ -1,4 +1,5 @@ resetNamespaces(); // And LinkCache - LinkCache::destroySingleton(); + MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' ); } protected function tearDown() { @@ -46,7 +47,7 @@ class ContentHandlerTest extends MediaWikiTestCase { MWNamespace::getCanonicalNamespaces( true ); $wgContLang->resetNamespaces(); // And LinkCache - LinkCache::destroySingleton(); + MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' ); parent::tearDown(); }