Go to the documentation of this file.
55 $this->expiry =
$expiry ?:
null;
80 $this->userId = (int)$row->ug_user;
81 $this->group = $row->ug_group;
82 $this->expiry = $row->ug_expiry ===
null ?
95 $ugm->initFromRow( $row );
124 if ( $dbw ===
null ) {
130 [
'ug_user' => $this->userId,
'ug_group' => $this->group ],
138 'user_former_groups',
139 [
'ufg_user' => $this->userId,
'ufg_group' => $this->group ],
157 if ( $this->group ===
null ) {
158 throw new UnexpectedValueException(
159 'Cannot insert an uninitialized UserGroupMembership instance'
161 } elseif ( $this->userId <= 0 ) {
162 throw new UnexpectedValueException(
163 'UserGroupMembership::insert() needs a positive user ID. ' .
164 'Perhaps addGroup() was called before the user was added to the database.'
172 $dbw->
insert(
'user_groups', $row, __METHOD__, [
'IGNORE' ] );
177 $conds = [
'ug_user' => $row[
'ug_user'],
'ug_group' => $row[
'ug_group'] ];
178 if ( $allowUpdate ) {
180 $conds[] = $this->expiry
181 ?
'ug_expiry IS NULL OR ug_expiry != ' .
183 :
'ug_expiry IS NOT NULL';
190 [
'ug_expiry' => $this->expiry ? $dbw->
timestamp( $this->expiry ) : null ],
207 if ( $hasExpiredRow ) {
212 return $affected > 0;
224 'ug_expiry' => $this->expiry ? $db->
timestamp( $this->expiry ) :
null,
233 if ( !$this->expiry ) {
246 $services = MediaWikiServices::getInstance();
247 if ( $services->getReadOnlyMode()->isReadOnly() ) {
251 $lbFactory = $services->getDBLoadBalancerFactory();
252 $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
253 $dbw = $services->getDBLoadBalancer()->getConnectionRef(
DB_MASTER );
255 $lockKey =
"{$dbw->getDomainID()}:UserGroupMembership:purge";
257 if ( !$scopedLock ) {
268 self::selectFields(),
271 [
'FOR UPDATE',
'LIMIT' => 100 ]
274 if (
$res->numRows() > 0 ) {
277 foreach (
$res as $row ) {
278 $insertData[] = [
'ufg_user' => $row->ug_user,
'ufg_group' => $row->ug_group ];
280 [
'ug_user' => $row->ug_user,
'ug_group' => $row->ug_group ],
291 $dbw->
insert(
'user_former_groups', $insertData, __METHOD__, [
'IGNORE' ] );
293 $purgedRows +=
$res->numRows();
298 $lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
299 }
while (
$res->numRows() > 0 );
316 $res = $db->select(
'user_groups',
317 self::selectFields(),
322 foreach (
$res as $row ) {
324 if ( !$ugm->isExpired() ) {
325 $ugms[$ugm->group] = $ugm;
348 $row = $db->selectRow(
'user_groups',
349 self::selectFields(),
357 if ( !$ugm->isExpired() ) {
379 if ( $format !==
'wiki' && $format !==
'html' ) {
380 throw new MWException(
'UserGroupMembership::getLink() $format parameter should be ' .
381 "'wiki' or 'html'" );
386 $group = $ugm->getGroup();
392 if ( $userName !==
null ) {
400 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
402 if ( $format ===
'wiki' ) {
403 $linkPage = $linkTitle->getFullText();
404 $groupLink =
"[[$linkPage|$groupName]]";
406 $groupLink = $linkRenderer->makeLink( $linkTitle, $groupName );
409 $groupLink = htmlspecialchars( $groupName );
414 $uiLanguage =
$context->getLanguage();
416 $expiryDT = $uiLanguage->userTimeAndDate(
$expiry, $uiUser );
417 $expiryD = $uiLanguage->userDate(
$expiry, $uiUser );
418 $expiryT = $uiLanguage->userTime(
$expiry, $uiUser );
419 if ( $format ===
'html' ) {
420 $groupLink = Message::rawParam( $groupLink );
422 return $context->msg(
'group-membership-link-with-expiry' )
423 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->text();
437 return $msg->isBlank() ?
$group : $msg->text();
449 $msg =
wfMessage(
"group-$group-member", $username );
450 return $msg->isBlank() ?
$group : $msg->text();
461 $msg =
wfMessage(
"grouppage-$group" )->inContentLanguage();
462 if ( $msg->exists() ) {
464 if ( is_object(
$title ) ) {
getDatabaseArray(IDatabase $db)
Get an array suitable for passing to $dbw->insert() or $dbw->update()
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
insert( $allowUpdate=false, IDatabase $dbw=null)
Insert a user right membership into the database.
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
string null $expiry
Timestamp of expiry in TS_MW format, or null if no expiry.
wfReadOnly()
Check whether the wiki is in read-only mode.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
static purgeExpired()
Purge expired memberships from the user_groups table.
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
static getMembershipsForUser( $userId, IDatabase $db=null)
Returns UserGroupMembership objects for all the groups a user currently belongs to.
static getLink( $ugm, IContextSource $context, $format, $userName=null)
Gets a link for a user group, possibly including the expiry date if relevant.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
static newFromRow( $row)
Creates a new UserGroupMembership object from a database row.
__construct( $userId=0, $group=null, $expiry=null)
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
isExpired()
Has the membership expired?
static selectFields()
Returns the list of user_groups fields that should be selected to create a new user group membership.
int $userId
The ID of the user who belongs to the group.
Interface for objects which can provide a MediaWiki context on request.
static singleton( $domain=false)
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 addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
static getGroupMemberName( $group, $username)
Gets the localized name for a member of a group, if it exists.
Represents a "user group membership" – a specific instance of a user belonging to a group.