MediaWiki  master
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 }
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:383
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:191
UserGroupMembership\getExpiry
getExpiry()
Definition: UserGroupMembership.php:77
UserGroupMembership\getGroupName
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
Definition: UserGroupMembership.php:168
UserGroupMembership\$expiry
string null $expiry
Timestamp of expiry in TS_MW format, or null if no expiry.
Definition: UserGroupMembership.php:43
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1182
UserGroupMembership\getGroup
getGroup()
Definition: UserGroupMembership.php:70
UserGroupMembership\equals
equals(UserGroupMembership $ugm)
Compares two pure value objects.
Definition: UserGroupMembership.php:212
UserGroupMembership\$expired
bool $expired
Expiration flag.
Definition: UserGroupMembership.php:46
UserGroupMembership\getGroupPage
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
Definition: UserGroupMembership.php:193
UserGroupMembership\getUserId
getUserId()
Definition: UserGroupMembership.php:63
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:103
UserGroupMembership\__construct
__construct(int $userId=0, ?string $group=null, ?string $expiry=null)
Definition: UserGroupMembership.php:53
$title
$title
Definition: testCompression.php:38
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1720
UserGroupMembership\isExpired
isExpired()
Has the membership expired?
Definition: UserGroupMembership.php:86
UserGroupMembership\$userId
int $userId
The ID of the user who belongs to the group.
Definition: UserGroupMembership.php:37
Message\rawParam
static rawParam( $raw)
Definition: Message.php:1074
IContextSource\getUser
getUser()
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
UserGroupMembership\$group
string $group
Definition: UserGroupMembership.php:40
UserGroupMembership\getGroupMemberName
static getGroupMemberName( $group, $username)
Gets the localized name for a member of a group, if it exists.
Definition: UserGroupMembership.php:181
UserGroupMembership
Represents a "user group membership" – a specific instance of a user belonging to a group.
Definition: UserGroupMembership.php:34
IContextSource\getLanguage
getLanguage()