MediaWiki REL1_39
UserInfo.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Session;
25
28use User;
29
53final class UserInfo {
55 private $verified = false;
56
58 private $user = null;
59
60 private function __construct( ?User $user, $verified ) {
61 $userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
62 if ( $user && $user->isAnon() && !$userNameUtils->isUsable( $user->getName() ) ) {
63 $this->verified = true;
64 $this->user = null;
65 } else {
66 $this->verified = $verified;
67 $this->user = $user;
68 }
69 }
70
78 public static function newAnonymous() {
79 return new self( null, true );
80 }
81
88 public static function newFromId( $id, $verified = false ) {
89 $user = MediaWikiServices::getInstance()->getUserFactory()->newFromId( (int)$id );
90
91 // Ensure the ID actually exists
92 $user->load();
93 if ( $user->isAnon() ) {
94 throw new \InvalidArgumentException( 'Invalid ID' );
95 }
96
97 return new self( $user, $verified );
98 }
99
106 public static function newFromName( $name, $verified = false ) {
107 $user = MediaWikiServices::getInstance()->getUserFactory()->newFromName(
108 (string)$name,
109 UserRigorOptions::RIGOR_USABLE
110 );
111 if ( !$user ) {
112 throw new \InvalidArgumentException( 'Invalid user name' );
113 }
114 return new self( $user, $verified );
115 }
116
123 public static function newFromUser( User $user, $verified = false ) {
124 return new self( $user, $verified );
125 }
126
131 public function isAnon() {
132 return $this->user === null;
133 }
134
139 public function isVerified() {
140 return $this->verified;
141 }
142
148 public function getId() {
149 return $this->user === null ? 0 : $this->user->getId();
150 }
151
156 public function getName() {
157 return $this->user === null ? null : $this->user->getName();
158 }
159
164 public function getToken() {
165 return $this->user === null || $this->user->getId() === 0 ? '' : $this->user->getToken( false );
166 }
167
172 public function getUser() {
173 return $this->user ?? new User;
174 }
175
180 public function verified() {
181 return $this->verified ? $this : new self( $this->user, true );
182 }
183
184 public function __toString() {
185 if ( $this->user === null ) {
186 return '<anon>';
187 }
188 return '<' .
189 ( $this->verified ? '+' : '-' ) . ':' .
190 $this->getId() . ':' . $this->getName() .
191 '>';
192 }
193
194}
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Object holding data about a session's user.
Definition UserInfo.php:53
getId()
Return the user ID.
Definition UserInfo.php:148
static newFromName( $name, $verified=false)
Create an instance for a logged-in user by name.
Definition UserInfo.php:106
isVerified()
Return whether this represents a verified user.
Definition UserInfo.php:139
static newAnonymous()
Create an instance for an anonymous (i.e.
Definition UserInfo.php:78
isAnon()
Return whether this is an anonymous user.
Definition UserInfo.php:131
getUser()
Return a User object.
Definition UserInfo.php:172
getName()
Return the user name.
Definition UserInfo.php:156
static newFromUser(User $user, $verified=false)
Create an instance from an existing User object.
Definition UserInfo.php:123
static newFromId( $id, $verified=false)
Create an instance for a logged-in user by ID.
Definition UserInfo.php:88
verified()
Return a verified version of this object.
Definition UserInfo.php:180
getToken()
Return the user token.
Definition UserInfo.php:164
internal since 1.36
Definition User.php:70
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:1679
static newFromName( $name, $validate='valid')
Definition User.php:598
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:639
load( $flags=self::READ_NORMAL)
Load the user table data for this object from the source given by mFrom.
Definition User.php:367
isAnon()
Get whether the user is anonymous.
Definition User.php:2319
Shared interface for rigor levels when dealing with User methods.
return true
Definition router.php:92