MediaWiki  master
UserIdentityValue.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\User;
24 
26 use Wikimedia\Assert\Assert;
27 use Wikimedia\Assert\PostconditionException;
29 
37 class UserIdentityValue implements UserIdentity {
39 
43  private $id;
44 
48  private $name;
49 
53  private $actor;
54 
56  private $wikiId;
57 
66  public function __construct( $id, $name, $actor = 0, $wikiId = self::LOCAL ) {
67  Assert::parameterType( 'integer', $id, '$id' );
68  Assert::parameterType( 'string', $name, '$name' );
69  Assert::parameterType( 'integer', $actor, '$actor' );
70  $this->assertWikiIdParam( $wikiId );
71 
72  $this->id = $id;
73  $this->name = $name;
74  $this->actor = $actor;
75  $this->wikiId = $wikiId;
76  }
77 
86  public function getWikiId() {
87  return $this->wikiId;
88  }
89 
97  public function getId( $wikiId = self::LOCAL ) : int {
98  $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
99  return $this->id;
100  }
101 
105  public function getName() : string {
106  return $this->name;
107  }
108 
119  public function getActorId( $wikiId = self::LOCAL ) : int {
120  // need to check if $wikiId is an instance of IDatabase, since for legacy reasons,
121  // some callers call this function on a UserIdentity passing an IDatabase
122  if ( $wikiId instanceof IDatabase ) {
123  wfDeprecatedMsg( 'Passing parameter of type IDatabase', '1.36' );
124  if ( $this->actor === 0 ) {
125  // Passing an IDatabase was never actually allowed on this class.
126  // We just support it to prevent hard breakage in places where callers where
127  // assuming they got a User, when they actually just have a UserIdentity.
128  // However, the caller is expecting to get back a valid actorId. Not getting one
129  // may cause data corruption.
130  throw new PostconditionException(
131  'Caller expects non-zero actor ID.'
132  );
133  }
134  } else {
135  $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
136  }
137  return $this->actor;
138  }
139 
146  public function equals( UserIdentity $user ) : bool {
147  // XXX it's not clear whether central ID providers are supposed to obey this
148  return $this->getName() === $user->getName();
149  }
150 
158  public function isRegistered() : bool {
159  return $this->getId() != 0;
160  }
161 
162  public function __toString(): string {
163  return $this->getName();
164  }
165 
176  public function setActorId( int $actorId ) {
177  $this->actor = $actorId;
178  }
179 }
MediaWiki\User\UserIdentityValue
Value object representing a user's identity.
Definition: UserIdentityValue.php:37
MediaWiki\DAO\WikiAwareEntityTrait
trait WikiAwareEntityTrait
Definition: WikiAwareEntityTrait.php:32
MediaWiki\DAO\deprecateInvalidCrossWiki
deprecateInvalidCrossWiki( $wikiId, string $since)
Emits a deprecation warning $since version if $wikiId is not the same as this wiki.
Definition: WikiAwareEntityTrait.php:70
MediaWiki\User\UserIdentityValue\getActorId
getActorId( $wikiId=self::LOCAL)
The numerical actor ID provided to the constructor or 0 if no actor ID has been assigned.
Definition: UserIdentityValue.php:119
MediaWiki\User\UserIdentityValue\__toString
__toString()
Definition: UserIdentityValue.php:162
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
wfDeprecatedMsg
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
Definition: GlobalFunctions.php:1066
MediaWiki\User\UserIdentityValue\__construct
__construct( $id, $name, $actor=0, $wikiId=self::LOCAL)
Definition: UserIdentityValue.php:66
MediaWiki\User\UserIdentityValue\getId
getId( $wikiId=self::LOCAL)
The numerical user ID provided to the constructor.
Definition: UserIdentityValue.php:97
MediaWiki\User\UserIdentityValue\getWikiId
getWikiId()
Get the ID of the wiki this UserIdentity belongs to.
Definition: UserIdentityValue.php:86
MediaWiki\User\UserIdentity\getName
getName()
MediaWiki\User\UserIdentityValue\$name
string $name
Definition: UserIdentityValue.php:48
MediaWiki\User\UserIdentityValue\$wikiId
bool string $wikiId
Definition: UserIdentityValue.php:56
MediaWiki\User
Definition: ActorNormalization.php:21
MediaWiki\DAO\assertWikiIdParam
assertWikiIdParam( $wikiId)
Asserts correct $wikiId parameter was passed.
Definition: WikiAwareEntityTrait.php:88
MediaWiki\User\UserIdentityValue\$actor
int $actor
Definition: UserIdentityValue.php:53
MediaWiki\User\UserIdentityValue\getName
getName()
Definition: UserIdentityValue.php:105
MediaWiki\User\UserIdentityValue\isRegistered
isRegistered()
Definition: UserIdentityValue.php:158
MediaWiki\User\UserIdentityValue\setActorId
setActorId(int $actorId)
Sets the actor id.
Definition: UserIdentityValue.php:176
MediaWiki\User\UserIdentityValue\equals
equals(UserIdentity $user)
Definition: UserIdentityValue.php:146
MediaWiki\User\UserIdentityValue\$id
int $id
Definition: UserIdentityValue.php:43