MediaWiki REL1_37
UserGroupMembership.php
Go to the documentation of this file.
1<?php
24
35
37 private $userId;
38
40 private $group;
41
43 private $expiry;
44
46 private $expired;
47
53 public function __construct( int $userId = 0, ?string $group = null, ?string $expiry = null ) {
54 $this->userId = $userId;
55 $this->group = $group;
56 $this->expiry = $expiry ?: null;
57 $this->expired = $expiry ? wfTimestampNow() > $expiry : false;
58 }
59
63 public function getUserId() {
64 return $this->userId;
65 }
66
70 public function getGroup() {
71 return $this->group;
72 }
73
77 public function getExpiry() {
78 return $this->expiry;
79 }
80
86 public function isExpired() {
87 return $this->expired;
88 }
89
103 public static function getLink( $ugm, IContextSource $context, $format, $userName = null ) {
104 if ( $format !== 'wiki' && $format !== 'html' ) {
105 throw new MWException( 'UserGroupMembership::getLink() $format parameter should be ' .
106 "'wiki' or 'html'" );
107 }
108
109 if ( $ugm instanceof UserGroupMembership ) {
110 $expiry = $ugm->getExpiry();
111 $group = $ugm->getGroup();
112 } else {
113 $expiry = null;
114 $group = $ugm;
115 }
116
117 if ( $userName !== null ) {
118 $groupName = self::getGroupMemberName( $group, $userName );
119 } else {
120 $groupName = self::getGroupName( $group );
121 }
122
123 // link to the group description page, if it exists
124 $linkTitle = self::getGroupPage( $group );
125 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
126 if ( $format === 'wiki' ) {
127 if ( $linkTitle ) {
128 $linkPage = $linkTitle->getFullText();
129 $groupLink = "[[$linkPage|$groupName]]";
130 } else {
131 $groupLink = $groupName;
132 }
133 } else {
134 if ( $linkTitle ) {
135 $groupLink = $linkRenderer->makeLink( $linkTitle, $groupName );
136 } else {
137 $groupLink = htmlspecialchars( $groupName );
138 }
139 }
140
141 if ( $expiry ) {
142 // format the expiry to a nice string
143 $uiLanguage = $context->getLanguage();
144 $uiUser = $context->getUser();
145 $expiryDT = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
146 $expiryD = $uiLanguage->userDate( $expiry, $uiUser );
147 $expiryT = $uiLanguage->userTime( $expiry, $uiUser );
148
149 if ( $format === 'wiki' ) {
150 return $context->msg( 'group-membership-link-with-expiry' )
151 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->text();
152 } else {
153 $groupLink = Message::rawParam( $groupLink );
154 return $context->msg( 'group-membership-link-with-expiry' )
155 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->escaped();
156 }
157 }
158 return $groupLink;
159 }
160
168 public static function getGroupName( $group ) {
169 $msg = wfMessage( "group-$group" );
170 return $msg->isBlank() ? $group : $msg->text();
171 }
172
181 public static function getGroupMemberName( $group, $username ) {
182 $msg = wfMessage( "group-$group-member", $username );
183 return $msg->isBlank() ? $group : $msg->text();
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static rawParam( $raw)
Definition Message.php:1090
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.
int $userId
The ID of the user who belongs to the group.
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
isExpired()
Has the membership expired?
static getGroupMemberName( $group, $username)
Gets the localized name for a member of a group, if it exists.
string null $expiry
Timestamp of expiry in TS_MW format, or null if no expiry.
equals(UserGroupMembership $ugm)
Compares two pure value objects.
bool $expired
Expiration flag.
Interface for objects which can provide a MediaWiki context on request.
msg( $key,... $params)
This is the method for getting translated interface messages.