2017-08-27 15:29:18 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Value object representing a user's identity.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\User;
|
|
|
|
|
|
2021-02-06 00:54:54 +00:00
|
|
|
use MediaWiki\DAO\WikiAwareEntityTrait;
|
2024-06-12 23:59:33 +00:00
|
|
|
use Stringable;
|
2017-08-27 15:29:18 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Value object representing a user's identity.
|
|
|
|
|
*
|
2020-06-26 12:56:03 +00:00
|
|
|
* @newable
|
|
|
|
|
*
|
2017-08-27 15:29:18 +00:00
|
|
|
* @since 1.31
|
|
|
|
|
*/
|
2024-06-12 23:59:33 +00:00
|
|
|
class UserIdentityValue implements Stringable, UserIdentity {
|
2021-02-06 00:54:54 +00:00
|
|
|
use WikiAwareEntityTrait;
|
2017-08-27 15:29:18 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $name;
|
|
|
|
|
|
2022-07-31 00:02:18 +00:00
|
|
|
/** @var string|false */
|
2021-01-27 04:25:24 +00:00
|
|
|
private $wikiId;
|
|
|
|
|
|
2017-08-27 15:29:18 +00:00
|
|
|
/**
|
2020-07-13 08:53:06 +00:00
|
|
|
* @stable to call
|
2020-06-26 12:56:03 +00:00
|
|
|
*
|
2023-12-27 11:59:14 +00:00
|
|
|
* @note Signature in 1.35 was: ( $id, $name, $actor ).
|
2021-02-15 18:58:09 +00:00
|
|
|
*
|
2021-01-27 04:25:24 +00:00
|
|
|
* @param int $id user ID
|
|
|
|
|
* @param string $name user name
|
|
|
|
|
* @param string|false $wikiId wiki ID or self::LOCAL for the local wiki
|
2017-08-27 15:29:18 +00:00
|
|
|
*/
|
2021-07-30 16:29:14 +00:00
|
|
|
public function __construct( int $id, string $name, $wikiId = self::LOCAL ) {
|
2021-02-06 00:54:54 +00:00
|
|
|
$this->assertWikiIdParam( $wikiId );
|
2017-08-27 15:29:18 +00:00
|
|
|
|
|
|
|
|
$this->id = $id;
|
|
|
|
|
$this->name = $name;
|
2021-01-27 04:25:24 +00:00
|
|
|
$this->wikiId = $wikiId;
|
2017-08-27 15:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-05 18:13:13 +00:00
|
|
|
/**
|
|
|
|
|
* Create UserIdentity for an anonymous user.
|
|
|
|
|
*
|
2021-05-12 00:53:39 +00:00
|
|
|
* @since 1.36
|
2021-05-05 18:13:13 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @param string|false $wikiId wiki ID or self::LOCAL for the local wiki
|
|
|
|
|
* @return UserIdentityValue
|
|
|
|
|
*/
|
|
|
|
|
public static function newAnonymous( string $name, $wikiId = self::LOCAL ): self {
|
|
|
|
|
return new self( 0, $name, $wikiId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create UserIdentity for a registered user.
|
|
|
|
|
*
|
2021-11-13 22:02:07 +00:00
|
|
|
* @since 1.36
|
2021-05-05 18:13:13 +00:00
|
|
|
* @param int $userId
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string|false $wikiId wiki ID or self::LOCAL for the local wiki
|
|
|
|
|
* @return UserIdentityValue
|
|
|
|
|
*/
|
|
|
|
|
public static function newRegistered( int $userId, string $name, $wikiId = self::LOCAL ): self {
|
|
|
|
|
Assert::parameter( $userId > 0, '$userId', 'must be greater than zero (user must exist)' );
|
|
|
|
|
return new self( $userId, $name, $wikiId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create UserIdentity for an external user with $prefix and $name
|
|
|
|
|
*
|
2021-11-13 22:02:07 +00:00
|
|
|
* @since 1.36
|
2021-05-05 18:13:13 +00:00
|
|
|
* @param string $prefix
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string|false $wikiId wiki ID or self::LOCAL for the local wiki
|
|
|
|
|
* @return UserIdentityValue
|
|
|
|
|
*/
|
|
|
|
|
public static function newExternal( string $prefix, string $name, $wikiId = self::LOCAL ): self {
|
|
|
|
|
// > is a standard separator for external users in the database, see ExternalUserNames
|
|
|
|
|
return new self( 0, "$prefix>$name", $wikiId );
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 04:25:24 +00:00
|
|
|
/**
|
|
|
|
|
* Get the ID of the wiki this UserIdentity belongs to.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.36
|
|
|
|
|
* @see RevisionRecord::getWikiId()
|
|
|
|
|
*
|
|
|
|
|
* @return string|false The wiki's logical name or self::LOCAL to indicate the local wiki
|
|
|
|
|
*/
|
|
|
|
|
public function getWikiId() {
|
|
|
|
|
return $this->wikiId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-06 00:54:54 +00:00
|
|
|
* The numerical user ID provided to the constructor.
|
2021-01-27 04:25:24 +00:00
|
|
|
*
|
2021-02-23 15:40:12 +00:00
|
|
|
* @param string|false $wikiId The wiki ID expected by the caller
|
2021-02-06 00:54:54 +00:00
|
|
|
* @return int The user ID. May be 0 for anonymous users or for users with no local account.
|
2021-01-27 04:25:24 +00:00
|
|
|
*
|
2017-08-27 15:29:18 +00:00
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function getId( $wikiId = self::LOCAL ): int {
|
2022-05-01 14:40:37 +00:00
|
|
|
$this->assertWiki( $wikiId );
|
2017-08-27 15:29:18 +00:00
|
|
|
return $this->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string The user's logical name. May be an IPv4 or IPv6 address for anonymous users.
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function getName(): string {
|
2017-08-27 15:29:18 +00:00
|
|
|
return $this->name;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-12 17:12:29 +00:00
|
|
|
/**
|
2021-02-20 21:51:37 +00:00
|
|
|
* @deprecated since 1.36, use ActorNormalization::acquireActorId instead.
|
|
|
|
|
*
|
2021-02-15 18:58:09 +00:00
|
|
|
* @param string|false $wikiId
|
2021-01-27 04:25:24 +00:00
|
|
|
*
|
2021-02-15 18:58:09 +00:00
|
|
|
* @return int always 0.
|
2017-09-12 17:12:29 +00:00
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function getActorId( $wikiId = self::LOCAL ): int {
|
2021-02-15 18:58:09 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.36' );
|
|
|
|
|
return 0;
|
2017-09-12 17:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
2018-05-08 14:31:03 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.32
|
|
|
|
|
*
|
2021-05-27 16:56:43 +00:00
|
|
|
* @param UserIdentity|null $user
|
2018-05-08 14:31:03 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function equals( ?UserIdentity $user ): bool {
|
2021-05-27 16:56:43 +00:00
|
|
|
if ( !$user ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-05-08 14:31:03 +00:00
|
|
|
// XXX it's not clear whether central ID providers are supposed to obey this
|
|
|
|
|
return $this->getName() === $user->getName();
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 11:07:18 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.34
|
|
|
|
|
*
|
|
|
|
|
* @return bool True if user is registered on this wiki, i.e., has a user ID. False if user is
|
|
|
|
|
* anonymous or has no local account (which can happen when importing). This is equivalent to
|
|
|
|
|
* getId() != 0 and is provided for code readability.
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function isRegistered(): bool {
|
2021-05-05 18:13:13 +00:00
|
|
|
return $this->getId( $this->wikiId ) != 0;
|
2019-04-28 11:07:18 +00:00
|
|
|
}
|
2021-01-22 19:51:43 +00:00
|
|
|
|
2021-02-17 20:45:31 +00:00
|
|
|
public function __toString(): string {
|
|
|
|
|
return $this->getName();
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-27 15:29:18 +00:00
|
|
|
}
|