MediaWiki master
UserGroupMembership.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\User;
8
9use InvalidArgumentException;
14
27
29 private $userId;
30
32 private $group;
33
35 private $expiry;
36
38 private $expired;
39
45 public function __construct( int $userId = 0, ?string $group = null, ?string $expiry = null ) {
46 $this->userId = $userId;
47 $this->group = $group;
48 $this->expiry = $expiry ?: null;
49 $this->expired = $expiry && wfTimestampNow() > $expiry;
50 }
51
55 public function getUserId() {
56 return $this->userId;
57 }
58
62 public function getGroup() {
63 return $this->group;
64 }
65
69 public function getExpiry() {
70 return $this->expiry;
71 }
72
78 public function isExpired() {
79 return $this->expired;
80 }
81
97 public static function getLink( $ugm, IContextSource $context, string $format, $userName = null ) {
98 switch ( $format ) {
99 case 'wiki':
100 return self::getLinkWiki( $ugm, $context, $userName );
101 case 'html':
102 return self::getLinkHTML( $ugm, $context, $userName );
103 default:
104 throw new InvalidArgumentException( 'UserGroupMembership::getLink() $format parameter should be ' .
105 "'wiki' or 'html'" );
106 }
107 }
108
122 public static function getLinkHTML( $ugm, IContextSource $context, $userName = null ): string {
123 [
124 'expiry' => $expiry,
125 'linkTitle' => $linkTitle,
126 'groupName' => $groupName
127 ] = self::getLinkInfo( $ugm, $context, $userName );
128
129 // link to the group description page, if it exists
130 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
131 if ( $linkTitle ) {
132 $groupLink = $linkRenderer->makeLink( $linkTitle, $groupName );
133 } else {
134 $groupLink = htmlspecialchars( $groupName );
135 }
136
137 if ( $expiry ) {
138 [
139 'expiryDT' => $expiryDT,
140 'expiryD' => $expiryD,
141 'expiryT' => $expiryT
142 ] = self::getLinkExpiryParams( $context, $expiry );
143 $groupLink = Message::rawParam( $groupLink );
144 return $context->msg( 'group-membership-link-with-expiry' )
145 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->escaped();
146 }
147 return $groupLink;
148 }
149
163 public static function getLinkWiki( $ugm, IContextSource $context, $userName = null ): string {
164 [
165 'expiry' => $expiry,
166 'linkTitle' => $linkTitle,
167 'groupName' => $groupName
168 ] = self::getLinkInfo( $ugm, $context, $userName );
169
170 // link to the group description page, if it exists
171 if ( $linkTitle ) {
172 $linkPage = $linkTitle->getFullText();
173 $groupLink = "[[$linkPage|$groupName]]";
174 } else {
175 $groupLink = $groupName;
176 }
177
178 if ( $expiry ) {
179 [
180 'expiryDT' => $expiryDT,
181 'expiryD' => $expiryD,
182 'expiryT' => $expiryT
183 ] = self::getLinkExpiryParams( $context, $expiry );
184 return $context->msg( 'group-membership-link-with-expiry' )
185 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->text();
186 }
187 return $groupLink;
188 }
189
196 private static function getLinkInfo( $ugm, $context, $userName = null ): array {
197 if ( $ugm instanceof UserGroupMembership ) {
198 $expiry = $ugm->getExpiry();
199 $group = $ugm->getGroup();
200 } else {
201 $expiry = null;
202 $group = $ugm;
203 }
204
205 $uiLanguage = $context->getLanguage();
206 if ( $userName !== null ) {
207 $groupName = $uiLanguage->getGroupMemberName( $group, $userName );
208 } else {
209 $groupName = $uiLanguage->getGroupName( $group );
210 }
211 $linkTitle = self::getGroupPage( $group );
212 return [ 'expiry' => $expiry, 'linkTitle' => $linkTitle, 'groupName' => $groupName ];
213 }
214
220 private static function getLinkExpiryParams( IContextSource $context, string $expiry ): array {
221 // format the expiry to a nice string
222 $uiLanguage = $context->getLanguage();
223 $uiUser = $context->getUser();
224 $expiryDT = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
225 $expiryD = $uiLanguage->userDate( $expiry, $uiUser );
226 $expiryT = $uiLanguage->userTime( $expiry, $uiUser );
227 return [ 'expiryDT' => $expiryDT, 'expiryD' => $expiryD, 'expiryT' => $expiryT ];
228 }
229
237 public static function getGroupPage( $group ) {
238 $msg = wfMessage( "grouppage-$group" )->inContentLanguage();
239 if ( $msg->exists() ) {
240 $title = Title::newFromText( $msg->text() );
241 if ( $title ) {
242 return $title;
243 }
244 }
245 return false;
246 }
247
256 public function equals( UserGroupMembership $ugm ) {
257 return (
258 $ugm->getUserId() === $this->userId
259 && $ugm->getGroup() === $this->group
260 );
261 }
262
263}
264
266class_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:68
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.
__construct(int $userId=0, ?string $group=null, ?string $expiry=null)
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.
Interface for objects which can provide a MediaWiki context on request.
msg( $key,... $params)
This is the method for getting translated interface messages.