Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
78.95% |
15 / 19 |
|
81.82% |
9 / 11 |
CRAP | |
0.00% |
0 / 1 |
| UserIdentityValue | |
78.95% |
15 / 19 |
|
81.82% |
9 / 11 |
13.34 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| newAnonymous | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| newRegistered | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| newExternal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getWikiId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getActorId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| equals | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| isRegistered | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\User; |
| 8 | |
| 9 | use MediaWiki\DAO\WikiAwareEntityTrait; |
| 10 | use Stringable; |
| 11 | use Wikimedia\Assert\Assert; |
| 12 | |
| 13 | /** |
| 14 | * Value object representing a user's identity. |
| 15 | * |
| 16 | * @newable |
| 17 | * @since 1.31 |
| 18 | * @ingroup User |
| 19 | */ |
| 20 | class UserIdentityValue implements Stringable, UserIdentity { |
| 21 | use WikiAwareEntityTrait; |
| 22 | |
| 23 | /** |
| 24 | * @var int |
| 25 | */ |
| 26 | private $id; |
| 27 | |
| 28 | /** |
| 29 | * @var string |
| 30 | */ |
| 31 | private $name; |
| 32 | |
| 33 | /** @var string|false */ |
| 34 | private $wikiId; |
| 35 | |
| 36 | /** |
| 37 | * @stable to call |
| 38 | * |
| 39 | * @note Signature in 1.35 was: ( $id, $name, $actor ). |
| 40 | * |
| 41 | * @param int $id user ID |
| 42 | * @param string $name user name |
| 43 | * @param string|false $wikiId wiki ID or self::LOCAL for the local wiki |
| 44 | */ |
| 45 | public function __construct( int $id, string $name, $wikiId = self::LOCAL ) { |
| 46 | $this->assertWikiIdParam( $wikiId ); |
| 47 | |
| 48 | $this->id = $id; |
| 49 | $this->name = $name; |
| 50 | $this->wikiId = $wikiId; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Create UserIdentity for an anonymous user. |
| 55 | * |
| 56 | * @since 1.36 |
| 57 | * @param string $name |
| 58 | * @param string|false $wikiId wiki ID or self::LOCAL for the local wiki |
| 59 | * @return UserIdentityValue |
| 60 | */ |
| 61 | public static function newAnonymous( string $name, $wikiId = self::LOCAL ): self { |
| 62 | return new self( 0, $name, $wikiId ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Create UserIdentity for a registered user. |
| 67 | * |
| 68 | * @since 1.36 |
| 69 | * @param int $userId |
| 70 | * @param string $name |
| 71 | * @param string|false $wikiId wiki ID or self::LOCAL for the local wiki |
| 72 | * @return UserIdentityValue |
| 73 | */ |
| 74 | public static function newRegistered( int $userId, string $name, $wikiId = self::LOCAL ): self { |
| 75 | Assert::parameter( $userId > 0, '$userId', 'must be greater than zero (user must exist)' ); |
| 76 | return new self( $userId, $name, $wikiId ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Create UserIdentity for an external user with $prefix and $name |
| 81 | * |
| 82 | * @since 1.36 |
| 83 | * @param string $prefix |
| 84 | * @param string $name |
| 85 | * @param string|false $wikiId wiki ID or self::LOCAL for the local wiki |
| 86 | * @return UserIdentityValue |
| 87 | */ |
| 88 | public static function newExternal( string $prefix, string $name, $wikiId = self::LOCAL ): self { |
| 89 | // > is a standard separator for external users in the database, see ExternalUserNames |
| 90 | return new self( 0, "$prefix>$name", $wikiId ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the ID of the wiki this UserIdentity belongs to. |
| 95 | * |
| 96 | * @since 1.36 |
| 97 | * @see RevisionRecord::getWikiId() |
| 98 | * |
| 99 | * @return string|false The wiki's logical name or self::LOCAL to indicate the local wiki |
| 100 | */ |
| 101 | public function getWikiId() { |
| 102 | return $this->wikiId; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * The numerical user ID provided to the constructor. |
| 107 | * |
| 108 | * @param string|false $wikiId The wiki ID expected by the caller |
| 109 | * @return int The user ID. May be 0 for anonymous users or for users with no local account. |
| 110 | */ |
| 111 | public function getId( $wikiId = self::LOCAL ): int { |
| 112 | $this->assertWiki( $wikiId ); |
| 113 | return $this->id; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @return string The user's logical name. May be an IPv4 or IPv6 address for anonymous users. |
| 118 | */ |
| 119 | public function getName(): string { |
| 120 | return $this->name; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @deprecated since 1.36, use ActorNormalization::acquireActorId instead. |
| 125 | * |
| 126 | * @param string|false $wikiId |
| 127 | * |
| 128 | * @return int always 0. |
| 129 | */ |
| 130 | public function getActorId( $wikiId = self::LOCAL ): int { |
| 131 | wfDeprecated( __METHOD__, '1.36' ); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @since 1.32 |
| 137 | * |
| 138 | * @param UserIdentity|null $user |
| 139 | * @return bool |
| 140 | */ |
| 141 | public function equals( ?UserIdentity $user ): bool { |
| 142 | if ( !$user ) { |
| 143 | return false; |
| 144 | } |
| 145 | // XXX it's not clear whether central ID providers are supposed to obey this |
| 146 | return $this->getName() === $user->getName(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @since 1.34 |
| 151 | * |
| 152 | * @return bool True if user is registered on this wiki, i.e., has a user ID. False if user is |
| 153 | * anonymous or has no local account (which can happen when importing). This is equivalent to |
| 154 | * getId() != 0 and is provided for code readability. |
| 155 | */ |
| 156 | public function isRegistered(): bool { |
| 157 | return $this->getId( $this->wikiId ) != 0; |
| 158 | } |
| 159 | |
| 160 | public function __toString(): string { |
| 161 | return $this->getName(); |
| 162 | } |
| 163 | |
| 164 | } |