MediaWiki REL1_39
UserGroupMembership.php
Go to the documentation of this file.
1<?php
25
36
38 private $userId;
39
41 private $group;
42
44 private $expiry;
45
47 private $expired;
48
54 public function __construct( int $userId = 0, ?string $group = null, ?string $expiry = null ) {
55 $this->userId = $userId;
56 $this->group = $group;
57 $this->expiry = $expiry ?: null;
58 $this->expired = $expiry ? wfTimestampNow() > $expiry : false;
59 }
60
64 public function getUserId() {
65 return $this->userId;
66 }
67
71 public function getGroup() {
72 return $this->group;
73 }
74
78 public function getExpiry() {
79 return $this->expiry;
80 }
81
87 public function isExpired() {
88 return $this->expired;
89 }
90
104 public static function getLink( $ugm, IContextSource $context, $format, $userName = null ) {
105 if ( $format !== 'wiki' && $format !== 'html' ) {
106 throw new MWException( 'UserGroupMembership::getLink() $format parameter should be ' .
107 "'wiki' or 'html'" );
108 }
109
110 if ( $ugm instanceof UserGroupMembership ) {
111 $expiry = $ugm->getExpiry();
112 $group = $ugm->getGroup();
113 } else {
114 $expiry = null;
115 $group = $ugm;
116 }
117
118 if ( $userName !== null ) {
119 $groupName = self::getGroupMemberName( $group, $userName );
120 } else {
121 $groupName = self::getGroupName( $group );
122 }
123
124 // link to the group description page, if it exists
125 $linkTitle = self::getGroupPage( $group );
126 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
127 if ( $format === 'wiki' ) {
128 if ( $linkTitle ) {
129 $linkPage = $linkTitle->getFullText();
130 $groupLink = "[[$linkPage|$groupName]]";
131 } else {
132 $groupLink = $groupName;
133 }
134 } else {
135 if ( $linkTitle ) {
136 $groupLink = $linkRenderer->makeLink( $linkTitle, $groupName );
137 } else {
138 $groupLink = htmlspecialchars( $groupName );
139 }
140 }
141
142 if ( $expiry ) {
143 // format the expiry to a nice string
144 $uiLanguage = $context->getLanguage();
145 $uiUser = $context->getUser();
146 $expiryDT = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
147 $expiryD = $uiLanguage->userDate( $expiry, $uiUser );
148 $expiryT = $uiLanguage->userTime( $expiry, $uiUser );
149
150 if ( $format === 'wiki' ) {
151 return $context->msg( 'group-membership-link-with-expiry' )
152 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->text();
153 } else {
154 $groupLink = Message::rawParam( $groupLink );
155 return $context->msg( 'group-membership-link-with-expiry' )
156 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->escaped();
157 }
158 }
159 return $groupLink;
160 }
161
170 public static function getGroupName( $group ) {
171 return RequestContext::getMain()->getLanguage()->getGroupName( $group );
172 }
173
182 public static function getGroupMemberName( $group, $member ) {
183 return RequestContext::getMain()->getLanguage()->getGroupMemberName( $group, $member );
184 }
185
193 public static function getGroupPage( $group ) {
194 $msg = wfMessage( "grouppage-$group" )->inContentLanguage();
195 if ( $msg->exists() ) {
196 $title = Title::newFromText( $msg->text() );
197 if ( is_object( $title ) ) {
198 return $title;
199 }
200 }
201 return false;
202 }
203
212 public function equals( UserGroupMembership $ugm ) {
213 return (
214 $ugm->getUserId() === $this->userId
215 && $ugm->getGroup() === $this->group
216 );
217 }
218
219}
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
MediaWiki exception.
Service locator for MediaWiki core services.
static rawParam( $raw)
Definition Message.php:1134
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 getLink( $ugm, IContextSource $context, $format, $userName=null)
Gets a link for a user group, possibly including the expiry date if relevant.
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
static getGroupMemberName( $group, $member)
Gets the localized name for a member of a group, if it exists.
isExpired()
Has the membership expired?
equals(UserGroupMembership $ugm)
Compares two pure value objects.
Interface for objects which can provide a MediaWiki context on request.
Interface for objects representing user identity.
msg( $key,... $params)
This is the method for getting translated interface messages.