MediaWiki master
MediaWiki\Linker\LinkRenderer Class Reference

Class that generates HTML for internal links. More...

Public Member Functions

 __construct (private TitleFormatter $titleFormatter, private LinkCache $linkCache, private SpecialPageFactory $specialPageFactory, HookContainer $hookContainer, TempUserConfig $tempUserConfig, TempUserDetailsLookup $tempUserDetailsLookup, UserIdentityLookup $userIdentityLookup, UserNameUtils $userNameUtils, private UrlUtils $urlUtils, ServiceOptions $options, bool $renderForComment)
 
 getExpandURLs ()
 
 getExternalLinkRel ( $url=false, $title=null)
 Get the rel attribute for a particular external link.
 
 getForceArticlePath ()
 
 getLinkClasses ( $target, bool $isDefaultCaption=false)
 Returns CSS classes to add to a known link.
 
 isDefaultLinkCaption (LinkTarget|PageReference|null $targetPage, string|HtmlArmor|null $caption)
 Checks if the provided caption can be considered a default link caption for the given target page.
 
 isForComment ()
 True when the links will be rendered in an edit summary or log comment.
 
 makeBrokenLink ( $target, $text=null, array $extraAttribs=[], array $query=[])
 Make a link that's styled as if the target page doesn't exist (a "red link").
 
 makeExternalLink (string $url, $text, $title, $linktype='', $attribs=[])
 Make an external link.
 
 makeKnownLink ( $target, $text=null, array $extraAttribs=[], array $query=[])
 Make a link that's styled as if the target page exists (usually a "blue link", although the styling might depend on e.g.
 
 makeLink ( $target, $text=null, array $extraAttribs=[], array $query=[])
 Render a wikilink.
 
 makePreloadedLink ( $target, $text=null, $classes=[], array $extraAttribs=[], array $query=[])
 Make a link that's styled as if the target page exists (a "blue link"), with a specified class attribute.
 
 makeRedirectHeader (Language $lang, Title $target, bool $forceKnown=false, bool $addLinkTag=false)
 Return the HTML for the top of a redirect page.
 
 makeUserLink (UserIdentity $targetUser, IContextSource $context, ?string $altUserName=null, array $attributes=[])
 Render a user page link (or user contributions for anonymous and temporary users).
 
 normalizeTarget ( $target)
 Normalizes the provided target.
 
 setExpandURLs ( $expand)
 Whether/how to expand URLs.
 
 setForceArticlePath ( $force)
 Whether to force the link to use the article path ($wgArticlePath) even if a query string is present, resulting in URLs like /wiki/Main_Page?action=foobar.
 

Public Attributes

const CONSTRUCTOR_OPTIONS
 

Detailed Description

Class that generates HTML for internal links.

See the Linker class for other kinds of links.

See also
https://www.mediawiki.org/wiki/Manual:LinkRenderer
Since
1.28

Definition at line 41 of file LinkRenderer.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Linker\LinkRenderer::__construct ( private TitleFormatter $titleFormatter,
private LinkCache $linkCache,
private SpecialPageFactory $specialPageFactory,
HookContainer $hookContainer,
TempUserConfig $tempUserConfig,
TempUserDetailsLookup $tempUserDetailsLookup,
UserIdentityLookup $userIdentityLookup,
UserNameUtils $userNameUtils,
private UrlUtils $urlUtils,
ServiceOptions $options,
bool $renderForComment )

Member Function Documentation

◆ getExpandURLs()

MediaWiki\Linker\LinkRenderer::getExpandURLs ( )
Returns
string|bool|int a PROTO_* constant or false for no expansion
See also
setExpandURLs()

Definition at line 148 of file LinkRenderer.php.

◆ getExternalLinkRel()

MediaWiki\Linker\LinkRenderer::getExternalLinkRel ( $url = false,
$title = null )

Get the rel attribute for a particular external link.

Since
1.47
Parameters
string | false$urlOptional URL, to extract the domain from for rel => nofollow if appropriate
LinkTarget | PageReference | null$titleOptional page, for wgNoFollowNsExceptions lookups
Returns
string|null Rel attribute for $url

Definition at line 751 of file LinkRenderer.php.

◆ getForceArticlePath()

MediaWiki\Linker\LinkRenderer::getForceArticlePath ( )
Returns
bool
See also
setForceArticlePath()

Definition at line 130 of file LinkRenderer.php.

◆ getLinkClasses()

MediaWiki\Linker\LinkRenderer::getLinkClasses ( $target,
bool $isDefaultCaption = false )

Returns CSS classes to add to a known link.

Note that most CSS classes set on wikilinks are actually handled elsewhere (e.g. in makeKnownLink() or in LinkHolderArray).

