52 public function __construct( $userId = 0, $group =
null, $expiry =
null ) {
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 ],
200 DeferredUpdates::addCallableUpdate(
function () use ( $dbw, $fname ) {
207 if ( $hasExpiredRow ) {
212 return $affected > 0;
222 'ug_user' => $this->userId,
223 'ug_group' => $this->group,
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 ],
287 $dbw->
makeList( $deleteCond, $dbw::LIST_OR ),
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 ) {
323 $ugm = self::newFromRow( $row );
324 if ( !$ugm->isExpired() ) {
325 $ugms[$ugm->group] = $ugm;
348 $row = $db->selectRow(
'user_groups',
349 self::selectFields(),
356 $ugm = self::newFromRow( $row );
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 ) {
393 $groupName = self::getGroupMemberName(
$group, $userName );
395 $groupName = self::getGroupName(
$group );
399 $linkTitle = self::getGroupPage(
$group );
400 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
401 if ( $format ===
'wiki' ) {
403 $linkPage = $linkTitle->getFullText();
404 $groupLink =
"[[$linkPage|$groupName]]";
406 $groupLink = $groupName;
410 $groupLink = $linkRenderer->makeLink( $linkTitle, $groupName );
412 $groupLink = htmlspecialchars( $groupName );
418 $uiLanguage =
$context->getLanguage();
420 $expiryDT = $uiLanguage->userTimeAndDate(
$expiry, $uiUser );
421 $expiryD = $uiLanguage->userDate(
$expiry, $uiUser );
422 $expiryT = $uiLanguage->userTime(
$expiry, $uiUser );
424 if ( $format ===
'wiki' ) {
425 return $context->msg(
'group-membership-link-with-expiry' )
426 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->text();
429 return $context->msg(
'group-membership-link-with-expiry' )
430 ->params( $groupLink, $expiryDT, $expiryD, $expiryT )->escaped();
445 return $msg->isBlank() ?
$group : $msg->text();
457 $msg =
wfMessage(
"group-$group-member", $username );
458 return $msg->isBlank() ?
$group : $msg->text();
469 $msg =
wfMessage(
"grouppage-$group" )->inContentLanguage();
470 if ( $msg->exists() ) {
471 $title = Title::newFromText( $msg->text() );
472 if ( is_object(
$title ) ) {
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfReadOnly()
Check whether the wiki is in read-only mode.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
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.
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.
getDatabaseArray(IDatabase $db)
Get an array suitable for passing to $dbw->insert() or $dbw->update()
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.
Interface for objects which can provide a MediaWiki context on request.