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