MediaWiki master
UserGroupMembership.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\User;
24
25use InvalidArgumentException;
30
41
43 private $userId;
44
46 private $group;
47
49 private $expiry;
50
52 private $expired;
53
59 public function __construct( int $userId = 0, ?string $group = null, ?string $expiry = null ) {
60 $this->userId = $userId;
61 $this->group = $group;
62 $this->expiry = $expiry ?: null;
63 $this->expired = $expiry && wfTimestampNow() > $expiry;
64 }
65
69 public function getUserId() {
70 return $this->userId;
71 }
72
76 public function getGroup() {
77 return $this->group;
78 }
79
83 public function getExpiry() {
84 return $this->expiry;
85 }
86
92 public function isExpired() {
93 return $this->expired;
94 }
95
111 public static function getLink( $ugm, IContextSource $context, string $format, $userName = null ) {
112 switch ( $format ) {
113 case 'wiki':
114 return self::getLinkWiki( $ugm, $context, $userName );
115 case 'html':
116 return self::getLinkHTML( $ugm, $context, $userName );
117 default:
118 throw new InvalidArgumentException( 'UserGroupMembership::getLink() $format parameter should be ' .
119 "'wiki' or 'html'" );
120 }
121 }
122
136 public static function getLinkHTML( $ugm, IContextSource $context, $userName = null ): string {
137 [
138 'expiry' => $expiry,
139 'linkTitle' => $linkTitle,
140 'groupName' => $groupName
141 ] = self::getLinkInfo( $ugm, $context, $userName );
142
143 // link to the group description page, if it exists
144 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
145 if ( $linkTitle ) {
146 $groupLink = $linkRenderer->makeLink( $linkTitle, $groupName );
147 } else {
148 $groupLink = htmlspecialchars( $groupName );
149 }
150
151 if ( $expiry ) {
152 [
153 'expiryDT' => $expiryDT,
154 'expiryD' => $expiryD,
155 'expiryT' => $expiryT
156 ] = self::getLinkExpiryParams( $context, $expiry );
157 $groupLink = Message::rawParam( $groupLink );
158 return $context->msg( 'group-membership-link-with-expiry' )
159 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->escaped();
160 }
161 return $groupLink;
162 }
163
177 public static function getLinkWiki( $ugm, IContextSource $context, $userName = null ): string {
178 [
179 'expiry' => $expiry,
180 'linkTitle' => $linkTitle,
181 'groupName' => $groupName
182 ] = self::getLinkInfo( $ugm, $context, $userName );
183
184 // link to the group description page, if it exists
185 if ( $linkTitle ) {
186 $linkPage = $linkTitle->getFullText();
187 $groupLink = "[[$linkPage|$groupName]]";
188 } else {
189 $groupLink = $groupName;
190 }
191
192 if ( $expiry ) {
193 [
194 'expiryDT' => $expiryDT,
195 'expiryD' => $expiryD,
196 'expiryT' => $expiryT
197 ] = self::getLinkExpiryParams( $context, $expiry );
198 return $context->msg( 'group-membership-link-with-expiry' )
199 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->text();
200 }
201 return $groupLink;
202 }
203
210 private static function getLinkInfo( $ugm, $context, $userName = null ): array {
211 if ( $ugm instanceof UserGroupMembership ) {
212 $expiry = $ugm->getExpiry();
213 $group = $ugm->getGroup();
214 } else {
215 $expiry = null;
216 $group = $ugm;
217 }
218
219 $uiLanguage = $context->getLanguage();
220 if ( $userName !== null ) {
221 $groupName = $uiLanguage->getGroupMemberName( $group, $userName );
222 } else {
223 $groupName = $uiLanguage->getGroupName( $group );
224 }
225 $linkTitle = self::getGroupPage( $group );
226 return [ 'expiry' => $expiry, 'linkTitle' => $linkTitle, 'groupName' => $groupName ];
227 }
228
234 private static function getLinkExpiryParams( IContextSource $context, string $expiry ): array {
235 // format the expiry to a nice string
236 $uiLanguage = $context->getLanguage();
237 $uiUser = $context->getUser();
238 $expiryDT = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
239 $expiryD = $uiLanguage->userDate( $expiry, $uiUser );
240 $expiryT = $uiLanguage->userTime( $expiry, $uiUser );
241 return [ 'expiryDT' => $expiryDT, 'expiryD' => $expiryD, 'expiryT' => $expiryT ];
242 }
243
251 public static function getGroupPage( $group ) {
252 $msg = wfMessage( "grouppage-$group" )->inContentLanguage();
253 if ( $msg->exists() ) {
254 $title = Title::newFromText( $msg->text() );
255 if ( is_object( $title ) ) {
256 return $title;
257 }
258 }
259 return false;
260 }
261
270 public function equals( UserGroupMembership $ugm ) {
271 return (
272 $ugm->getUserId() === $this->userId
273 && $ugm->getGroup() === $this->group
274 );
275 }
276
277}
278
280class_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:81
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:158
Represents a title within MediaWiki.
Definition Title.php:78
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 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.
Utility class for bot passwords.