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( $id, $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  Assert::parameterType( 'integer', $id, '$id' );
78  Assert::parameterType( 'string', $name, '$name' );
79  $this->assertWikiIdParam( $wikiId );
80 
81  $this->id = $id;
82  $this->name = $name;
83  $this->wikiId = $wikiId;
84  }
85 
94  public static function newAnonymous( string $name, $wikiId = self::LOCAL ): self {
95  return new self( 0, $name, $wikiId );
96  }
97 
107  public static function newRegistered( int $userId, string $name, $wikiId = self::LOCAL ): self {
108  Assert::parameter( $userId > 0, '$userId', 'must be greater than zero (user must exist)' );
109  return new self( $userId, $name, $wikiId );
110  }
111 
121  public static function newExternal( string $prefix, string $name, $wikiId = self::LOCAL ): self {
122  // > is a standard separator for external users in the database, see ExternalUserNames
123  return new self( 0, "$prefix>$name", $wikiId );
124  }
125 
134  public function getWikiId() {
135  return $this->wikiId;
136  }
137 
145  public function getId( $wikiId = self::LOCAL ) : int {
146  $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
147  return $this->id;
148  }
149 
153  public function getName() : string {
154  return $this->name;
155  }
156 
164  public function getActorId( $wikiId = self::LOCAL ) : int {
165  wfDeprecated( __METHOD__, '1.36' );
166  return 0;
167  }
168 
175  public function equals( ?UserIdentity $user ) : bool {
176  if ( !$user ) {
177  return false;
178  }
179  // XXX it's not clear whether central ID providers are supposed to obey this
180  return $this->getName() === $user->getName();
181  }
182 
190  public function isRegistered() : bool {
191  return $this->getId( $this->wikiId ) != 0;
192  }
193 
194  public function __toString(): string {
195  return $this->getName();
196  }
197 
198 }
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:164
MediaWiki\User\UserIdentityValue\__toString
__toString()
Definition: UserIdentityValue.php:194
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\User\UserIdentityValue\__construct
__construct( $id, $name, $wikiId=self::LOCAL)
Definition: UserIdentityValue.php:61
MediaWiki\User\UserIdentityValue\equals
equals(?UserIdentity $user)
Definition: UserIdentityValue.php:175
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 $function is deprecated.
Definition: GlobalFunctions.php:996
MediaWiki\User\UserIdentityValue\getId
getId( $wikiId=self::LOCAL)
The numerical user ID provided to the constructor.
Definition: UserIdentityValue.php:145
$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:134
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:94
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:107
MediaWiki\User\UserIdentityValue\getName
getName()
Definition: UserIdentityValue.php:153
MediaWiki\User\UserIdentityValue\isRegistered
isRegistered()
Definition: UserIdentityValue.php:190
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:121
MediaWiki\User\UserIdentityValue\$id
int $id
Definition: UserIdentityValue.php:41