Parameters
LinkTarget | PageReference$targetPage that will be visited when the user clicks on the link.
bool$isDefaultCaptionWhether the link text is the default caption for the target.
Returns
string CSS class

Definition at line 631 of file LinkRenderer.php.

◆ isDefaultLinkCaption()

MediaWiki\Linker\LinkRenderer::isDefaultLinkCaption ( LinkTarget|PageReference|null $targetPage,
string|HtmlArmor|null $caption )

Checks if the provided caption can be considered a default link caption for the given target page.

A caption is default if, after stripping HTML tags and decoding HTML entities in it:

  • it's a suffix of the target's prefixed title, and
  • the target's prefixed title, after stripping the caption from the end, ends with a colon, slash, or is empty.

NOTE: For a given title, there may be multiple captions that are considered default. For example, for "User:Admin/Sandbox", the default captions will be: "Sandbox", "Admin/Sandbox", and "User:Admin/Sandbox".

Examples:

  • Target: "Help:Contents", Caption: "Contents" => true
  • Target: "Help:Contents", Caption: "Help:Contents" => true
  • Target: "Help:Contents", Caption: "ents" => false
  • Target: "User:~2025-1", Caption: "~2025-1" => true
Returns
bool

Definition at line 704 of file LinkRenderer.php.

◆ isForComment()

MediaWiki\Linker\LinkRenderer::isForComment ( )

True when the links will be rendered in an edit summary or log comment.

Definition at line 155 of file LinkRenderer.php.

◆ makeBrokenLink()

MediaWiki\Linker\LinkRenderer::makeBrokenLink ( $target,
$text = null,
array $extraAttribs = [],
array $query = [] )

Make a link that's styled as if the target page doesn't exist (a "red link").

Parameters
LinkTarget | PageReference$targetPage that will be visited when the user clicks on the link.
string | HtmlArmor | null$textText that the user can click on to visit the link.
array$extraAttribsAttributes you would like to add to the tag. For example, if you would like to add title="Text when hovering!", you would set this to [ 'title' => 'Text when hovering!' ]
array$queryParameters you would like to add to the URL. For example, to add ?redirect=no&debug=true, you would pass ‘[ 'redirect’ => 'no', 'debug' => 'true' ]`
Returns
string

Definition at line 329 of file LinkRenderer.php.

References $url, NS_SPECIAL, and wfMessage().

◆ makeExternalLink()

MediaWiki\Linker\LinkRenderer::makeExternalLink ( string $url,
$text,
$title,
$linktype = '',
$attribs = [] )

Make an external link.

Since
1.43
Parameters
string$urlURL to link to
string | HtmlArmor | Message$textText of link; will be escaped if a string.
LinkTarget | PageReference$titleWhere the link is being rendered, used for title specific link attributes
string$linktypeType of external link. Gets added to the classes
array$attribsArray of extra attributes to
Returns
string

Definition at line 394 of file LinkRenderer.php.

References $success, $url, NS_SPECIAL, and wfDebug().

◆ makeKnownLink()

MediaWiki\Linker\LinkRenderer::makeKnownLink ( $target,
$text = null,
array $extraAttribs = [],
array $query = [] )

Make a link that's styled as if the target page exists (usually a "blue link", although the styling might depend on e.g.

whether the target is a redirect).

This will result in a DB lookup if the title wasn't cached yet. If you want to avoid that, and don't care about matching the exact styling of links within page content, you can use makePreloadedLink() instead.

