MediaWiki  master
UserGroupMembership.php
Go to the documentation of this file.
1 <?php
24 use Wikimedia\Assert\Assert;
25 use Wikimedia\Assert\ParameterTypeException;
26 
37 
39  private $userId;
40 
42  private $group;
43 
45  private $expiry;
46 
48  private $expired;
49 
55  public function __construct( $userId = 0, $group = null, $expiry = null ) {
57  $this->userId = (int)$userId;
58  $this->group = $group;
59  $this->expiry = $expiry ?: null;
60  $this->expired = $expiry ? wfTimestampNow() > $expiry : false;
61  }
62 
72  private static function assertValidSpec( $userId, $group, $expiry ) {
73  Assert::parameterType( 'integer', $userId, '$userId' );
74  Assert::parameterType( [ 'string', 'null' ], $group, '$group' );
75  Assert::parameterType( [ 'string', 'null' ], $expiry, '$expiry' );
76  }
77 
81  public function getUserId() {
82  return $this->userId;
83  }
84 
88  public function getGroup() {
89  return $this->group;
90  }
91 
95  public function getExpiry() {
96  return $this->expiry;
97  }
98 
104  public function isExpired() {
105  return $this->expired;
106  }
107 
121  public static function getLink( $ugm, IContextSource $context, $format, $userName = null ) {
122  if ( $format !== 'wiki' && $format !== 'html' ) {
123  throw new MWException( 'UserGroupMembership::getLink() $format parameter should be ' .
124  "'wiki' or 'html'" );
125  }
126 
127  if ( $ugm instanceof UserGroupMembership ) {
128  $expiry = $ugm->getExpiry();
129  $group = $ugm->getGroup();
130  } else {
131  $expiry = null;
132  $group = $ugm;
133  }
134 
135  if ( $userName !== null ) {
136  $groupName = self::getGroupMemberName( $group, $userName );
137  } else {
138  $groupName = self::getGroupName( $group );
139  }
140 
141  // link to the group description page, if it exists
142  $linkTitle = self::getGroupPage( $group );
143  $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
144  if ( $format === 'wiki' ) {
145  if ( $linkTitle ) {
146  $linkPage = $linkTitle->getFullText();
147  $groupLink = "[[$linkPage|$groupName]]";
148  } else {
149  $groupLink = $groupName;
150  }
151  } else {
152  if ( $linkTitle ) {
153  $groupLink = $linkRenderer->makeLink( $linkTitle, $groupName );
154  } else {
155  $groupLink = htmlspecialchars( $groupName );
156  }
157  }
158 
159  if ( $expiry ) {
160  // format the expiry to a nice string
161  $uiLanguage = $context->getLanguage();
162  $uiUser = $context->getUser();
163  $expiryDT = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
164  $expiryD = $uiLanguage->userDate( $expiry, $uiUser );
165  $expiryT = $uiLanguage->userTime( $expiry, $uiUser );
166 
167  if ( $format === 'wiki' ) {
168  return $context->msg( 'group-membership-link-with-expiry' )
169  ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->text();
170  } else {
171  $groupLink = Message::rawParam( $groupLink );
172  return $context->msg( 'group-membership-link-with-expiry' )
173  ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->escaped();
174  }
175  }
176  return $groupLink;
177  }
178 
186  public static function getGroupName( $group ) {
187  $msg = wfMessage( "group-$group" );
188  return $msg->isBlank() ? $group : $msg->text();
189  }
190 
199  public static function getGroupMemberName( $group, $username ) {
200  $msg = wfMessage( "group-$group-member", $username );
201  return $msg->isBlank() ? $group : $msg->text();
202  }
203 
211  public static function getGroupPage( $group ) {
212  $msg = wfMessage( "grouppage-$group" )->inContentLanguage();
213  if ( $msg->exists() ) {
214  $title = Title::newFromText( $msg->text() );
215  if ( is_object( $title ) ) {
216  return $title;
217  }
218  }
219  return false;
220  }
221 
230  public function equals( UserGroupMembership $ugm ) {
231  return (
232  $ugm->getUserId() === $this->userId
233  && $ugm->getGroup() === $this->group
234  );
235  }
236 
237 }
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:328
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:163
UserGroupMembership\getExpiry
getExpiry()
Definition: UserGroupMembership.php:95
UserGroupMembership\getGroupName
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
Definition: UserGroupMembership.php:186
UserGroupMembership\$expiry
string null $expiry
Timestamp of expiry in TS_MW format, or null if no expiry.
Definition: UserGroupMembership.php:45
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1220
UserGroupMembership\getGroup
getGroup()
Definition: UserGroupMembership.php:88
UserGroupMembership\equals
equals(UserGroupMembership $ugm)
Compares two pure value objects.
Definition: UserGroupMembership.php:230
UserGroupMembership\$expired
bool $expired
Expiration flag.
Definition: UserGroupMembership.php:48
UserGroupMembership\getGroupPage
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
Definition: UserGroupMembership.php:211
UserGroupMembership\getUserId
getUserId()
Definition: UserGroupMembership.php:81
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:121
UserGroupMembership\__construct
__construct( $userId=0, $group=null, $expiry=null)
Definition: UserGroupMembership.php:55
UserGroupMembership\assertValidSpec
static assertValidSpec( $userId, $group, $expiry)
Asserts that the given parameters could be used to construct a UserGroupMembership object.
Definition: UserGroupMembership.php:72
$title
$title
Definition: testCompression.php:38
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1844
UserGroupMembership\isExpired
isExpired()
Has the membership expired?
Definition: UserGroupMembership.php:104
UserGroupMembership\$userId
int $userId
The ID of the user who belongs to the group.
Definition: UserGroupMembership.php:39
Message\rawParam
static rawParam( $raw)
Definition: Message.php:1024
IContextSource\getUser
getUser()
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
UserGroupMembership\$group
string $group
Definition: UserGroupMembership.php:42
UserGroupMembership\getGroupMemberName
static getGroupMemberName( $group, $username)
Gets the localized name for a member of a group, if it exists.
Definition: UserGroupMembership.php:199
UserGroupMembership
Represents a "user group membership" – a specific instance of a user belonging to a group.
Definition: UserGroupMembership.php:36
IContextSource\getLanguage
getLanguage()