Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
36.84% |
7 / 19 |
|
45.45% |
5 / 11 |
CRAP | |
0.00% |
0 / 1 |
UserIdentityValue | |
36.84% |
7 / 19 |
|
45.45% |
5 / 11 |
48.28 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
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 | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getActorId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
equals | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
isRegistered | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
__toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Value object representing a user's identity. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | */ |
22 | |
23 | namespace MediaWiki\User; |
24 | |
25 | use MediaWiki\DAO\WikiAwareEntityTrait; |
26 | use Stringable; |
27 | use Wikimedia\Assert\Assert; |
28 | |
29 | /** |
30 | * Value object representing a user's identity. |
31 | * |
32 | * @newable |
33 | * |
34 | * @since 1.31 |
35 | */ |
36 | class UserIdentityValue implements Stringable, UserIdentity { |
37 | use WikiAwareEntityTrait; |
38 | |
39 | /** |
40 | * @var int |
41 | */ |
42 | private $id; |
43 | |
44 | /** |
45 | * @var string |
46 | */ |
47 | private $name; |
48 | |
49 | /** @var string|false */ |
50 | private $wikiId; |
51 | |
52 | /** |
53 | * @stable to call |
54 | * |
55 | * @note Signature in 1.35 was: ( $id, $name, $actor ). |
56 | * |
57 | * @param int $id user ID |
58 | * @param string $name user name |
59 | * @param string|false $wikiId wiki ID or self::LOCAL for the local wiki |
60 | */ |
61 | public function __construct( int $id, string $name, $wikiId = self::LOCAL ) { |
62 | $this->assertWikiIdParam( $wikiId ); |
63 | |
64 | $this->id = $id; |
65 | $this->name = $name; |
66 | $this->wikiId = $wikiId; |
67 | } |
68 | |
69 | /** |
70 | * Create UserIdentity for an anonymous user. |
71 | * |
72 | * @since 1.36 |
73 | * @param string $name |
74 | * @param string|false $wikiId wiki ID or self::LOCAL for the local wiki |
75 | * @return UserIdentityValue |
76 | */ |
77 | public static function newAnonymous( string $name, $wikiId = self::LOCAL ): self { |
78 | return new self( 0, $name, $wikiId ); |
79 | } |
80 | |
81 | /** |
82 | * Create UserIdentity for a registered user. |
83 | * |
84 | * @since 1.36 |
85 | * @param int $userId |
86 | * @param string $name |
87 | * @param string|false $wikiId wiki ID or self::LOCAL for the local wiki |
88 | * @return UserIdentityValue |
89 | */ |
90 | public static function newRegistered( int $userId, string $name, $wikiId = self::LOCAL ): self { |
91 | Assert::parameter( $userId > 0, '$userId', 'must be greater than zero (user must exist)' ); |
92 | return new self( $userId, $name, $wikiId ); |
93 | } |
94 | |
95 | /** |
96 | * Create UserIdentity for an external user with $prefix and $name |
97 | * |
98 | * @since 1.36 |
99 | * @param string $prefix |
100 | * @param string $name |
101 | * @param string|false $wikiId wiki ID or self::LOCAL for the local wiki |
102 | * @return UserIdentityValue |
103 | */ |
104 | public static function newExternal( string $prefix, string $name, $wikiId = self::LOCAL ): self { |
105 | // > is a standard separator for external users in the database, see ExternalUserNames |
106 | return new self( 0, "$prefix>$name", $wikiId ); |
107 | } |
108 | |
109 | /** |
110 | * Get the ID of the wiki this UserIdentity belongs to. |
111 | * |
112 | * @since 1.36 |
113 | * @see RevisionRecord::getWikiId() |
114 | * |
115 | * @return string|false The wiki's logical name or self::LOCAL to indicate the local wiki |
116 | */ |
117 | public function getWikiId() { |
118 | return $this->wikiId; |
119 | } |
120 | |
121 | /** |
122 | * The numerical user ID provided to the constructor. |
123 | * |
124 | * @param string|false $wikiId The wiki ID expected by the caller |
125 | * @return int The user ID. May be 0 for anonymous users or for users with no local account. |
126 | * |
127 | */ |
128 | public function getId( $wikiId = self::LOCAL ): int { |
129 | $this->assertWiki( $wikiId ); |
130 | return $this->id; |
131 | } |
132 | |
133 | /** |
134 | * @return string The user's logical name. May be an IPv4 or IPv6 address for anonymous users. |
135 | */ |
136 | public function getName(): string { |
137 | return $this->name; |
138 | } |
139 | |
140 | /** |
141 | * @deprecated since 1.36, use ActorNormalization::acquireActorId instead. |
142 | * |
143 | * @param string|false $wikiId |
144 | * |
145 | * @return int always 0. |
146 | */ |
147 | public function getActorId( $wikiId = self::LOCAL ): int { |
148 | wfDeprecated( __METHOD__, '1.36' ); |
149 | return 0; |
150 | } |
151 | |
152 | /** |
153 | * @since 1.32 |
154 | * |
155 | * @param UserIdentity|null $user |
156 | * @return bool |
157 | */ |
158 | public function equals( ?UserIdentity $user ): bool { |
159 | if ( !$user ) { |
160 | return false; |
161 | } |
162 | // XXX it's not clear whether central ID providers are supposed to obey this |
163 | return $this->getName() === $user->getName(); |
164 | } |
165 | |
166 | /** |
167 | * @since 1.34 |
168 | * |
169 | * @return bool True if user is registered on this wiki, i.e., has a user ID. False if user is |
170 | * anonymous or has no local account (which can happen when importing). This is equivalent to |
171 | * getId() != 0 and is provided for code readability. |
172 | */ |
173 | public function isRegistered(): bool { |
174 | return $this->getId( $this->wikiId ) != 0; |
175 | } |
176 | |
177 | public function __toString(): string { |
178 | return $this->getName(); |
179 | } |
180 | |
181 | } |