Parameters
LinkTarget | PageReference$targetPage that will be visited when the user clicks on the link.
string | HtmlArmor | null$textText that the user can click on to visit the link.
array$extraAttribsAttributes you would like to add to the tag. For example, if you would like to add title="Text when hovering!", you would set this to [ 'title' => 'Text when hovering!' ]
array$queryParameters you would like to add to the URL. For example, to add ?redirect=no&debug=true, you would pass ‘[ 'redirect’ => 'no', 'debug' => 'true' ]`
Returns
string HTML

Definition at line 283 of file LinkRenderer.php.

Referenced by MediaWiki\Specials\Pager\PagerTools\__construct(), MediaWiki\Logging\LogEventsList\getBlockLogWarningBox(), and MediaWiki\Gallery\TraditionalImageGallery\getCaptionHtml().

◆ makeLink()

MediaWiki\Linker\LinkRenderer::makeLink ( $target,
$text = null,
array $extraAttribs = [],
array $query = [] )

Render a wikilink.

Will call makeKnownLink() or makeBrokenLink() as appropriate.

Parameters
LinkTarget | PageReference$targetPage that will be visited when the user clicks on the link.
string | HtmlArmor | null$textText that the user can click on to visit the link.
array$extraAttribsAttributes you would like to add to the tag. For example, if you would like to add title="Text when hovering!", you would set this to [ 'title' => 'Text when hovering!' ]
array$queryParameters you would like to add to the URL. For example, to add ?redirect=no&debug=true, you would pass ‘[ 'redirect’ => 'no', 'debug' => 'true' ]`
Returns
string HTML

Definition at line 178 of file LinkRenderer.php.

◆ makePreloadedLink()

MediaWiki\Linker\LinkRenderer::makePreloadedLink ( $target,
$text = null,
$classes = [],
array $extraAttribs = [],
array $query = [] )

Make a link that's styled as if the target page exists (a "blue link"), with a specified class attribute.

Usually you should use makeLink() or makeKnownLink() instead, which will select the CSS classes automatically. Use this method if the exact styling doesn't matter and you want to ensure no extra DB lookup happens, e.g. for links generated by the skin.

Parameters
LinkTarget | PageReference$targetPage that will be visited when the user clicks on the link.
string | HtmlArmor | null$textText that the user can click on to visit the link.
string | array$classesCSS classes to add
array$extraAttribsAttributes you would like to add to the tag. For example, if you would like to add title="Text when hovering!", you would set this to [ 'title' => 'Text when hovering!' ]
array$queryParameters you would like to add to the URL. For example, to add ?redirect=no&debug=true, you would pass ‘[ 'redirect’ => 'no', 'debug' => 'true' ]`
Returns
string

Definition at line 230 of file LinkRenderer.php.

References $url.

◆ makeRedirectHeader()

MediaWiki\Linker\LinkRenderer::makeRedirectHeader ( Language $lang,
Title $target,
bool $forceKnown = false,
bool $addLinkTag = false )

Return the HTML for the top of a redirect page.

Chances are you should just be using the ParserOutput from WikitextContent::getParserOutput (which will have this header added automatically) instead of calling this for redirects.

If creating your own redirect-alike, please use return value of this method to set the 'core:redirect-header' extension data field in your ParserOutput, rather than concatenating HTML directly. See WikitextContentHandler::fillParserOutput().

Since
1.41
Parameters
Language$lang
Title$targetDestination to redirect
bool$forceKnownShould the image be shown as a bluelink regardless of existence?
bool$addLinkTagShould a <link> tag be added?
Returns
string Containing HTML with redirect link

Definition at line 469 of file LinkRenderer.php.

References MediaWiki\Title\Title\getFullText(), MediaWiki\Title\Title\isRedirect(), and wfMessage().

◆ makeUserLink()

MediaWiki\Linker\LinkRenderer::makeUserLink ( UserIdentity $targetUser,
IContextSource $context,
?string $altUserName = null,
array $attributes = [] )

Render a user page link (or user contributions for anonymous and temporary users).

Returns potentially cached link HTML.

Parameters
UserIdentity$targetUserThe user to render a link for.
IContextSource$context
?string$altUserNameOptional text to display instead of the user name, or null to use the user name.
array<string,string>$attributes Optional extra HTML attributes for the link.
Returns
string HTML fragment
Since
1.45

Definition at line 522 of file LinkRenderer.php.

◆ normalizeTarget()

MediaWiki\Linker\LinkRenderer::normalizeTarget ( $target)

Normalizes the provided target.

Access: internal
For use by Linker::getImageLinkMTOParams()
Parameters
LinkTarget | PageReference$targetPage that will be visited when the user clicks on the link.
Returns
MWLinkTarget

Definition at line 603 of file LinkRenderer.php.

References NS_SPECIAL.

◆ setExpandURLs()

MediaWiki\Linker\LinkRenderer::setExpandURLs ( $expand)

Whether/how to expand URLs.

Parameters
string | bool | int$expandA PROTO_* constant or false for no expansion
See also
UrlUtils::expand()

Definition at line 140 of file LinkRenderer.php.

◆ setForceArticlePath()

MediaWiki\Linker\LinkRenderer::setForceArticlePath ( $force)

Whether to force the link to use the article path ($wgArticlePath) even if a query string is present, resulting in URLs like /wiki/Main_Page?action=foobar.

Parameters
bool$force

Definition at line 122 of file LinkRenderer.php.

Member Data Documentation

◆ CONSTRUCTOR_OPTIONS

const MediaWiki\Linker\LinkRenderer::CONSTRUCTOR_OPTIONS
Initial value:
= [
]
const NoFollowDomainExceptions
Name constant for the NoFollowDomainExceptions setting, for use with Config::get()
const NoFollowNsExceptions
Name constant for the NoFollowNsExceptions setting, for use with Config::get()
const NoFollowLinks
Name constant for the NoFollowLinks setting, for use with Config::get()

Definition at line 43 of file LinkRenderer.php.


The documentation for this class was generated from the following file: