MediaWiki  master
UserGroupMembership.php
Go to the documentation of this file.
1 <?php
24 use Wikimedia\Assert\Assert;
25 use Wikimedia\Assert\ParameterTypeException;
27 
38 
40  private $userId;
41 
43  private $group;
44 
46  private $expiry;
47 
49  private $expired;
50 
56  public function __construct( $userId = 0, $group = null, $expiry = null ) {
58  $this->userId = (int)$userId;
59  $this->group = $group;
60  $this->expiry = $expiry ?: null;
61  $this->expired = $expiry ? wfTimestampNow() > $expiry : false;
62  }
63 
73  private static function assertValidSpec( $userId, $group, $expiry ) {
74  Assert::parameterType( 'integer', $userId, '$userId' );
75  Assert::parameterType( [ 'string', 'null' ], $group, '$group' );
76  Assert::parameterType( [ 'string', 'null' ], $expiry, '$expiry' );
77  }
78 
82  public function getUserId() {
83  return $this->userId;
84  }
85 
89  public function getGroup() {
90  return $this->group;
91  }
92 
96  public function getExpiry() {
97  return $this->expiry;
98  }
99 
104  protected function initFromRow( $row ) {
105  wfDeprecated( __METHOD__, '1.35' );
106  $this->userId = (int)$row->ug_user;
107  $this->group = $row->ug_group;
108  $this->expiry = $row->ug_expiry === null ?
109  null :
110  wfTimestamp( TS_MW, $row->ug_expiry );
111  }
112 
121  public static function newFromRow( $row ) {
122  wfDeprecated( __METHOD__, '1.35' );
123  return new self(
124  (int)$row->ug_user,
125  $row->ug_group,
126  $row->ug_expiry === null ? null : wfTimestamp( TS_MW, $row->ug_expiry )
127  );
128  }
129 
137  public static function selectFields() {
138  wfDeprecated( __METHOD__, '1.35' );
139  return MediaWikiServices::getInstance()->getUserGroupManager()->getQueryInfo()['fields'];
140  }
141 
151  public function delete( IDatabase $dbw = null ) {
152  wfDeprecated( __METHOD__, '1.35' );
153  return MediaWikiServices::getInstance()
154  ->getUserGroupManager()
155  ->removeUserFromGroup(
156  // TODO: we're forced to forge a User instance here because we don't have
157  // a username around to create an artificial UserIdentityValue
158  // and the username is being used down the tree. This will be gone once the
159  // deprecated method is removed
160  User::newFromId( $this->getUserId() ),
161  $this->group
162  );
163  }
164 
177  public function insert( $allowUpdate = false, IDatabase $dbw = null ) {
178  wfDeprecated( __METHOD__, '1.35' );
179  return MediaWikiServices::getInstance()
180  ->getUserGroupManager()
181  ->addUserToGroup(
182  // TODO: we're forced to forge a User instance here because we don't have
183  // a username around to create an artificial UserIdentityValue
184  // and the username is being used down the tree. This will be gone once the
185  // deprecated method is removed
186  User::newFromId( $this->getUserId() ),
187  $this->group,
188  $this->expiry,
189  $allowUpdate
190  );
191  }
192 
198  public function isExpired() {
199  return $this->expired;
200  }
201 
210  public static function purgeExpired() {
211  wfDeprecated( __METHOD__, '1.35' );
212  return MediaWikiServices::getInstance()
213  ->getUserGroupManager()
214  ->purgeExpired();
215  }
216 
227  public static function getMembershipsForUser( $userId, IDatabase $db = null ) {
228  wfDeprecated( __METHOD__, '1.35' );
229  return MediaWikiServices::getInstance()
230  ->getUserGroupManager()
231  ->getUserGroupMemberships(
232  // TODO: we're forced to forge a User instance here because we don't have
233  // a username around to create an artificial UserIdentityValue
234  // and the username is being used down the tree. This will be gone once the
235  // deprecated method is removed
237  );
238  }
239 
252  public static function getMembership( $userId, $group, IDatabase $db = null ) {
253  wfDeprecated( __METHOD__, '1.35' );
254  $ugms = MediaWikiServices::getInstance()
255  ->getUserGroupManager()
256  ->getUserGroupMemberships(
257  // TODO: we're forced to forge a User instance here because we don't have
258  // a username around to create an artificial UserIdentityValue
259  // and the username is being used down the tree. This will be gone once the
260  // deprecated method is removed
262  );
263  return $ugms[$group] ?? false;
264  }
265 
279  public static function getLink( $ugm, IContextSource $context, $format, $userName = null ) {
280  if ( $format !== 'wiki' && $format !== 'html' ) {
281  throw new MWException( 'UserGroupMembership::getLink() $format parameter should be ' .
282  "'wiki' or 'html'" );
283  }
284 
285  if ( $ugm instanceof UserGroupMembership ) {
286  $expiry = $ugm->getExpiry();
287  $group = $ugm->getGroup();
288  } else {
289  $expiry = null;
290  $group = $ugm;
291  }
292 
293  if ( $userName !== null ) {
294  $groupName = self::getGroupMemberName( $group, $userName );
295  } else {
296  $groupName = self::getGroupName( $group );
297  }
298 
299  // link to the group description page, if it exists
300  $linkTitle = self::getGroupPage( $group );
301  $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
302  if ( $format === 'wiki' ) {
303  if ( $linkTitle ) {
304  $linkPage = $linkTitle->getFullText();
305  $groupLink = "[[$linkPage|$groupName]]";
306  } else {
307  $groupLink = $groupName;
308  }
309  } else {
310  if ( $linkTitle ) {
311  $groupLink = $linkRenderer->makeLink( $linkTitle, $groupName );
312  } else {
313  $groupLink = htmlspecialchars( $groupName );
314  }
315  }
316 
317  if ( $expiry ) {
318  // format the expiry to a nice string
319  $uiLanguage = $context->getLanguage();
320  $uiUser = $context->getUser();
321  $expiryDT = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
322  $expiryD = $uiLanguage->userDate( $expiry, $uiUser );
323  $expiryT = $uiLanguage->userTime( $expiry, $uiUser );
324 
325  if ( $format === 'wiki' ) {
326  return $context->msg( 'group-membership-link-with-expiry' )
327  ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->text();
328  } else {
329  $groupLink = Message::rawParam( $groupLink );
330  return $context->msg( 'group-membership-link-with-expiry' )
331  ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->escaped();
332  }
333  }
334  return $groupLink;
335  }
336 
344  public static function getGroupName( $group ) {
345  $msg = wfMessage( "group-$group" );
346  return $msg->isBlank() ? $group : $msg->text();
347  }
348 
357  public static function getGroupMemberName( $group, $username ) {
358  $msg = wfMessage( "group-$group-member", $username );
359  return $msg->isBlank() ? $group : $msg->text();
360  }
361 
369  public static function getGroupPage( $group ) {
370  $msg = wfMessage( "grouppage-$group" )->inContentLanguage();
371  if ( $msg->exists() ) {
372  $title = Title::newFromText( $msg->text() );
373  if ( is_object( $title ) ) {
374  return $title;
375  }
376  }
377  return false;
378  }
379 
388  public function equals( UserGroupMembership $ugm ) {
389  return (
390  $ugm->getUserId() === $this->userId
391  && $ugm->getGroup() === $this->group
392  );
393  }
394 
395 }
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:562
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:329
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
UserGroupMembership\insert
insert( $allowUpdate=false, IDatabase $dbw=null)
Insert a user right membership into the database.
Definition: UserGroupMembership.php:177
UserGroupMembership\getExpiry
getExpiry()
Definition: UserGroupMembership.php:96
UserGroupMembership\getGroupName
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
Definition: UserGroupMembership.php:344
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1808
UserGroupMembership\$expiry
string null $expiry
Timestamp of expiry in TS_MW format, or null if no expiry.
Definition: UserGroupMembership.php:46
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1219
UserGroupMembership\purgeExpired
static purgeExpired()
Purge expired memberships from the user_groups table.
Definition: UserGroupMembership.php:210
UserGroupMembership\getGroup
getGroup()
Definition: UserGroupMembership.php:89
UserGroupMembership\equals
equals(UserGroupMembership $ugm)
Compares two pure value objects.
Definition: UserGroupMembership.php:388
UserGroupMembership\$expired
bool $expired
Expiration flag.
Definition: UserGroupMembership.php:49
UserGroupMembership\getGroupPage
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
Definition: UserGroupMembership.php:369
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
UserGroupMembership\getMembershipsForUser
static getMembershipsForUser( $userId, IDatabase $db=null)
Returns UserGroupMembership objects for all the groups a user currently belongs to.
Definition: UserGroupMembership.php:227
UserGroupMembership\getUserId
getUserId()
Definition: UserGroupMembership.php:82
MWException
MediaWiki exception.
Definition: MWException.php:29
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1026
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:279
UserGroupMembership\newFromRow
static newFromRow( $row)
Creates a new UserGroupMembership object from a database row.
Definition: UserGroupMembership.php:121
UserGroupMembership\__construct
__construct( $userId=0, $group=null, $expiry=null)
Definition: UserGroupMembership.php:56
UserGroupMembership\assertValidSpec
static assertValidSpec( $userId, $group, $expiry)
Asserts that the given parameters could be used to construct a UserGroupMembership object.
Definition: UserGroupMembership.php:73
$title
$title
Definition: testCompression.php:38
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1837
UserGroupMembership\isExpired
isExpired()
Has the membership expired?
Definition: UserGroupMembership.php:198
UserGroupMembership\selectFields
static selectFields()
Returns the list of user_groups fields that should be selected to create a new user group membership.
Definition: UserGroupMembership.php:137
UserGroupMembership\$userId
int $userId
The ID of the user who belongs to the group.
Definition: UserGroupMembership.php:40
Message\rawParam
static rawParam( $raw)
Definition: Message.php:1037
IContextSource\getUser
getUser()
UserGroupMembership\initFromRow
initFromRow( $row)
Definition: UserGroupMembership.php:104
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
UserGroupMembership\getMembership
static getMembership( $userId, $group, IDatabase $db=null)
Returns a UserGroupMembership object that pertains to the given user and group, or false if the user ...
Definition: UserGroupMembership.php:252
UserGroupMembership\$group
string $group
Definition: UserGroupMembership.php:43
UserGroupMembership\getGroupMemberName
static getGroupMemberName( $group, $username)
Gets the localized name for a member of a group, if it exists.
Definition: UserGroupMembership.php:357
UserGroupMembership
Represents a "user group membership" – a specific instance of a user belonging to a group.
Definition: UserGroupMembership.php:37
IContextSource\getLanguage
getLanguage()