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