MediaWiki  master
UserIdentityValue.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\User;
24 
26 use Wikimedia\Assert\Assert;
27 
35 class UserIdentityValue implements UserIdentity {
37 
41  private $id;
42 
46  private $name;
47 
49  private $wikiId;
50 
61  public function __construct( int $id, string $name, $wikiId = self::LOCAL ) {
62  if ( is_int( $wikiId ) ) {
63  // Handle old signature: ( $id, $name, $actor, $wikiId )
64  $args = func_get_args();
65  $actor = $args[2];
66  $wikiId = $args[3] ?? self::LOCAL;
67 
68  if ( $actor > 0 || $wikiId !== self::LOCAL ) {
69  // NOTE: For now, we only trigger a deprecation warning if $actor is not 0,
70  // or a $wikiId is given.
71  // A lot of tests construct UserIdentityValue with $actor = 0.
72  // TODO: Trigger a deprecation warning even if $actor is 0
73  wfDeprecatedMsg( 'Old constructor signature: $actor is no longer supported', '1.36' );
74  }
75  }
76 
77  $this->assertWikiIdParam( $wikiId );
78 
79  $this->id = $id;
80  $this->name = $name;
81  $this->wikiId = $wikiId;
82  }
83 
92  public static function newAnonymous( string $name, $wikiId = self::LOCAL ): self {
93  return new self( 0, $name, $wikiId );
94  }
95 
105  public static function newRegistered( int $userId, string $name, $wikiId = self::LOCAL ): self {
106  Assert::parameter( $userId > 0, '$userId', 'must be greater than zero (user must exist)' );
107  return new self( $userId, $name, $wikiId );
108  }
109 
119  public static function newExternal( string $prefix, string $name, $wikiId = self::LOCAL ): self {
120  // > is a standard separator for external users in the database, see ExternalUserNames
121  return new self( 0, "$prefix>$name", $wikiId );
122  }
123 
132  public function getWikiId() {
133  return $this->wikiId;
134  }
135 
143  public function getId( $wikiId = self::LOCAL ): int {
144  $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
145  return $this->id;
146  }
147 
151  public function getName(): string {
152  return $this->name;
153  }
154 
162  public function getActorId( $wikiId = self::LOCAL ): int {
163  wfDeprecated( __METHOD__, '1.36' );
164  return 0;
165  }
166 
173  public function equals( ?UserIdentity $user ): bool {
174  if ( !$user ) {
175  return false;
176  }
177  // XXX it's not clear whether central ID providers are supposed to obey this
178  return $this->getName() === $user->getName();
179  }
180 
188  public function isRegistered(): bool {
189  return $this->getId( $this->wikiId ) != 0;
190  }
191 
192  public function __toString(): string {
193  return $this->getName();
194  }
195 
196 }
MediaWiki\User\UserIdentityValue
Value object representing a user's identity.
Definition: UserIdentityValue.php:35
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)
Definition: UserIdentityValue.php:162
MediaWiki\User\UserIdentityValue\__toString
__toString()
Definition: UserIdentityValue.php:192
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\User\UserIdentityValue\equals
equals(?UserIdentity $user)
Definition: UserIdentityValue.php:173
wfDeprecatedMsg
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
Definition: GlobalFunctions.php:1028
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
MediaWiki\User\UserIdentityValue\getId
getId( $wikiId=self::LOCAL)
The numerical user ID provided to the constructor.
Definition: UserIdentityValue.php:143
$args
if( $line===false) $args
Definition: mcc.php:124
MediaWiki\User\UserIdentityValue\getWikiId
getWikiId()
Get the ID of the wiki this UserIdentity belongs to.
Definition: UserIdentityValue.php:132
MediaWiki\User\UserIdentity\getName
getName()
MediaWiki\User\UserIdentityValue\newAnonymous
static newAnonymous(string $name, $wikiId=self::LOCAL)
Create UserIdentity for an anonymous user.
Definition: UserIdentityValue.php:92
MediaWiki\User\UserIdentityValue\$name
string $name
Definition: UserIdentityValue.php:46
MediaWiki\User\UserIdentityValue\$wikiId
bool string $wikiId
Definition: UserIdentityValue.php:49
MediaWiki\User
Definition: ActorCache.php:21
MediaWiki\DAO\assertWikiIdParam
assertWikiIdParam( $wikiId)
Asserts correct $wikiId parameter was passed.
Definition: WikiAwareEntityTrait.php:88
MediaWiki\User\UserIdentityValue\newRegistered
static newRegistered(int $userId, string $name, $wikiId=self::LOCAL)
Create UserIdentity for a registered user.
Definition: UserIdentityValue.php:105
MediaWiki\User\UserIdentityValue\__construct
__construct(int $id, string $name, $wikiId=self::LOCAL)
Definition: UserIdentityValue.php:61
MediaWiki\User\UserIdentityValue\getName
getName()
Definition: UserIdentityValue.php:151
MediaWiki\User\UserIdentityValue\isRegistered
isRegistered()
Definition: UserIdentityValue.php:188
MediaWiki\User\UserIdentityValue\newExternal
static newExternal(string $prefix, string $name, $wikiId=self::LOCAL)
Create UserIdentity for an external user with $prefix and $name.
Definition: UserIdentityValue.php:119
MediaWiki\User\UserIdentityValue\$id
int $id
Definition: UserIdentityValue.php:41