linker: Move Linker::normaliseSpecialPage to LinkRenderer class

Would have love to just kill Linker::normaliseSpecialPage() but
the deprecation policy has to be followed and we'll drop the above
method in 1.36.

For now, we'll just soft and hard deprecate it, also, callers have
been updated.

Dependency inject SpecialPageFactory to LinkRenderer service and
note that ->normalizeTarget() is only for internal use by Linker
& DummyLinker via their `->normaliseSpecialPage()` methods.

Also, updated unit tests to capture injecting the special page
factory class.

Change-Id: I951403c89ff497fd1f7441ad0304dd5bc9442ad7
This commit is contained in:
Derick A 2020-05-22 19:33:06 +01:00 committed by Krinkle
parent 7c9e19ed5e
commit 707567bd73
8 changed files with 76 additions and 27 deletions

View file

@ -1051,6 +1051,8 @@ because of Phabricator reports.
GuzzleHttpRequest now require the timeout and connectTimeout options to GuzzleHttpRequest now require the timeout and connectTimeout options to
always be specified, otherwise a deprecation warning will be raised. Most always be specified, otherwise a deprecation warning will be raised. Most
callers should use HttpRequestFactory which always sets these options. callers should use HttpRequestFactory which always sets these options.
* Linker::normaliseSpecialPage() has been deprecated, instead make use of
LinkRenderer::normalizeTarget().
* … * …
=== Other changes in 1.35 === === Other changes in 1.35 ===

View file

@ -1,5 +1,7 @@
<?php <?php
use MediaWiki\MediaWikiServices;
/** /**
* @since 1.18 * @since 1.18
*/ */
@ -66,7 +68,8 @@ class DummyLinker {
} }
public function normaliseSpecialPage( Title $title ) { public function normaliseSpecialPage( Title $title ) {
return Linker::normaliseSpecialPage( $title ); $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
return $linkRenderer->normalizeTarget( $title );
} }
public function makeExternalImage( $url, $alt = '' ) { public function makeExternalImage( $url, $alt = '' ) {

View file

@ -202,19 +202,14 @@ class Linker {
/** /**
* @since 1.16.3 * @since 1.16.3
* @deprecated since 1.35, use LinkRenderer::normalizeTarget()
* @param LinkTarget $target * @param LinkTarget $target
* @return LinkTarget * @return LinkTarget
*/ */
public static function normaliseSpecialPage( LinkTarget $target ) { public static function normaliseSpecialPage( LinkTarget $target ) {
if ( $target->getNamespace() == NS_SPECIAL && !$target->isExternal() ) { wfDeprecated( __METHOD__, '1.35' );
list( $name, $subpage ) = MediaWikiServices::getInstance()->getSpecialPageFactory()-> $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
resolveAlias( $target->getDBkey() ); return $linkRenderer->normalizeTarget( $target );
if ( $name ) {
return SpecialPage::getTitleValueFor( $name, $subpage, $target->getFragment() );
}
}
return $target;
} }
/** /**
@ -467,8 +462,9 @@ class Linker {
} }
} }
} elseif ( isset( $frameParams['link-title'] ) && $frameParams['link-title'] !== '' ) { } elseif ( isset( $frameParams['link-title'] ) && $frameParams['link-title'] !== '' ) {
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
$mtoParams['custom-title-link'] = Title::newFromLinkTarget( $mtoParams['custom-title-link'] = Title::newFromLinkTarget(
self::normaliseSpecialPage( $frameParams['link-title'] ) $linkRenderer->normalizeTarget( $frameParams['link-title'] )
); );
} elseif ( !empty( $frameParams['no-link'] ) ) { } elseif ( !empty( $frameParams['no-link'] ) ) {
// No link // No link

View file

@ -489,7 +489,8 @@ return [
return new LinkRendererFactory( return new LinkRendererFactory(
$services->getTitleFormatter(), $services->getTitleFormatter(),
$services->getLinkCache(), $services->getLinkCache(),
$services->getNamespaceInfo() $services->getNamespaceInfo(),
$services->getSpecialPageFactory()
); );
}, },

View file

@ -25,10 +25,11 @@ use Hooks;
use Html; use Html;
use HtmlArmor; use HtmlArmor;
use LinkCache; use LinkCache;
use Linker;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use MediaWiki\SpecialPage\SpecialPageFactory;
use NamespaceInfo; use NamespaceInfo;
use Sanitizer; use Sanitizer;
use SpecialPage;
use Title; use Title;
use TitleFormatter; use TitleFormatter;
@ -82,16 +83,27 @@ class LinkRenderer {
private $runLegacyBeginHook = true; private $runLegacyBeginHook = true;
/** /**
* @var SpecialPageFactory
*/
private $specialPageFactory;
/**
* @internal For use by LinkRendererFactory
* @param TitleFormatter $titleFormatter * @param TitleFormatter $titleFormatter
* @param LinkCache $linkCache * @param LinkCache $linkCache
* @param NamespaceInfo $nsInfo * @param NamespaceInfo $nsInfo
* @param SpecialPageFactory $specialPageFactory
*/ */
public function __construct( public function __construct(
TitleFormatter $titleFormatter, LinkCache $linkCache, NamespaceInfo $nsInfo TitleFormatter $titleFormatter,
LinkCache $linkCache,
NamespaceInfo $nsInfo,
SpecialPageFactory $specialPageFactory
) { ) {
$this->titleFormatter = $titleFormatter; $this->titleFormatter = $titleFormatter;
$this->linkCache = $linkCache; $this->linkCache = $linkCache;
$this->nsInfo = $nsInfo; $this->nsInfo = $nsInfo;
$this->specialPageFactory = $specialPageFactory;
} }
/** /**
@ -427,12 +439,22 @@ class LinkRenderer {
/** /**
* Normalizes the provided target * Normalizes the provided target
* *
* @todo move the code from Linker actually here * @internal For use by deprecated Linker & DummyLinker
* ::normaliseSpecialPage() methods
* @param LinkTarget $target * @param LinkTarget $target
* @return LinkTarget * @return LinkTarget
*/ */
private function normalizeTarget( LinkTarget $target ) { public function normalizeTarget( LinkTarget $target ) {
return Linker::normaliseSpecialPage( $target ); if ( $target->getNamespace() == NS_SPECIAL && !$target->isExternal() ) {
list( $name, $subpage ) = $this->specialPageFactory->resolveAlias(
$target->getDBkey()
);
if ( $name ) {
return SpecialPage::getTitleValueFor( $name, $subpage, $target->getFragment() );
}
}
return $target;
} }
/** /**

View file

@ -21,6 +21,7 @@
namespace MediaWiki\Linker; namespace MediaWiki\Linker;
use LinkCache; use LinkCache;
use MediaWiki\SpecialPage\SpecialPageFactory;
use NamespaceInfo; use NamespaceInfo;
use TitleFormatter; use TitleFormatter;
use User; use User;
@ -47,23 +48,36 @@ class LinkRendererFactory {
private $nsInfo; private $nsInfo;
/** /**
* @var SpecialPageFactory
*/
private $specialPageFactory;
/**
* @internal For use by core ServiceWiring
* @param TitleFormatter $titleFormatter * @param TitleFormatter $titleFormatter
* @param LinkCache $linkCache * @param LinkCache $linkCache
* @param NamespaceInfo $nsInfo * @param NamespaceInfo $nsInfo
* @param SpecialPageFactory $specialPageFactory
*/ */
public function __construct( public function __construct(
TitleFormatter $titleFormatter, LinkCache $linkCache, NamespaceInfo $nsInfo TitleFormatter $titleFormatter,
LinkCache $linkCache,
NamespaceInfo $nsInfo,
SpecialPageFactory $specialPageFactory
) { ) {
$this->titleFormatter = $titleFormatter; $this->titleFormatter = $titleFormatter;
$this->linkCache = $linkCache; $this->linkCache = $linkCache;
$this->nsInfo = $nsInfo; $this->nsInfo = $nsInfo;
$this->specialPageFactory = $specialPageFactory;
} }
/** /**
* @return LinkRenderer * @return LinkRenderer
*/ */
public function create() { public function create() {
return new LinkRenderer( $this->titleFormatter, $this->linkCache, $this->nsInfo ); return new LinkRenderer(
$this->titleFormatter, $this->linkCache, $this->nsInfo, $this->specialPageFactory
);
} }
/** /**

View file

@ -3,6 +3,7 @@
use MediaWiki\Linker\LinkRenderer; use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Linker\LinkRendererFactory; use MediaWiki\Linker\LinkRendererFactory;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use MediaWiki\SpecialPage\SpecialPageFactory;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
/** /**
@ -25,6 +26,11 @@ class LinkRendererFactoryTest extends MediaWikiLangTestCase {
*/ */
private $nsInfo; private $nsInfo;
/**
* @var SpecialPageFactory
*/
private $specialPageFactory;
public function setUp() : void { public function setUp() : void {
parent::setUp(); parent::setUp();
@ -32,6 +38,7 @@ class LinkRendererFactoryTest extends MediaWikiLangTestCase {
$this->titleFormatter = $services->getTitleFormatter(); $this->titleFormatter = $services->getTitleFormatter();
$this->linkCache = $services->getLinkCache(); $this->linkCache = $services->getLinkCache();
$this->nsInfo = $services->getNamespaceInfo(); $this->nsInfo = $services->getNamespaceInfo();
$this->specialPageFactory = $services->getSpecialPageFactory();
} }
public static function provideCreateFromLegacyOptions() { public static function provideCreateFromLegacyOptions() {
@ -63,8 +70,9 @@ class LinkRendererFactoryTest extends MediaWikiLangTestCase {
* @dataProvider provideCreateFromLegacyOptions * @dataProvider provideCreateFromLegacyOptions
*/ */
public function testCreateFromLegacyOptions( $options, $func, $val ) { public function testCreateFromLegacyOptions( $options, $func, $val ) {
$factory = $factory = new LinkRendererFactory(
new LinkRendererFactory( $this->titleFormatter, $this->linkCache, $this->nsInfo ); $this->titleFormatter, $this->linkCache, $this->nsInfo, $this->specialPageFactory
);
$linkRenderer = $factory->createFromLegacyOptions( $linkRenderer = $factory->createFromLegacyOptions(
$options $options
); );
@ -73,20 +81,22 @@ class LinkRendererFactoryTest extends MediaWikiLangTestCase {
} }
public function testCreate() { public function testCreate() {
$factory = $factory = new LinkRendererFactory(
new LinkRendererFactory( $this->titleFormatter, $this->linkCache, $this->nsInfo ); $this->titleFormatter, $this->linkCache, $this->nsInfo, $this->specialPageFactory
);
$this->assertInstanceOf( LinkRenderer::class, $factory->create() ); $this->assertInstanceOf( LinkRenderer::class, $factory->create() );
} }
public function testCreateForUser() { public function testCreateForUser() {
/** @var MockObject|User $user */ /** @var MockObject|User $user */
$user = $this->getMockBuilder( User::class ) $user = $this->getMockBuilder( User::class )
->setMethods( [ 'getStubThreshold' ] )->getMock(); ->onlyMethods( [ 'getStubThreshold' ] )->getMock();
$user->expects( $this->once() ) $user->expects( $this->once() )
->method( 'getStubThreshold' ) ->method( 'getStubThreshold' )
->willReturn( 15 ); ->willReturn( 15 );
$factory = $factory = new LinkRendererFactory(
new LinkRendererFactory( $this->titleFormatter, $this->linkCache, $this->nsInfo ); $this->titleFormatter, $this->linkCache, $this->nsInfo, $this->specialPageFactory
);
$linkRenderer = $factory->createForUser( $user ); $linkRenderer = $factory->createForUser( $user );
$this->assertInstanceOf( LinkRenderer::class, $linkRenderer ); $this->assertInstanceOf( LinkRenderer::class, $linkRenderer );
$this->assertEquals( 15, $linkRenderer->getStubThreshold() ); $this->assertEquals( 15, $linkRenderer->getStubThreshold() );

View file

@ -142,6 +142,7 @@ class LinkRendererTest extends MediaWikiLangTestCase {
$wanCache = $services->getMainWANObjectCache(); $wanCache = $services->getMainWANObjectCache();
$titleFormatter = $services->getTitleFormatter(); $titleFormatter = $services->getTitleFormatter();
$nsInfo = $services->getNamespaceInfo(); $nsInfo = $services->getNamespaceInfo();
$specialPageFactory = $services->getSpecialPageFactory();
$linkCache = new LinkCache( $titleFormatter, $wanCache, $nsInfo ); $linkCache = new LinkCache( $titleFormatter, $wanCache, $nsInfo );
$foobarTitle = new TitleValue( NS_MAIN, 'FooBar' ); $foobarTitle = new TitleValue( NS_MAIN, 'FooBar' );
$redirectTitle = new TitleValue( NS_MAIN, 'Redirect' ); $redirectTitle = new TitleValue( NS_MAIN, 'Redirect' );
@ -166,7 +167,7 @@ class LinkRendererTest extends MediaWikiLangTestCase {
0 // redir 0 // redir
); );
$linkRenderer = new LinkRenderer( $titleFormatter, $linkCache, $nsInfo ); $linkRenderer = new LinkRenderer( $titleFormatter, $linkCache, $nsInfo, $specialPageFactory );
$linkRenderer->setStubThreshold( 0 ); $linkRenderer->setStubThreshold( 0 );
$this->assertSame( $this->assertSame(
'', '',