Add comment flag to LinkRenderer
This marks whether links are being rendered for comments or elsewhere. This information can be used in hooks; specifically, Wikibase wants to add labels to entity links only in comments and on special pages. Bug: T292203 Change-Id: I9164f760e8b46e05218fb36f430bc36ef1fdf30f
This commit is contained in:
parent
7c0ffb3b24
commit
37d90f3664
5 changed files with 54 additions and 6 deletions
|
|
@ -353,8 +353,9 @@ return [
|
|||
},
|
||||
|
||||
'CommentFormatter' => static function ( MediaWikiServices $services ): CommentFormatter {
|
||||
$linkRenderer = $services->getLinkRendererFactory()->create( [ 'renderForComment' => true ] );
|
||||
$parserFactory = new CommentParserFactory(
|
||||
$services->getLinkRenderer(),
|
||||
$linkRenderer,
|
||||
$services->getLinkBatchFactory(),
|
||||
$services->getLinkCache(),
|
||||
$services->getRepoGroup(),
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ namespace MediaWiki\Linker;
|
|||
use Html;
|
||||
use HtmlArmor;
|
||||
use LinkCache;
|
||||
use MediaWiki\Config\ServiceOptions;
|
||||
use MediaWiki\HookContainer\HookContainer;
|
||||
use MediaWiki\HookContainer\HookRunner;
|
||||
use MediaWiki\Page\PageReference;
|
||||
|
|
@ -41,6 +42,10 @@ use Wikimedia\Assert\Assert;
|
|||
*/
|
||||
class LinkRenderer {
|
||||
|
||||
public const CONSTRUCTOR_OPTIONS = [
|
||||
'renderForComment',
|
||||
];
|
||||
|
||||
/**
|
||||
* Whether to force the pretty article path
|
||||
*
|
||||
|
|
@ -55,6 +60,13 @@ class LinkRenderer {
|
|||
*/
|
||||
private $expandUrls = false;
|
||||
|
||||
/**
|
||||
* Whether links are being rendered for comments.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $comment = false;
|
||||
|
||||
/**
|
||||
* @var TitleFormatter
|
||||
*/
|
||||
|
|
@ -75,17 +87,23 @@ class LinkRenderer {
|
|||
|
||||
/**
|
||||
* @internal For use by LinkRendererFactory
|
||||
*
|
||||
* @param TitleFormatter $titleFormatter
|
||||
* @param LinkCache $linkCache
|
||||
* @param SpecialPageFactory $specialPageFactory
|
||||
* @param HookContainer $hookContainer
|
||||
* @param ServiceOptions $options
|
||||
*/
|
||||
public function __construct(
|
||||
TitleFormatter $titleFormatter,
|
||||
LinkCache $linkCache,
|
||||
SpecialPageFactory $specialPageFactory,
|
||||
HookContainer $hookContainer
|
||||
HookContainer $hookContainer,
|
||||
ServiceOptions $options
|
||||
) {
|
||||
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
||||
$this->comment = $options->get( 'renderForComment' );
|
||||
|
||||
$this->titleFormatter = $titleFormatter;
|
||||
$this->linkCache = $linkCache;
|
||||
$this->specialPageFactory = $specialPageFactory;
|
||||
|
|
@ -120,6 +138,10 @@ class LinkRenderer {
|
|||
return $this->expandUrls;
|
||||
}
|
||||
|
||||
public function isForComment(): bool {
|
||||
return $this->comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 1.37. Stub threshold feature was removed. See T284917
|
||||
* @param int $threshold
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@
|
|||
namespace MediaWiki\Linker;
|
||||
|
||||
use LinkCache;
|
||||
use MediaWiki\Config\ServiceOptions;
|
||||
use MediaWiki\HookContainer\HookContainer;
|
||||
use MediaWiki\SpecialPage\SpecialPageFactory;
|
||||
use MediaWiki\User\UserIdentity;
|
||||
|
|
@ -72,12 +73,16 @@ class LinkRendererFactory {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param array $options optional array with flags for rendering
|
||||
* * 'renderForComment' set to true if this LinkRender is to be used for edit summary comment
|
||||
*
|
||||
* @return LinkRenderer
|
||||
*/
|
||||
public function create() {
|
||||
public function create( array $options = [ 'renderForComment' => false ] ) {
|
||||
return new LinkRenderer(
|
||||
$this->titleFormatter, $this->linkCache, $this->specialPageFactory,
|
||||
$this->hookContainer
|
||||
$this->hookContainer,
|
||||
new ServiceOptions( LinkRenderer::CONSTRUCTOR_OPTIONS, $options )
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Config\ServiceOptions;
|
||||
use MediaWiki\Linker\LinkRenderer;
|
||||
use MediaWiki\Linker\LinkRendererFactory;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
|
|
@ -231,7 +232,8 @@ class LinkRendererTest extends MediaWikiLangTestCase {
|
|||
$titleFormatter,
|
||||
$linkCache,
|
||||
$specialPageFactory,
|
||||
$hookContainer
|
||||
$hookContainer,
|
||||
new ServiceOptions( LinkRenderer::CONSTRUCTOR_OPTIONS, [ 'renderForComment' => false ] )
|
||||
);
|
||||
$this->assertSame(
|
||||
'',
|
||||
|
|
|
|||
|
|
@ -74,6 +74,10 @@ class LinkRendererFactoryTest extends MediaWikiUnitTestCase {
|
|||
);
|
||||
$this->assertInstanceOf( LinkRenderer::class, $linkRenderer );
|
||||
$this->assertEquals( $val, $linkRenderer->$func(), $func );
|
||||
$this->assertFalse(
|
||||
$linkRenderer->isForComment(),
|
||||
'isForComment should default to false in legacy implementation'
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreate() {
|
||||
|
|
@ -83,6 +87,20 @@ class LinkRendererFactoryTest extends MediaWikiUnitTestCase {
|
|||
$this->specialPageFactory,
|
||||
$this->hookContainer
|
||||
);
|
||||
$this->assertInstanceOf( LinkRenderer::class, $factory->create() );
|
||||
$linkRenderer = $factory->create();
|
||||
$this->assertInstanceOf( LinkRenderer::class, $linkRenderer );
|
||||
$this->assertFalse( $linkRenderer->isForComment(), 'isForComment should default to false' );
|
||||
}
|
||||
|
||||
public function testCreateForComment() {
|
||||
$factory = new LinkRendererFactory(
|
||||
$this->titleFormatter,
|
||||
$this->linkCache,
|
||||
$this->specialPageFactory,
|
||||
$this->hookContainer
|
||||
);
|
||||
$linkRenderer = $factory->create( [ 'renderForComment' => true ] );
|
||||
$this->assertInstanceOf( LinkRenderer::class, $linkRenderer );
|
||||
$this->assertTrue( $linkRenderer->isForComment() );
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue