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
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @author Kunal Mehta <legoktm@member.fsf.org>
|
|
|
|
|
*/
|
|
|
|
|
namespace MediaWiki\Linker;
|
|
|
|
|
|
|
|
|
|
use DummyLinker;
|
|
|
|
|
use Hooks;
|
|
|
|
|
use Html;
|
|
|
|
|
use HtmlArmor;
|
2016-05-27 16:11:58 +00:00
|
|
|
use LinkCache;
|
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 Linker;
|
|
|
|
|
use MediaWiki\MediaWikiServices;
|
2018-08-05 12:44:11 +00:00
|
|
|
use NamespaceInfo;
|
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 Sanitizer;
|
|
|
|
|
use Title;
|
|
|
|
|
use TitleFormatter;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class that generates HTML <a> links for pages.
|
|
|
|
|
*
|
2016-07-12 22:26:53 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual: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
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
class LinkRenderer {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether to force the pretty article path
|
|
|
|
|
*
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
private $forceArticlePath = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A PROTO_* constant or false
|
|
|
|
|
*
|
|
|
|
|
* @var string|bool|int
|
|
|
|
|
*/
|
|
|
|
|
private $expandUrls = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
private $stubThreshold = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var TitleFormatter
|
|
|
|
|
*/
|
|
|
|
|
private $titleFormatter;
|
|
|
|
|
|
2016-05-27 16:11:58 +00:00
|
|
|
/**
|
|
|
|
|
* @var LinkCache
|
|
|
|
|
*/
|
|
|
|
|
private $linkCache;
|
|
|
|
|
|
2018-08-05 12:44:11 +00:00
|
|
|
/**
|
|
|
|
|
* @var NamespaceInfo
|
|
|
|
|
*/
|
|
|
|
|
private $nsInfo;
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* Whether to run the legacy Linker hooks
|
|
|
|
|
*
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
private $runLegacyBeginHook = true;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param TitleFormatter $titleFormatter
|
2016-05-27 16:11:58 +00:00
|
|
|
* @param LinkCache $linkCache
|
2018-08-05 12:44:11 +00:00
|
|
|
* @param NamespaceInfo $nsInfo
|
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
|
|
|
*/
|
2018-08-05 12:44:11 +00:00
|
|
|
public function __construct(
|
|
|
|
|
TitleFormatter $titleFormatter, LinkCache $linkCache, NamespaceInfo $nsInfo
|
|
|
|
|
) {
|
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
|
|
|
$this->titleFormatter = $titleFormatter;
|
2016-05-27 16:11:58 +00:00
|
|
|
$this->linkCache = $linkCache;
|
2018-08-05 12:44:11 +00:00
|
|
|
$this->nsInfo = $nsInfo;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param bool $force
|
|
|
|
|
*/
|
|
|
|
|
public function setForceArticlePath( $force ) {
|
|
|
|
|
$this->forceArticlePath = $force;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function getForceArticlePath() {
|
|
|
|
|
return $this->forceArticlePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string|bool|int $expand A PROTO_* constant or false
|
|
|
|
|
*/
|
|
|
|
|
public function setExpandURLs( $expand ) {
|
|
|
|
|
$this->expandUrls = $expand;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string|bool|int a PROTO_* constant or false
|
|
|
|
|
*/
|
|
|
|
|
public function getExpandURLs() {
|
|
|
|
|
return $this->expandUrls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $threshold
|
|
|
|
|
*/
|
|
|
|
|
public function setStubThreshold( $threshold ) {
|
|
|
|
|
$this->stubThreshold = $threshold;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getStubThreshold() {
|
|
|
|
|
return $this->stubThreshold;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param bool $run
|
|
|
|
|
*/
|
|
|
|
|
public function setRunLegacyBeginHook( $run ) {
|
|
|
|
|
$this->runLegacyBeginHook = $run;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param LinkTarget $target
|
|
|
|
|
* @param string|HtmlArmor|null $text
|
|
|
|
|
* @param array $extraAttribs
|
|
|
|
|
* @param array $query
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function makeLink(
|
|
|
|
|
LinkTarget $target, $text = null, array $extraAttribs = [], array $query = []
|
|
|
|
|
) {
|
|
|
|
|
$title = Title::newFromLinkTarget( $target );
|
|
|
|
|
if ( $title->isKnown() ) {
|
|
|
|
|
return $this->makeKnownLink( $target, $text, $extraAttribs, $query );
|
|
|
|
|
} else {
|
|
|
|
|
return $this->makeBrokenLink( $target, $text, $extraAttribs, $query );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the options in the legacy format
|
|
|
|
|
*
|
|
|
|
|
* @param bool $isKnown Whether the link is known or broken
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function getLegacyOptions( $isKnown ) {
|
|
|
|
|
$options = [ 'stubThreshold' => $this->stubThreshold ];
|
|
|
|
|
if ( $this->forceArticlePath ) {
|
|
|
|
|
$options[] = 'forcearticlepath';
|
|
|
|
|
}
|
|
|
|
|
if ( $this->expandUrls === PROTO_HTTP ) {
|
|
|
|
|
$options[] = 'http';
|
|
|
|
|
} elseif ( $this->expandUrls === PROTO_HTTPS ) {
|
|
|
|
|
$options[] = 'https';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$options[] = $isKnown ? 'known' : 'broken';
|
|
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function runBeginHook( LinkTarget $target, &$text, &$extraAttribs, &$query, $isKnown ) {
|
|
|
|
|
$ret = null;
|
|
|
|
|
if ( !Hooks::run( 'HtmlPageLinkRendererBegin',
|
|
|
|
|
[ $this, $target, &$text, &$extraAttribs, &$query, &$ret ] )
|
|
|
|
|
) {
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now run the legacy hook
|
|
|
|
|
return $this->runLegacyBeginHook( $target, $text, $extraAttribs, $query, $isKnown );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function runLegacyBeginHook( LinkTarget $target, &$text, &$extraAttribs, &$query,
|
|
|
|
|
$isKnown
|
|
|
|
|
) {
|
|
|
|
|
if ( !$this->runLegacyBeginHook || !Hooks::isRegistered( 'LinkBegin' ) ) {
|
|
|
|
|
// Disabled, or nothing registered
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$realOptions = $options = $this->getLegacyOptions( $isKnown );
|
|
|
|
|
$ret = null;
|
|
|
|
|
$dummy = new DummyLinker();
|
|
|
|
|
$title = Title::newFromLinkTarget( $target );
|
2016-05-25 19:25:12 +00:00
|
|
|
if ( $text !== null ) {
|
|
|
|
|
$realHtml = $html = HtmlArmor::getHtml( $text );
|
|
|
|
|
} else {
|
|
|
|
|
$realHtml = $html = null;
|
|
|
|
|
}
|
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
|
|
|
if ( !Hooks::run( 'LinkBegin',
|
2018-06-10 18:30:15 +00:00
|
|
|
[ $dummy, $title, &$html, &$extraAttribs, &$query, &$options, &$ret ], '1.28' )
|
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
|
|
|
) {
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $html !== null && $html !== $realHtml ) {
|
|
|
|
|
// &$html was modified, so re-armor it as $text
|
|
|
|
|
$text = new HtmlArmor( $html );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if they changed any of the options, hopefully not!
|
|
|
|
|
if ( $options !== $realOptions ) {
|
|
|
|
|
$factory = MediaWikiServices::getInstance()->getLinkRendererFactory();
|
|
|
|
|
// They did, so create a separate instance and have that take over the rest
|
|
|
|
|
$newRenderer = $factory->createFromLegacyOptions( $options );
|
|
|
|
|
// Don't recurse the hook...
|
|
|
|
|
$newRenderer->setRunLegacyBeginHook( false );
|
|
|
|
|
if ( in_array( 'known', $options, true ) ) {
|
|
|
|
|
return $newRenderer->makeKnownLink( $title, $text, $extraAttribs, $query );
|
|
|
|
|
} elseif ( in_array( 'broken', $options, true ) ) {
|
|
|
|
|
return $newRenderer->makeBrokenLink( $title, $text, $extraAttribs, $query );
|
|
|
|
|
} else {
|
|
|
|
|
return $newRenderer->makeLink( $title, $text, $extraAttribs, $query );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-27 16:11:58 +00:00
|
|
|
* If you have already looked up the proper CSS classes using LinkRenderer::getLinkClasses()
|
2016-05-25 23:37:21 +00:00
|
|
|
* or some other method, use this to avoid looking it up again.
|
|
|
|
|
*
|
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
|
|
|
* @param LinkTarget $target
|
|
|
|
|
* @param string|HtmlArmor|null $text
|
2016-05-25 23:37:21 +00:00
|
|
|
* @param string $classes CSS classes to add
|
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
|
|
|
* @param array $extraAttribs
|
|
|
|
|
* @param array $query
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2016-05-25 23:37:21 +00:00
|
|
|
public function makePreloadedLink(
|
2018-06-08 21:32:55 +00:00
|
|
|
LinkTarget $target, $text = null, $classes = '', array $extraAttribs = [], array $query = []
|
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
|
|
|
) {
|
|
|
|
|
// Run begin hook
|
|
|
|
|
$ret = $this->runBeginHook( $target, $text, $extraAttribs, $query, true );
|
|
|
|
|
if ( $ret !== null ) {
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
$target = $this->normalizeTarget( $target );
|
|
|
|
|
$url = $this->getLinkURL( $target, $query );
|
2016-05-25 23:37:21 +00:00
|
|
|
$attribs = [ 'class' => $classes ];
|
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
|
|
|
$prefixedText = $this->titleFormatter->getPrefixedText( $target );
|
|
|
|
|
if ( $prefixedText !== '' ) {
|
|
|
|
|
$attribs['title'] = $prefixedText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attribs = [
|
|
|
|
|
'href' => $url,
|
|
|
|
|
] + $this->mergeAttribs( $attribs, $extraAttribs );
|
|
|
|
|
|
|
|
|
|
if ( $text === null ) {
|
|
|
|
|
$text = $this->getLinkText( $target );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->buildAElement( $target, $text, $attribs, true );
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-25 23:37:21 +00:00
|
|
|
/**
|
|
|
|
|
* @param LinkTarget $target
|
|
|
|
|
* @param string|HtmlArmor|null $text
|
|
|
|
|
* @param array $extraAttribs
|
|
|
|
|
* @param array $query
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function makeKnownLink(
|
|
|
|
|
LinkTarget $target, $text = null, array $extraAttribs = [], array $query = []
|
|
|
|
|
) {
|
|
|
|
|
$classes = [];
|
|
|
|
|
if ( $target->isExternal() ) {
|
|
|
|
|
$classes[] = 'extiw';
|
|
|
|
|
}
|
2016-05-27 16:11:58 +00:00
|
|
|
$colour = $this->getLinkClasses( $target );
|
2016-05-25 23:37:21 +00:00
|
|
|
if ( $colour !== '' ) {
|
|
|
|
|
$classes[] = $colour;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makePreloadedLink(
|
|
|
|
|
$target,
|
|
|
|
|
$text,
|
2019-04-20 15:23:50 +00:00
|
|
|
implode( ' ', $classes ),
|
2016-05-25 23:37:21 +00:00
|
|
|
$extraAttribs,
|
|
|
|
|
$query
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* @param LinkTarget $target
|
|
|
|
|
* @param string|HtmlArmor|null $text
|
|
|
|
|
* @param array $extraAttribs
|
|
|
|
|
* @param array $query
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function makeBrokenLink(
|
|
|
|
|
LinkTarget $target, $text = null, array $extraAttribs = [], array $query = []
|
|
|
|
|
) {
|
|
|
|
|
// Run legacy hook
|
|
|
|
|
$ret = $this->runBeginHook( $target, $text, $extraAttribs, $query, false );
|
|
|
|
|
if ( $ret !== null ) {
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# We don't want to include fragments for broken links, because they
|
|
|
|
|
# generally make no sense.
|
|
|
|
|
if ( $target->hasFragment() ) {
|
|
|
|
|
$target = $target->createFragmentTarget( '' );
|
|
|
|
|
}
|
|
|
|
|
$target = $this->normalizeTarget( $target );
|
|
|
|
|
|
|
|
|
|
if ( !isset( $query['action'] ) && $target->getNamespace() !== NS_SPECIAL ) {
|
|
|
|
|
$query['action'] = 'edit';
|
|
|
|
|
$query['redlink'] = '1';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$url = $this->getLinkURL( $target, $query );
|
2016-05-25 23:37:21 +00:00
|
|
|
$attribs = [ 'class' => 'new' ];
|
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
|
|
|
$prefixedText = $this->titleFormatter->getPrefixedText( $target );
|
|
|
|
|
if ( $prefixedText !== '' ) {
|
|
|
|
|
// This ends up in parser cache!
|
|
|
|
|
$attribs['title'] = wfMessage( 'red-link-title', $prefixedText )
|
|
|
|
|
->inContentLanguage()
|
|
|
|
|
->text();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attribs = [
|
|
|
|
|
'href' => $url,
|
|
|
|
|
] + $this->mergeAttribs( $attribs, $extraAttribs );
|
|
|
|
|
|
|
|
|
|
if ( $text === null ) {
|
|
|
|
|
$text = $this->getLinkText( $target );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->buildAElement( $target, $text, $attribs, false );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Builds the final <a> element
|
|
|
|
|
*
|
|
|
|
|
* @param LinkTarget $target
|
|
|
|
|
* @param string|HtmlArmor $text
|
|
|
|
|
* @param array $attribs
|
|
|
|
|
* @param bool $isKnown
|
|
|
|
|
* @return null|string
|
|
|
|
|
*/
|
|
|
|
|
private function buildAElement( LinkTarget $target, $text, array $attribs, $isKnown ) {
|
|
|
|
|
$ret = null;
|
|
|
|
|
if ( !Hooks::run( 'HtmlPageLinkRendererEnd',
|
|
|
|
|
[ $this, $target, $isKnown, &$text, &$attribs, &$ret ] )
|
|
|
|
|
) {
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$html = HtmlArmor::getHtml( $text );
|
|
|
|
|
|
|
|
|
|
// Run legacy hook
|
|
|
|
|
if ( Hooks::isRegistered( 'LinkEnd' ) ) {
|
|
|
|
|
$dummy = new DummyLinker();
|
|
|
|
|
$title = Title::newFromLinkTarget( $target );
|
|
|
|
|
$options = $this->getLegacyOptions( $isKnown );
|
|
|
|
|
if ( !Hooks::run( 'LinkEnd',
|
2018-06-10 18:30:15 +00:00
|
|
|
[ $dummy, $title, $options, &$html, &$attribs, &$ret ], '1.28' )
|
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
|
|
|
) {
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Html::rawElement( 'a', $attribs, $html );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param LinkTarget $target
|
|
|
|
|
* @return string non-escaped text
|
|
|
|
|
*/
|
|
|
|
|
private function getLinkText( LinkTarget $target ) {
|
|
|
|
|
$prefixedText = $this->titleFormatter->getPrefixedText( $target );
|
|
|
|
|
// If the target is just a fragment, with no title, we return the fragment
|
|
|
|
|
// text. Otherwise, we return the title text itself.
|
|
|
|
|
if ( $prefixedText === '' && $target->hasFragment() ) {
|
|
|
|
|
return $target->getFragment();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $prefixedText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getLinkURL( LinkTarget $target, array $query = [] ) {
|
|
|
|
|
// TODO: Use a LinkTargetResolver service instead of Title
|
|
|
|
|
$title = Title::newFromLinkTarget( $target );
|
|
|
|
|
if ( $this->forceArticlePath ) {
|
|
|
|
|
$realQuery = $query;
|
|
|
|
|
$query = [];
|
|
|
|
|
} else {
|
|
|
|
|
$realQuery = [];
|
|
|
|
|
}
|
2016-06-01 03:11:31 +00:00
|
|
|
$url = $title->getLinkURL( $query, false, $this->expandUrls );
|
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
|
|
|
|
|
|
|
|
if ( $this->forceArticlePath && $realQuery ) {
|
|
|
|
|
$url = wfAppendQuery( $url, $realQuery );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Normalizes the provided target
|
|
|
|
|
*
|
|
|
|
|
* @todo move the code from Linker actually here
|
|
|
|
|
* @param LinkTarget $target
|
|
|
|
|
* @return LinkTarget
|
|
|
|
|
*/
|
|
|
|
|
private function normalizeTarget( LinkTarget $target ) {
|
|
|
|
|
return Linker::normaliseSpecialPage( $target );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Merges two sets of attributes
|
|
|
|
|
*
|
|
|
|
|
* @param array $defaults
|
|
|
|
|
* @param array $attribs
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function mergeAttribs( $defaults, $attribs ) {
|
|
|
|
|
if ( !$attribs ) {
|
|
|
|
|
return $defaults;
|
|
|
|
|
}
|
|
|
|
|
# Merge the custom attribs with the default ones, and iterate
|
|
|
|
|
# over that, deleting all "false" attributes.
|
|
|
|
|
$ret = [];
|
|
|
|
|
$merged = Sanitizer::mergeAttributes( $defaults, $attribs );
|
|
|
|
|
foreach ( $merged as $key => $val ) {
|
|
|
|
|
# A false value suppresses the attribute
|
|
|
|
|
if ( $val !== false ) {
|
|
|
|
|
$ret[$key] = $val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-27 16:11:58 +00:00
|
|
|
/**
|
|
|
|
|
* Return the CSS classes of a known link
|
|
|
|
|
*
|
|
|
|
|
* @param LinkTarget $target
|
|
|
|
|
* @return string CSS class
|
|
|
|
|
*/
|
|
|
|
|
public function getLinkClasses( LinkTarget $target ) {
|
|
|
|
|
// Make sure the target is in the cache
|
|
|
|
|
$id = $this->linkCache->addLinkObj( $target );
|
|
|
|
|
if ( $id == 0 ) {
|
|
|
|
|
// Doesn't exist
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->linkCache->getGoodLinkFieldObj( $target, 'redirect' ) ) {
|
|
|
|
|
# Page is a redirect
|
|
|
|
|
return 'mw-redirect';
|
2018-08-05 12:44:11 +00:00
|
|
|
} elseif (
|
|
|
|
|
$this->stubThreshold > 0 && $this->nsInfo->isContent( $target->getNamespace() ) &&
|
|
|
|
|
$this->linkCache->getGoodLinkFieldObj( $target, 'length' ) < $this->stubThreshold
|
2016-05-27 16:11:58 +00:00
|
|
|
) {
|
|
|
|
|
# Page is a stub
|
|
|
|
|
return 'stub';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
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
|
|
|
}
|