Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
2016-05-27 16:11:58 +00:00
|
|
|
use MediaWiki\Linker\LinkRendererFactory;
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-27 18:53:04 +00:00
|
|
|
* @covers MediaWiki\Linker\LinkRenderer
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
*/
|
|
|
|
|
class LinkRendererTest extends MediaWikiLangTestCase {
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-27 16:11:58 +00:00
|
|
|
* @var LinkRendererFactory
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
*/
|
2016-05-27 16:11:58 +00:00
|
|
|
private $factory;
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
|
2019-10-20 18:11:08 +00:00
|
|
|
public function setUp() : void {
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgArticlePath' => '/wiki/$1',
|
|
|
|
|
'wgServer' => '//example.org',
|
|
|
|
|
'wgCanonicalServer' => 'http://example.org',
|
|
|
|
|
'wgScriptPath' => '/w',
|
|
|
|
|
'wgScript' => '/w/index.php',
|
|
|
|
|
] );
|
2016-05-27 16:11:58 +00:00
|
|
|
$this->factory = MediaWikiServices::getInstance()->getLinkRendererFactory();
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testMergeAttribs() {
|
|
|
|
|
$target = new TitleValue( NS_SPECIAL, 'Blankpage' );
|
2016-05-27 16:11:58 +00:00
|
|
|
$linkRenderer = $this->factory->create();
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
$link = $linkRenderer->makeBrokenLink( $target, null, [
|
|
|
|
|
// Appended to class
|
|
|
|
|
'class' => 'foobar',
|
|
|
|
|
// Suppresses href attribute
|
|
|
|
|
'href' => false,
|
|
|
|
|
// Extra attribute
|
|
|
|
|
'bar' => 'baz'
|
|
|
|
|
] );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'<a href="/wiki/Special:BlankPage" class="new foobar" '
|
|
|
|
|
. 'title="Special:BlankPage (page does not exist)" bar="baz">'
|
|
|
|
|
. 'Special:BlankPage</a>',
|
|
|
|
|
$link
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testMakeKnownLink() {
|
|
|
|
|
$target = new TitleValue( NS_MAIN, 'Foobar' );
|
2016-05-27 16:11:58 +00:00
|
|
|
$linkRenderer = $this->factory->create();
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
|
|
|
|
|
// Query added
|
|
|
|
|
$this->assertEquals(
|
2019-03-01 21:15:22 +00:00
|
|
|
'<a href="/w/index.php?title=Foobar&foo=bar" title="Foobar">Foobar</a>',
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
$linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$linkRenderer->setForceArticlePath( true );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'<a href="/wiki/Foobar?foo=bar" title="Foobar">Foobar</a>',
|
|
|
|
|
$linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// expand = HTTPS
|
|
|
|
|
$linkRenderer->setForceArticlePath( false );
|
|
|
|
|
$linkRenderer->setExpandURLs( PROTO_HTTPS );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'<a href="https://example.org/wiki/Foobar" title="Foobar">Foobar</a>',
|
|
|
|
|
$linkRenderer->makeKnownLink( $target )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testMakeBrokenLink() {
|
|
|
|
|
$target = new TitleValue( NS_MAIN, 'Foobar' );
|
|
|
|
|
$special = new TitleValue( NS_SPECIAL, 'Foobar' );
|
2016-05-27 16:11:58 +00:00
|
|
|
$linkRenderer = $this->factory->create();
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
|
|
|
|
|
// action=edit&redlink=1 added
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'<a href="/w/index.php?title=Foobar&action=edit&redlink=1" '
|
|
|
|
|
. 'class="new" title="Foobar (page does not exist)">Foobar</a>',
|
|
|
|
|
$linkRenderer->makeBrokenLink( $target )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// action=edit&redlink=1 not added due to action query parameter
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'<a href="/w/index.php?title=Foobar&action=foobar" class="new" '
|
|
|
|
|
. 'title="Foobar (page does not exist)">Foobar</a>',
|
|
|
|
|
$linkRenderer->makeBrokenLink( $target, null, [], [ 'action' => 'foobar' ] )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// action=edit&redlink=1 not added due to NS_SPECIAL
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
|
|
|
|
|
. '(page does not exist)">Special:Foobar</a>',
|
|
|
|
|
$linkRenderer->makeBrokenLink( $special )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// fragment stripped
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'<a href="/w/index.php?title=Foobar&action=edit&redlink=1" '
|
|
|
|
|
. 'class="new" title="Foobar (page does not exist)">Foobar</a>',
|
|
|
|
|
$linkRenderer->makeBrokenLink( $target->createFragmentTarget( 'foobar' ) )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testMakeLink() {
|
2016-05-27 16:11:58 +00:00
|
|
|
$linkRenderer = $this->factory->create();
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
$foobar = new TitleValue( NS_SPECIAL, 'Foobar' );
|
|
|
|
|
$blankpage = new TitleValue( NS_SPECIAL, 'Blankpage' );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
|
|
|
|
|
. '(page does not exist)">foo</a>',
|
|
|
|
|
$linkRenderer->makeLink( $foobar, 'foo' )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'<a href="/wiki/Special:BlankPage" title="Special:BlankPage">blank</a>',
|
|
|
|
|
$linkRenderer->makeLink( $blankpage, 'blank' )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
|
|
|
|
|
. '(page does not exist)"><script>evil()</script></a>',
|
|
|
|
|
$linkRenderer->makeLink( $foobar, '<script>evil()</script>' )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
|
|
|
|
|
. '(page does not exist)"><script>evil()</script></a>',
|
|
|
|
|
$linkRenderer->makeLink( $foobar, new HtmlArmor( '<script>evil()</script>' ) )
|
|
|
|
|
);
|
2018-06-10 19:12:52 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'<a href="#fragment">fragment</a>',
|
|
|
|
|
$linkRenderer->makeLink( Title::newFromText( '#fragment' ) )
|
|
|
|
|
);
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
}
|
2016-05-27 16:11:58 +00:00
|
|
|
|
|
|
|
|
public function testGetLinkClasses() {
|
2019-07-03 12:17:16 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
$wanCache = $services->getMainWANObjectCache();
|
|
|
|
|
$titleFormatter = $services->getTitleFormatter();
|
|
|
|
|
$nsInfo = $services->getNamespaceInfo();
|
2019-04-09 09:30:58 +00:00
|
|
|
$linkCache = new LinkCache( $titleFormatter, $wanCache, $nsInfo );
|
2016-05-27 16:11:58 +00:00
|
|
|
$foobarTitle = new TitleValue( NS_MAIN, 'FooBar' );
|
|
|
|
|
$redirectTitle = new TitleValue( NS_MAIN, 'Redirect' );
|
|
|
|
|
$userTitle = new TitleValue( NS_USER, 'Someuser' );
|
|
|
|
|
$linkCache->addGoodLinkObj(
|
|
|
|
|
1, // id
|
|
|
|
|
$foobarTitle,
|
|
|
|
|
10, // len
|
|
|
|
|
0 // redir
|
|
|
|
|
);
|
|
|
|
|
$linkCache->addGoodLinkObj(
|
|
|
|
|
2, // id
|
|
|
|
|
$redirectTitle,
|
|
|
|
|
10, // len
|
|
|
|
|
1 // redir
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$linkCache->addGoodLinkObj(
|
|
|
|
|
3, // id
|
|
|
|
|
$userTitle,
|
|
|
|
|
10, // len
|
|
|
|
|
0 // redir
|
|
|
|
|
);
|
|
|
|
|
|
2018-08-05 12:44:11 +00:00
|
|
|
$linkRenderer = new LinkRenderer( $titleFormatter, $linkCache, $nsInfo );
|
2016-05-27 16:11:58 +00:00
|
|
|
$linkRenderer->setStubThreshold( 0 );
|
2019-09-17 14:28:35 +00:00
|
|
|
$this->assertSame(
|
2016-05-27 16:11:58 +00:00
|
|
|
'',
|
|
|
|
|
$linkRenderer->getLinkClasses( $foobarTitle )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$linkRenderer->setStubThreshold( 20 );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'stub',
|
|
|
|
|
$linkRenderer->getLinkClasses( $foobarTitle )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$linkRenderer->setStubThreshold( 0 );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'mw-redirect',
|
|
|
|
|
$linkRenderer->getLinkClasses( $redirectTitle )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$linkRenderer->setStubThreshold( 20 );
|
2019-09-17 14:28:35 +00:00
|
|
|
$this->assertSame(
|
2016-05-27 16:11:58 +00:00
|
|
|
'',
|
|
|
|
|
$linkRenderer->getLinkClasses( $userTitle )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 18:11:08 +00:00
|
|
|
public function tearDown() : void {
|
2018-10-25 04:31:58 +00:00
|
|
|
Title::clearCaches();
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
Add LinkRenderer (rewrite of Linker::link())
This is a rewrite of Linker::link() to a non-static, LinkTarget-based
interface. Users of plain Linker::link() with no options can use the
LinkRenderer instance provided by MediaWikiServices. Others that
have specific options should create and configure their own instance,
which can be used to create as many links as necessary.
The main entrypoints for making links are:
* ->makeLink( $target, $text, $attribs, $query );
* ->makeKnownLink( $target, $text, $attribs, $query );
* ->makeBrokenLink( $target, $text, $attribs, $query );
The order of the parameters are the same as Linker::link(), except
$options are now part of the LinkRenderer instance, and
known/broken status requires calling the function explicitly.
Additionally, instead of passing in raw $html for the link text, the
$text parameter will automatically be escaped unless it is specially
marked as safe HTML using the MediaWiki\Linker\HtmlArmor class.
The LinkBegin and LinkEnd hooks are now deprecated, but still function
for backwards-compatability. Clients should migrate to the nearly-
equivalent LinkRendererBegin and LinkRendererEnd hooks.
The main differences between the hooks are:
* Passing HtmlPageLinkRenderer object instead of deprecated DummyLinker
* Using LinkTarget instead of Title
* Begin hook can no longer change known/broken status of link. Use the
TitleIsAlwaysKnown hook for that.
* $options are no longer passed, they can be read (but shouldn't be
modified!) from the LinkRenderer object.
Bug: T469
Change-Id: I057cc86ae6404a080aa3c8e0e956ecbb10a897d5
2016-04-21 20:13:21 +00:00
|
|
|
}
|