MediaWiki  master
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 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:395
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:202
UserGroupMembership\getExpiry
getExpiry()
Definition: UserGroupMembership.php:78
UserGroupMembership\getGroupName
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
Definition: UserGroupMembership.php:170
UserGroupMembership\$expiry
string null $expiry
Timestamp of expiry in TS_MW format, or null if no expiry.
Definition: UserGroupMembership.php:44
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1183
UserGroupMembership\getGroup
getGroup()
Definition: UserGroupMembership.php:71
UserGroupMembership\equals
equals(UserGroupMembership $ugm)
Compares two pure value objects.
Definition: UserGroupMembership.php:212
UserGroupMembership\$expired
bool $expired
Expiration flag.
Definition: UserGroupMembership.php:47
UserGroupMembership\getGroupPage
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
Definition: UserGroupMembership.php:193
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
UserGroupMembership\getUserId
getUserId()
Definition: UserGroupMembership.php:64
MWException
MediaWiki exception.
Definition: MWException.php:29
MessageLocalizer\msg
msg( $key,... $params)
This is the method for getting translated interface messages.
UserGroupMembership\getLink
static getLink( $ugm, IContextSource $context, $format, $userName=null)
Gets a link for a user group, possibly including the expiry date if relevant.
Definition: UserGroupMembership.php:104
UserGroupMembership\__construct
__construct(int $userId=0, ?string $group=null, ?string $expiry=null)
Definition: UserGroupMembership.php:54
$title
$title
Definition: testCompression.php:38
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1694
UserGroupMembership\isExpired
isExpired()
Has the membership expired?
Definition: UserGroupMembership.php:87
UserGroupMembership\$userId
int $userId
The ID of the user who belongs to the group.
Definition: UserGroupMembership.php:38
Message\rawParam
static rawParam( $raw)
Definition: Message.php:1116
IContextSource\getUser
getUser()
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:484
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
UserGroupMembership\getGroupMemberName
static getGroupMemberName( $group, $member)
Gets the localized name for a member of a group, if it exists.
Definition: UserGroupMembership.php:182
UserGroupMembership\$group
string $group
Definition: UserGroupMembership.php:41
UserGroupMembership
Represents a "user group membership" – a specific instance of a user belonging to a group.
Definition: UserGroupMembership.php:35
IContextSource\getLanguage
getLanguage()