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