From 82311f8c2c79bc469cae14e17546fd79d3541b76 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Sun, 5 Aug 2018 15:44:11 +0300 Subject: [PATCH] Update LinkRenderer to use NamespaceInfo Change-Id: I4af843238ffd58925f57c0f7b98253a90cb285ec --- includes/ServiceWiring.php | 3 ++- includes/linker/LinkRenderer.php | 18 ++++++++++++---- includes/linker/LinkRendererFactory.php | 14 +++++++++++-- .../linker/LinkRendererFactoryTest.php | 21 ++++++++++++++----- .../includes/linker/LinkRendererTest.php | 2 +- 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index f74ba793e9..a30534e865 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -249,7 +249,8 @@ return [ 'LinkRendererFactory' => function ( MediaWikiServices $services ) : LinkRendererFactory { return new LinkRendererFactory( $services->getTitleFormatter(), - $services->getLinkCache() + $services->getLinkCache(), + $services->getNamespaceInfo() ); }, diff --git a/includes/linker/LinkRenderer.php b/includes/linker/LinkRenderer.php index c77b156f0b..707c38210c 100644 --- a/includes/linker/LinkRenderer.php +++ b/includes/linker/LinkRenderer.php @@ -27,7 +27,7 @@ use HtmlArmor; use LinkCache; use Linker; use MediaWiki\MediaWikiServices; -use MWNamespace; +use NamespaceInfo; use Sanitizer; use Title; use TitleFormatter; @@ -69,6 +69,11 @@ class LinkRenderer { */ private $linkCache; + /** + * @var NamespaceInfo + */ + private $nsInfo; + /** * Whether to run the legacy Linker hooks * @@ -79,10 +84,14 @@ class LinkRenderer { /** * @param TitleFormatter $titleFormatter * @param LinkCache $linkCache + * @param NamespaceInfo $nsInfo */ - public function __construct( TitleFormatter $titleFormatter, LinkCache $linkCache ) { + public function __construct( + TitleFormatter $titleFormatter, LinkCache $linkCache, NamespaceInfo $nsInfo + ) { $this->titleFormatter = $titleFormatter; $this->linkCache = $linkCache; + $this->nsInfo = $nsInfo; } /** @@ -468,8 +477,9 @@ class LinkRenderer { if ( $this->linkCache->getGoodLinkFieldObj( $target, 'redirect' ) ) { # Page is a redirect return 'mw-redirect'; - } elseif ( $this->stubThreshold > 0 && MWNamespace::isContent( $target->getNamespace() ) - && $this->linkCache->getGoodLinkFieldObj( $target, 'length' ) < $this->stubThreshold + } elseif ( + $this->stubThreshold > 0 && $this->nsInfo->isContent( $target->getNamespace() ) && + $this->linkCache->getGoodLinkFieldObj( $target, 'length' ) < $this->stubThreshold ) { # Page is a stub return 'stub'; diff --git a/includes/linker/LinkRendererFactory.php b/includes/linker/LinkRendererFactory.php index 240ea09be9..eeb28b5560 100644 --- a/includes/linker/LinkRendererFactory.php +++ b/includes/linker/LinkRendererFactory.php @@ -21,6 +21,7 @@ namespace MediaWiki\Linker; use LinkCache; +use NamespaceInfo; use TitleFormatter; use User; @@ -40,20 +41,29 @@ class LinkRendererFactory { */ private $linkCache; + /** + * @var NamespaceInfo + */ + private $nsInfo; + /** * @param TitleFormatter $titleFormatter * @param LinkCache $linkCache + * @param NamespaceInfo $nsInfo */ - public function __construct( TitleFormatter $titleFormatter, LinkCache $linkCache ) { + public function __construct( + TitleFormatter $titleFormatter, LinkCache $linkCache, NamespaceInfo $nsInfo + ) { $this->titleFormatter = $titleFormatter; $this->linkCache = $linkCache; + $this->nsInfo = $nsInfo; } /** * @return LinkRenderer */ public function create() { - return new LinkRenderer( $this->titleFormatter, $this->linkCache ); + return new LinkRenderer( $this->titleFormatter, $this->linkCache, $this->nsInfo ); } /** diff --git a/tests/phpunit/includes/linker/LinkRendererFactoryTest.php b/tests/phpunit/includes/linker/LinkRendererFactoryTest.php index 27e5a657ed..83ce3d2ffb 100644 --- a/tests/phpunit/includes/linker/LinkRendererFactoryTest.php +++ b/tests/phpunit/includes/linker/LinkRendererFactoryTest.php @@ -19,10 +19,18 @@ class LinkRendererFactoryTest extends MediaWikiLangTestCase { */ private $linkCache; + /** + * @var NamespaceInfo + */ + private $nsInfo; + public function setUp() { parent::setUp(); - $this->titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter(); - $this->linkCache = MediaWikiServices::getInstance()->getLinkCache(); + + $services = MediaWikiServices::getInstance(); + $this->titleFormatter = $services->getTitleFormatter(); + $this->linkCache = $services->getLinkCache(); + $this->nsInfo = $services->getNamespaceInfo(); } public static function provideCreateFromLegacyOptions() { @@ -54,7 +62,8 @@ class LinkRendererFactoryTest extends MediaWikiLangTestCase { * @dataProvider provideCreateFromLegacyOptions */ public function testCreateFromLegacyOptions( $options, $func, $val ) { - $factory = new LinkRendererFactory( $this->titleFormatter, $this->linkCache ); + $factory = + new LinkRendererFactory( $this->titleFormatter, $this->linkCache, $this->nsInfo ); $linkRenderer = $factory->createFromLegacyOptions( $options ); @@ -63,7 +72,8 @@ class LinkRendererFactoryTest extends MediaWikiLangTestCase { } public function testCreate() { - $factory = new LinkRendererFactory( $this->titleFormatter, $this->linkCache ); + $factory = + new LinkRendererFactory( $this->titleFormatter, $this->linkCache, $this->nsInfo ); $this->assertInstanceOf( LinkRenderer::class, $factory->create() ); } @@ -74,7 +84,8 @@ class LinkRendererFactoryTest extends MediaWikiLangTestCase { $user->expects( $this->once() ) ->method( 'getStubThreshold' ) ->willReturn( 15 ); - $factory = new LinkRendererFactory( $this->titleFormatter, $this->linkCache ); + $factory = + new LinkRendererFactory( $this->titleFormatter, $this->linkCache, $this->nsInfo ); $linkRenderer = $factory->createForUser( $user ); $this->assertInstanceOf( LinkRenderer::class, $linkRenderer ); $this->assertEquals( 15, $linkRenderer->getStubThreshold() ); diff --git a/tests/phpunit/includes/linker/LinkRendererTest.php b/tests/phpunit/includes/linker/LinkRendererTest.php index e90577c907..d4e1961efe 100644 --- a/tests/phpunit/includes/linker/LinkRendererTest.php +++ b/tests/phpunit/includes/linker/LinkRendererTest.php @@ -165,7 +165,7 @@ class LinkRendererTest extends MediaWikiLangTestCase { 0 // redir ); - $linkRenderer = new LinkRenderer( $titleFormatter, $linkCache ); + $linkRenderer = new LinkRenderer( $titleFormatter, $linkCache, $nsInfo ); $linkRenderer->setStubThreshold( 0 ); $this->assertEquals( '', -- 2.20.1