MediaWiki REL1_35
UserGroupMembership.php
Go to the documentation of this file.
1<?php
24use Wikimedia\Assert\Assert;
25use 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 ) {
57 self::assertValidSpec( $userId, $group, $expiry );
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}
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static rawParam( $raw)
Definition Message.php:1053
Represents a "user group membership" – a specific instance of a user belonging to a group.
static newFromRow( $row)
Creates a new UserGroupMembership object from a database row.
static getMembershipsForUser( $userId, IDatabase $db=null)
Returns UserGroupMembership objects for all the groups a user currently belongs to.
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
static getMembership( $userId, $group, IDatabase $db=null)
Returns a UserGroupMembership object that pertains to the given user and group, or false if the user ...
static selectFields()
Returns the list of user_groups fields that should be selected to create a new user group membership.
static getLink( $ugm, IContextSource $context, $format, $userName=null)
Gets a link for a user group, possibly including the expiry date if relevant.
insert( $allowUpdate=false, IDatabase $dbw=null)
Insert a user right membership into the database.
int $userId
The ID of the user who belongs to the group.
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
static purgeExpired()
Purge expired memberships from the user_groups table.
__construct( $userId=0, $group=null, $expiry=null)
isExpired()
Has the membership expired?
static getGroupMemberName( $group, $username)
Gets the localized name for a member of a group, if it exists.
string null $expiry
Timestamp of expiry in TS_MW format, or null if no expiry.
equals(UserGroupMembership $ugm)
Compares two pure value objects.
bool $expired
Expiration flag.
static assertValidSpec( $userId, $group, $expiry)
Asserts that the given parameters could be used to construct a UserGroupMembership object.
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:565
Interface for objects which can provide a MediaWiki context on request.
msg( $key,... $params)
This is the method for getting translated interface messages.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38