MediaWiki 1.44.0
UserLinkRenderer.php
Go to the documentation of this file.
1<?php
2declare( strict_types=1 );
3
4namespace MediaWiki\Linker;
5
16use Wikimedia\IPUtils;
18
25 private TempUserConfig $tempUserConfig;
26 private SpecialPageFactory $specialPageFactory;
27 private LinkRenderer $linkRenderer;
28 private TempUserDetailsLookup $tempUserDetailsLookup;
29
36 private MapCacheLRU $userLinkCache;
37
38 public function __construct(
39 TempUserConfig $tempUserConfig,
40 SpecialPageFactory $specialPageFactory,
41 LinkRenderer $linkRenderer,
42 TempUserDetailsLookup $tempUserDetailsLookup
43 ) {
44 $this->tempUserConfig = $tempUserConfig;
45 $this->specialPageFactory = $specialPageFactory;
46 $this->linkRenderer = $linkRenderer;
47 $this->tempUserDetailsLookup = $tempUserDetailsLookup;
48
49 // Set a large enough cache size to accommodate long pagers,
50 // such as Special:RecentChanges with a high limit.
51 $this->userLinkCache = new MapCacheLRU( 1_000 );
52 }
53
65 public function userLink(
66 UserIdentity $targetUser,
67 IContextSource $context,
68 ?string $altUserName = null,
69 array $attributes = []
70 ): string {
71 $outputPage = $context->getOutput();
72 $outputPage->addModuleStyles( [
73 'mediawiki.interface.helpers.styles',
74 'mediawiki.interface.helpers.linker.styles'
75 ] );
76
77 return $this->userLinkCache->getWithSetCallback(
78 $this->userLinkCache->makeKey(
79 $targetUser->getName(),
80 $altUserName ?? '',
81 implode( ' ', $attributes )
82 ),
83 fn () => $this->renderUserLink( $targetUser, $context, $altUserName, $attributes )
84 );
85 }
86
98 private function renderUserLink(
99 UserIdentity $targetUser,
100 MessageLocalizer $messageLocalizer,
101 ?string $altUserName = null,
102 array $attributes = []
103 ): string {
104 $userName = $targetUser->getName();
105
106 $classes = [ 'mw-userlink' ];
107
108 $postfix = '';
109
110 if ( $this->tempUserConfig->isTempName( $userName ) ) {
111 $classes[] = 'mw-tempuserlink';
112 $attributes['data-mw-target'] = $userName;
113
114 // Adjust the styling of expired temporary account links (T358469).
115 if ( $this->tempUserDetailsLookup->isExpired( $targetUser ) ) {
116 $classes[] = 'mw-tempuserlink-expired';
117
118 $description = $messageLocalizer->msg( 'tempuser-expired-link-tooltip' )->text();
119
120 $postfix = Html::element(
121 'span',
122 [
123 'role' => 'presentation',
124 'class' => 'cdx-tooltip mw-tempuserlink-expired--tooltip',
125 ],
126 $description
127 );
128
129 $attributes['aria-description'] = $description;
130
131 // Hide default link title when rendering expired temporary account links
132 // to avoid conflicting with the tooltip.
133 $attributes['title'] = null;
134 }
135
136 $pageName = $this->specialPageFactory->getLocalNameFor( 'Contributions', $userName );
137 $page = new TitleValue( NS_SPECIAL, $pageName );
138 } elseif ( !$targetUser->isRegistered() ) {
139 $page = ExternalUserNames::getUserLinkTitle( $userName );
140
141 if ( ExternalUserNames::isExternal( $userName ) ) {
142 $classes[] = 'mw-extuserlink';
143 } elseif ( $altUserName === null ) {
144 $altUserName = IPUtils::prettifyIP( $userName );
145 }
146 $classes[] = 'mw-anonuserlink'; // Separate link class for anons (T45179)
147 } else {
148 $page = TitleValue::tryNew( NS_USER, strtr( $userName, ' ', '_' ) );
149 }
150
151 // Wrap the output with <bdi> tags for directionality isolation
152 $linkText =
153 '<bdi>' . htmlspecialchars( $altUserName ?? $userName ) . '</bdi>'
154 . $postfix;
155
156 if ( isset( $attributes['class'] ) ) {
157 $classes[] = $attributes['class'];
158 }
159
160 $attributes['class'] = $classes;
161
162 if ( $page !== null ) {
163 return $this->linkRenderer->makeLink( $page, new HtmlArmor( $linkText ), $attributes );
164 }
165
166 return Html::rawElement( 'span', $attributes, $linkText );
167 }
168}
const NS_USER
Definition Defines.php:67
const NS_SPECIAL
Definition Defines.php:54
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
Class that generates HTML for internal links.
Service class that renders HTML for user-related links.
userLink(UserIdentity $targetUser, IContextSource $context, ?string $altUserName=null, array $attributes=[])
Render a user page link (or user contributions for anonymous and temporary users).
__construct(TempUserConfig $tempUserConfig, SpecialPageFactory $specialPageFactory, LinkRenderer $linkRenderer, TempUserDetailsLookup $tempUserDetailsLookup)
Factory for handling the special page list and generating SpecialPage objects.
Represents the target of a wiki link.
Class to parse and build external user names.
Caching lookup service for metadata related to temporary accounts, such as expiration.
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:32
Store key-value entries in a size-limited in-memory LRU cache.
Interface for objects which can provide a MediaWiki context on request.
Interface for temporary user creation config and name matching.
Interface for objects representing user identity.
isRegistered()
This must be equivalent to getId() != 0 and is provided for code readability.
Interface for localizing messages in MediaWiki.
msg( $key,... $params)
This is the method for getting translated interface messages.
element(SerializerNode $parent, SerializerNode $node, $contents)