MediaWiki master
UserGroupMembership.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\User;
8
9use InvalidArgumentException;
14
27
29 private readonly ?string $expiry;
30
32 private readonly bool $expired;
33
39 public function __construct(
40 private readonly int $userId = 0,
41 private readonly ?string $group = null,
42 ?string $expiry = null,
43 ) {
44 $this->expiry = $expiry ?: null;
45 $this->expired = $expiry && wfTimestampNow() > $expiry;
46 }
47
51 public function getUserId() {
52 return $this->userId;
53 }
54
58 public function getGroup() {
59 return $this->group;
60 }
61
65 public function getExpiry() {
66 return $this->expiry;
67 }
68
74 public function isExpired() {
75 return $this->expired;
76 }
77
93 public static function getLink( $ugm, IContextSource $context, string $format, $userName = null ) {
94 switch ( $format ) {
95 case 'wiki':
96 return self::getLinkWiki( $ugm, $context, $userName );
97 case 'html':
98 return self::getLinkHTML( $ugm, $context, $userName );
99 default:
100 throw new InvalidArgumentException( 'UserGroupMembership::getLink() $format parameter should be ' .
101 "'wiki' or 'html'" );
102 }
103 }
104
118 public static function getLinkHTML( $ugm, IContextSource $context, $userName = null ): string {
119 [
120 'expiry' => $expiry,
121 'linkTitle' => $linkTitle,
122 'groupName' => $groupName
123 ] = self::getLinkInfo( $ugm, $context, $userName );
124
125 // link to the group description page, if it exists
126 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
127 if ( $linkTitle ) {
128 $groupLink = $linkRenderer->makeLink( $linkTitle, $groupName );
129 } else {
130 $groupLink = htmlspecialchars( $groupName );
131 }
132
133 if ( $expiry ) {
134 [
135 'expiryDT' => $expiryDT,
136 'expiryD' => $expiryD,
137 'expiryT' => $expiryT
138 ] = self::getLinkExpiryParams( $context, $expiry );
139 $groupLink = Message::rawParam( $groupLink );
140 return $context->msg( 'group-membership-link-with-expiry' )
141 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->escaped();
142 }
143 return $groupLink;
144 }
145
159 public static function getLinkWiki( $ugm, IContextSource $context, $userName = null ): string {
160 [
161 'expiry' => $expiry,
162 'linkTitle' => $linkTitle,
163 'groupName' => $groupName
164 ] = self::getLinkInfo( $ugm, $context, $userName );
165
166 // link to the group description page, if it exists
167 if ( $linkTitle ) {
168 $linkPage = $linkTitle->getFullText();
169 $groupLink = "[[$linkPage|$groupName]]";
170 } else {
171 $groupLink = $groupName;
172 }
173
174 if ( $expiry ) {
175 [
176 'expiryDT' => $expiryDT,
177 'expiryD' => $expiryD,
178 'expiryT' => $expiryT
179 ] = self::getLinkExpiryParams( $context, $expiry );
180 return $context->msg( 'group-membership-link-with-expiry' )
181 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->text();
182 }
183 return $groupLink;
184 }
185
192 private static function getLinkInfo( $ugm, $context, $userName = null ): array {
193 if ( $ugm instanceof UserGroupMembership ) {
194 $expiry = $ugm->getExpiry();
195 $group = $ugm->getGroup();
196 } else {
197 $expiry = null;
198 $group = $ugm;
199 }
200
201 $uiLanguage = $context->getLanguage();
202 if ( $userName !== null ) {
203 $groupName = $uiLanguage->getGroupMemberName( $group, $userName );
204 } else {
205 $groupName = $uiLanguage->getGroupName( $group );
206 }
207 $linkTitle = self::getGroupPage( $group );
208 return [ 'expiry' => $expiry, 'linkTitle' => $linkTitle, 'groupName' => $groupName ];
209 }
210
216 private static function getLinkExpiryParams( IContextSource $context, string $expiry ): array {
217 // format the expiry to a nice string
218 $uiLanguage = $context->getLanguage();
219 $uiUser = $context->getUser();
220 $expiryDT = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
221 $expiryD = $uiLanguage->userDate( $expiry, $uiUser );
222 $expiryT = $uiLanguage->userTime( $expiry, $uiUser );
223 return [ 'expiryDT' => $expiryDT, 'expiryD' => $expiryD, 'expiryT' => $expiryT ];
224 }
225
233 public static function getGroupPage( $group ) {
234 $msg = wfMessage( "grouppage-$group" )->inContentLanguage();
235 if ( $msg->exists() ) {
236 $title = Title::newFromText( $msg->text() );
237 if ( $title ) {
238 return $title;
239 }
240 }
241 return false;
242 }
243
252 public function equals( UserGroupMembership $ugm ) {
253 return (
254 $ugm->getUserId() === $this->userId
255 && $ugm->getGroup() === $this->group
256 );
257 }
258
259}
260
262class_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.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
Represents a title within MediaWiki.
Definition Title.php:69
Represents the membership of one user in one user group.
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
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 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.
__construct(private readonly int $userId=0, private readonly ?string $group=null, ?string $expiry=null,)
Interface for objects which can provide a MediaWiki context on request.
msg( $key,... $params)
This is the method for getting translated interface messages.