Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / session / UserInfo.php
blobc01b9eca9d4ed3eb030ca53cc3bacdf4be72100a
1 <?php
2 /**
3 * MediaWiki session user info
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.
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.
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
20 * @file
21 * @ingroup Session
24 namespace MediaWiki\Session;
26 use User;
28 /**
29 * Object holding data about a session's user
31 * In general, this class exists for two purposes:
32 * - User doesn't distinguish between "anonymous user" and "non-anonymous user
33 * that doesn't exist locally", while we do need to.
34 * - We also need the "verified" property described below; tracking it via
35 * another data item to SessionInfo's constructor makes things much more
36 * confusing.
38 * A UserInfo may be "verified". This indicates that the creator knows that the
39 * request really comes from that user, whether that's by validating OAuth
40 * credentials, SSL client certificates, or by having both the user ID and
41 * token available from cookies.
43 * An "unverified" UserInfo should be used when it's not possible to
44 * authenticate the user, e.g. the user ID cookie is set but the user Token
45 * cookie isn't. If the Token is available but doesn't match, don't return a
46 * UserInfo at all.
48 * @ingroup Session
49 * @since 1.27
51 final class UserInfo {
52 private $verified = false;
54 /** @var User|null */
55 private $user = null;
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;
67 /**
68 * Create an instance for an anonymous (i.e. not logged in) user
70 * Logged-out users are always "verified".
72 * @return UserInfo
74 public static function newAnonymous() {
75 return new self( null, true );
78 /**
79 * Create an instance for a logged-in user by ID
80 * @param int $id User ID
81 * @param bool $verified True if the user is verified
82 * @return UserInfo
84 public static function newFromId( $id, $verified = false ) {
85 $user = User::newFromId( $id );
87 // Ensure the ID actually exists
88 $user->load();
89 if ( $user->isAnon() ) {
90 throw new \InvalidArgumentException( 'Invalid ID' );
93 return new self( $user, $verified );
96 /**
97 * Create an instance for a logged-in user by name
98 * @param string $name User name (need not exist locally)
99 * @param bool $verified True if the user is verified
100 * @return UserInfo
102 public static function newFromName( $name, $verified = false ) {
103 $user = User::newFromName( $name, 'usable' );
104 if ( !$user ) {
105 throw new \InvalidArgumentException( 'Invalid user name' );
107 return new self( $user, $verified );
111 * Create an instance from an existing User object
112 * @param User $user (need not exist locally)
113 * @param bool $verified True if the user is verified
114 * @return UserInfo
116 public static function newFromUser( User $user, $verified = false ) {
117 return new self( $user, $verified );
121 * Return whether this is an anonymous user
122 * @return bool
124 public function isAnon() {
125 return $this->user === null;
129 * Return whether this represents a verified user
130 * @return bool
132 public function isVerified() {
133 return $this->verified;
137 * Return the user ID
138 * @note Do not use this to test for anonymous users!
139 * @return int
141 public function getId() {
142 return $this->user === null ? 0 : $this->user->getId();
146 * Return the user name
147 * @return string|null
149 public function getName() {
150 return $this->user === null ? null : $this->user->getName();
154 * Return the user token
155 * @return string
157 public function getToken() {
158 return $this->user === null || $this->user->getId() === 0 ? '' : $this->user->getToken( false );
162 * Return a User object
163 * @return User
165 public function getUser() {
166 return $this->user === null ? new User : $this->user;
170 * Return a verified version of this object
171 * @return UserInfo
173 public function verified() {
174 return $this->verified ? $this : new self( $this->user, true );
177 public function __toString() {
178 if ( $this->user === null ) {
179 return '<anon>';
181 return '<' .
182 ( $this->verified ? '+' : '-' ) . ':' .
183 $this->getId() . ':' . $this->getName() .
184 '>';