MediaWiki master
UserGroupMembership.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\User;
24
26use InvalidArgumentException;
29use Message;
31
42
44 private $userId;
45
47 private $group;
48
50 private $expiry;
51
53 private $expired;
54
60 public function __construct( int $userId = 0, ?string $group = null, ?string $expiry = null ) {
61 $this->userId = $userId;
62 $this->group = $group;
63 $this->expiry = $expiry ?: null;
64 $this->expired = $expiry && wfTimestampNow() > $expiry;
65 }
66
70 public function getUserId() {
71 return $this->userId;
72 }
73
77 public function getGroup() {
78 return $this->group;
79 }
80
84 public function getExpiry() {
85 return $this->expiry;
86 }
87
93 public function isExpired() {
94 return $this->expired;
95 }
96
112 public static function getLink( $ugm, IContextSource $context, string $format, $userName = null ) {
113 switch ( $format ) {
114 case 'wiki':
115 return self::getLinkWiki( $ugm, $context, $userName );
116 case 'html':
117 return self::getLinkHTML( $ugm, $context, $userName );
118 default:
119 throw new InvalidArgumentException( 'UserGroupMembership::getLink() $format parameter should be ' .
120 "'wiki' or 'html'" );
121 }
122 }
123
137 public static function getLinkHTML( $ugm, IContextSource $context, $userName = null ): string {
138 [
139 'expiry' => $expiry,
140 'linkTitle' => $linkTitle,
141 'groupName' => $groupName
142 ] = self::getLinkInfo( $ugm, $context, $userName );
143
144 // link to the group description page, if it exists
145 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
146 if ( $linkTitle ) {
147 $groupLink = $linkRenderer->makeLink( $linkTitle, $groupName );
148 } else {
149 $groupLink = htmlspecialchars( $groupName );
150 }
151
152 if ( $expiry ) {
153 [
154 'expiryDT' => $expiryDT,
155 'expiryD' => $expiryD,
156 'expiryT' => $expiryT
157 ] = self::getLinkExpiryParams( $context, $expiry );
158 $groupLink = Message::rawParam( $groupLink );
159 return $context->msg( 'group-membership-link-with-expiry' )
160 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->escaped();
161 }
162 return $groupLink;
163 }
164
178 public static function getLinkWiki( $ugm, IContextSource $context, $userName = null ): string {
179 [
180 'expiry' => $expiry,
181 'linkTitle' => $linkTitle,
182 'groupName' => $groupName
183 ] = self::getLinkInfo( $ugm, $context, $userName );
184
185 // link to the group description page, if it exists
186 if ( $linkTitle ) {
187 $linkPage = $linkTitle->getFullText();
188 $groupLink = "[[$linkPage|$groupName]]";
189 } else {
190 $groupLink = $groupName;
191 }
192
193 if ( $expiry ) {
194 [
195 'expiryDT' => $expiryDT,
196 'expiryD' => $expiryD,
197 'expiryT' => $expiryT
198 ] = self::getLinkExpiryParams( $context, $expiry );
199 return $context->msg( 'group-membership-link-with-expiry' )
200 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->text();
201 }
202 return $groupLink;
203 }
204
211 private static function getLinkInfo( $ugm, $context, $userName = null ): array {
212 if ( $ugm instanceof UserGroupMembership ) {
213 $expiry = $ugm->getExpiry();
214 $group = $ugm->getGroup();
215 } else {
216 $expiry = null;
217 $group = $ugm;
218 }
219
220 $uiLanguage = $context->getLanguage();
221 if ( $userName !== null ) {
222 $groupName = $uiLanguage->getGroupMemberName( $group, $userName );
223 } else {
224 $groupName = $uiLanguage->getGroupName( $group );
225 }
226 $linkTitle = self::getGroupPage( $group );
227 return [ 'expiry' => $expiry, 'linkTitle' => $linkTitle, 'groupName' => $groupName ];
228 }
229
235 private static function getLinkExpiryParams( IContextSource $context, string $expiry ): array {
236 // format the expiry to a nice string
237 $uiLanguage = $context->getLanguage();
238 $uiUser = $context->getUser();
239 $expiryDT = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
240 $expiryD = $uiLanguage->userDate( $expiry, $uiUser );
241 $expiryT = $uiLanguage->userTime( $expiry, $uiUser );
242 return [ 'expiryDT' => $expiryDT, 'expiryD' => $expiryD, 'expiryT' => $expiryT ];
243 }
244
253 public static function getGroupName( $group ) {
254 wfDeprecated( __METHOD__, '1.41' );
255 return RequestContext::getMain()->getLanguage()->getGroupName( $group );
256 }
257
268 public static function getGroupMemberName( $group, $member ) {
269 wfDeprecated( __METHOD__, '1.41' );
270 return RequestContext::getMain()->getLanguage()->getGroupMemberName( $group, $member );
271 }
272
280 public static function getGroupPage( $group ) {
281 $msg = wfMessage( "grouppage-$group" )->inContentLanguage();
282 if ( $msg->exists() ) {
283 $title = Title::newFromText( $msg->text() );
284 if ( is_object( $title ) ) {
285 return $title;
286 }
287 }
288 return false;
289 }
290
299 public function equals( UserGroupMembership $ugm ) {
300 return (
301 $ugm->getUserId() === $this->userId
302 && $ugm->getGroup() === $this->group
303 );
304 }
305
306}
307
312class_alias( UserGroupMembership::class, 'UserGroupMembership' );
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:88
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Represents a title within MediaWiki.
Definition Title.php:79
Represents a "user group membership" – a specific instance of a user belonging to a group.
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
__construct(int $userId=0, ?string $group=null, ?string $expiry=null)
static getGroupMemberName( $group, $member)
Gets the localized name for a member of a group, if it exists.
static getLinkWiki( $ugm, IContextSource $context, $userName=null)
Gets a link for a user group, possibly including the expiry date if relevant.
isExpired()
Has the membership expired?
equals(UserGroupMembership $ugm)
Compares two pure value objects.
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
static getLink( $ugm, IContextSource $context, string $format, $userName=null)
Gets a link for a user group, possibly including the expiry date if relevant.
static getLinkHTML( $ugm, IContextSource $context, $userName=null)
Gets a link for a user group, possibly including the expiry date if relevant.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
static rawParam( $raw)
Definition Message.php:1143
Group all the pieces relevant to the context of a request into one instance.
Interface for objects which can provide a MediaWiki context on request.
msg( $key,... $params)
This is the method for getting translated interface messages.
Utility class for bot passwords.