MediaWiki  1.32.5
UserInfo.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Session;
25 
26 use User;
27 
51 final class UserInfo {
52  private $verified = false;
53 
55  private $user = null;
56 
57  private function __construct( User $user = null, $verified ) {
58  if ( $user && $user->isAnon() && !User::isUsableName( $user->getName() ) ) {
59  $this->verified = true;
60  $this->user = null;
61  } else {
62  $this->verified = $verified;
63  $this->user = $user;
64  }
65  }
66 
74  public static function newAnonymous() {
75  return new self( null, true );
76  }
77 
84  public static function newFromId( $id, $verified = false ) {
85  $user = User::newFromId( $id );
86 
87  // Ensure the ID actually exists
88  $user->load();
89  if ( $user->isAnon() ) {
90  throw new \InvalidArgumentException( 'Invalid ID' );
91  }
92 
93  return new self( $user, $verified );
94  }
95 
102  public static function newFromName( $name, $verified = false ) {
103  $user = User::newFromName( $name, 'usable' );
104  if ( !$user ) {
105  throw new \InvalidArgumentException( 'Invalid user name' );
106  }
107  return new self( $user, $verified );
108  }
109 
116  public static function newFromUser( User $user, $verified = false ) {
117  return new self( $user, $verified );
118  }
119 
124  public function isAnon() {
125  return $this->user === null;
126  }
127 
132  public function isVerified() {
133  return $this->verified;
134  }
135 
141  public function getId() {
142  return $this->user === null ? 0 : $this->user->getId();
143  }
144 
149  public function getName() {
150  return $this->user === null ? null : $this->user->getName();
151  }
152 
157  public function getToken() {
158  return $this->user === null || $this->user->getId() === 0 ? '' : $this->user->getToken( false );
159  }
160 
165  public function getUser() {
166  return $this->user === null ? new User : $this->user;
167  }
168 
173  public function verified() {
174  return $this->verified ? $this : new self( $this->user, true );
175  }
176 
177  public function __toString() {
178  if ( $this->user === null ) {
179  return '<anon>';
180  }
181  return '<' .
182  ( $this->verified ? '+' : '-' ) . ':' .
183  $this->getId() . ':' . $this->getName() .
184  '>';
185  }
186 
187 }
MediaWiki\Session\UserInfo\newAnonymous
static newAnonymous()
Create an instance for an anonymous (i.e.
Definition: UserInfo.php:74
User\load
load( $flags=self::READ_NORMAL)
Load the user table data for this object from the source given by mFrom.
Definition: User.php:364
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:615
User\isAnon
isAnon()
Get whether the user is anonymous.
Definition: User.php:3801
MediaWiki\Session\UserInfo
Object holding data about a session's user.
Definition: UserInfo.php:51
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:592
MediaWiki\Session\UserInfo\isVerified
isVerified()
Return whether this represents a verified user.
Definition: UserInfo.php:132
User
User
Definition: All_system_messages.txt:425
MediaWiki\Session\UserInfo\$verified
$verified
Definition: UserInfo.php:52
MediaWiki\Session\UserInfo\newFromUser
static newFromUser(User $user, $verified=false)
Create an instance from an existing User object.
Definition: UserInfo.php:116
MediaWiki\Session\UserInfo\$user
User null $user
Definition: UserInfo.php:55
MediaWiki\Session\UserInfo\__construct
__construct(User $user=null, $verified)
Definition: UserInfo.php:57
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Session\UserInfo\getName
getName()
Return the user name.
Definition: UserInfo.php:149
user
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Definition: distributors.txt:9
MediaWiki\Session\UserInfo\__toString
__toString()
Definition: UserInfo.php:177
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
MediaWiki\Session\UserInfo\getUser
getUser()
Return a User object.
Definition: UserInfo.php:165
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
MediaWiki\Session\UserInfo\newFromName
static newFromName( $name, $verified=false)
Create an instance for a logged-in user by name.
Definition: UserInfo.php:102
MediaWiki\Session\UserInfo\getId
getId()
Return the user ID.
Definition: UserInfo.php:141
MediaWiki\Session\UserInfo\getToken
getToken()
Return the user token.
Definition: UserInfo.php:157
MediaWiki\Session\UserInfo\newFromId
static newFromId( $id, $verified=false)
Create an instance for a logged-in user by ID.
Definition: UserInfo.php:84
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:2044
User\isUsableName
static isUsableName( $name)
Usernames which fail to pass this function will be blocked from user login and new account registrati...
Definition: User.php:1046
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:47
MediaWiki\Session\UserInfo\verified
verified()
Return a verified version of this object.
Definition: UserInfo.php:173
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2462
MediaWiki\Session\UserInfo\isAnon
isAnon()
Return whether this is an anonymous user.
Definition: UserInfo.php:124