MediaWiki  master
UserGroupMembership.php
Go to the documentation of this file.
1 <?php
26 
37 
39  private $userId;
40 
42  private $group;
43 
45  private $expiry;
46 
48  private $expired;
49 
55  public function __construct( int $userId = 0, ?string $group = null, ?string $expiry = null ) {
56  $this->userId = $userId;
57  $this->group = $group;
58  $this->expiry = $expiry ?: null;
59  $this->expired = $expiry ? wfTimestampNow() > $expiry : false;
60  }
61 
65  public function getUserId() {
66  return $this->userId;
67  }
68 
72  public function getGroup() {
73  return $this->group;
74  }
75 
79  public function getExpiry() {
80  return $this->expiry;
81  }
82 
88  public function isExpired() {
89  return $this->expired;
90  }
91 
105  public static function getLink( $ugm, IContextSource $context, $format, $userName = null ) {
106  if ( $format !== 'wiki' && $format !== 'html' ) {
107  throw new InvalidArgumentException( 'UserGroupMembership::getLink() $format parameter should be ' .
108  "'wiki' or 'html'" );
109  }
110 
111  if ( $ugm instanceof UserGroupMembership ) {
112  $expiry = $ugm->getExpiry();
113  $group = $ugm->getGroup();
114  } else {
115  $expiry = null;
116  $group = $ugm;
117  }
118 
119  $uiLanguage = $context->getLanguage();
120  if ( $userName !== null ) {
121  $groupName = $uiLanguage->getGroupMemberName( $group, $userName );
122  } else {
123  $groupName = $uiLanguage->getGroupName( $group );
124  }
125 
126  // link to the group description page, if it exists
127  $linkTitle = self::getGroupPage( $group );
128  $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
129  if ( $format === 'wiki' ) {
130  if ( $linkTitle ) {
131  $linkPage = $linkTitle->getFullText();
132  $groupLink = "[[$linkPage|$groupName]]";
133  } else {
134  $groupLink = $groupName;
135  }
136  } else {
137  if ( $linkTitle ) {
138  $groupLink = $linkRenderer->makeLink( $linkTitle, $groupName );
139  } else {
140  $groupLink = htmlspecialchars( $groupName );
141  }
142  }
143 
144  if ( $expiry ) {
145  // format the expiry to a nice string
146  $uiUser = $context->getUser();
147  $expiryDT = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
148  $expiryD = $uiLanguage->userDate( $expiry, $uiUser );
149  $expiryT = $uiLanguage->userTime( $expiry, $uiUser );
150 
151  if ( $format === 'wiki' ) {
152  return $context->msg( 'group-membership-link-with-expiry' )
153  ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->text();
154  } else {
155  // @phan-suppress-next-line SecurityCheck-XSS False positive, only XSS if wiki format
156  $groupLink = Message::rawParam( $groupLink );
157  return $context->msg( 'group-membership-link-with-expiry' )
158  ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->escaped();
159  }
160  }
161  return $groupLink;
162  }
163 
172  public static function getGroupName( $group ) {
173  return RequestContext::getMain()->getLanguage()->getGroupName( $group );
174  }
175 
186  public static function getGroupMemberName( $group, $member ) {
187  return RequestContext::getMain()->getLanguage()->getGroupMemberName( $group, $member );
188  }
189 
197  public static function getGroupPage( $group ) {
198  $msg = wfMessage( "grouppage-$group" )->inContentLanguage();
199  if ( $msg->exists() ) {
200  $title = Title::newFromText( $msg->text() );
201  if ( is_object( $title ) ) {
202  return $title;
203  }
204  }
205  return false;
206  }
207 
216  public function equals( UserGroupMembership $ugm ) {
217  return (
218  $ugm->getUserId() === $this->userId
219  && $ugm->getGroup() === $this->group
220  );
221  }
222 
223 }
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition: Title.php:82
static rawParam( $raw)
Definition: Message.php:1135
static getMain()
Get the RequestContext object associated with the main request.
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.