Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class User implements IDBAccessObject { |
||
48 | /** |
||
49 | * @const int Number of characters in user_token field. |
||
50 | */ |
||
51 | const TOKEN_LENGTH = 32; |
||
52 | |||
53 | /** |
||
54 | * @const string An invalid value for user_token |
||
55 | */ |
||
56 | const INVALID_TOKEN = '*** INVALID ***'; |
||
57 | |||
58 | /** |
||
59 | * Global constant made accessible as class constants so that autoloader |
||
60 | * magic can be used. |
||
61 | * @deprecated since 1.27, use \MediaWiki\Session\Token::SUFFIX |
||
62 | */ |
||
63 | const EDIT_TOKEN_SUFFIX = EDIT_TOKEN_SUFFIX; |
||
|
|||
64 | |||
65 | /** |
||
66 | * @const int Serialized record version. |
||
67 | */ |
||
68 | const VERSION = 10; |
||
69 | |||
70 | /** |
||
71 | * Exclude user options that are set to their default value. |
||
72 | * @since 1.25 |
||
73 | */ |
||
74 | const GETOPTIONS_EXCLUDE_DEFAULTS = 1; |
||
75 | |||
76 | /** |
||
77 | * @since 1.27 |
||
78 | */ |
||
79 | const CHECK_USER_RIGHTS = true; |
||
80 | |||
81 | /** |
||
82 | * @since 1.27 |
||
83 | */ |
||
84 | const IGNORE_USER_RIGHTS = false; |
||
85 | |||
86 | /** |
||
87 | * Array of Strings List of member variables which are saved to the |
||
88 | * shared cache (memcached). Any operation which changes the |
||
89 | * corresponding database fields must call a cache-clearing function. |
||
90 | * @showinitializer |
||
91 | */ |
||
92 | protected static $mCacheVars = [ |
||
93 | // user table |
||
94 | 'mId', |
||
95 | 'mName', |
||
96 | 'mRealName', |
||
97 | 'mEmail', |
||
98 | 'mTouched', |
||
99 | 'mToken', |
||
100 | 'mEmailAuthenticated', |
||
101 | 'mEmailToken', |
||
102 | 'mEmailTokenExpires', |
||
103 | 'mRegistration', |
||
104 | 'mEditCount', |
||
105 | // user_groups table |
||
106 | 'mGroups', |
||
107 | // user_properties table |
||
108 | 'mOptionOverrides', |
||
109 | ]; |
||
110 | |||
111 | /** |
||
112 | * Array of Strings Core rights. |
||
113 | * Each of these should have a corresponding message of the form |
||
114 | * "right-$right". |
||
115 | * @showinitializer |
||
116 | */ |
||
117 | protected static $mCoreRights = [ |
||
118 | 'apihighlimits', |
||
119 | 'applychangetags', |
||
120 | 'autoconfirmed', |
||
121 | 'autocreateaccount', |
||
122 | 'autopatrol', |
||
123 | 'bigdelete', |
||
124 | 'block', |
||
125 | 'blockemail', |
||
126 | 'bot', |
||
127 | 'browsearchive', |
||
128 | 'changetags', |
||
129 | 'createaccount', |
||
130 | 'createpage', |
||
131 | 'createtalk', |
||
132 | 'delete', |
||
133 | 'deletechangetags', |
||
134 | 'deletedhistory', |
||
135 | 'deletedtext', |
||
136 | 'deletelogentry', |
||
137 | 'deleterevision', |
||
138 | 'edit', |
||
139 | 'editcontentmodel', |
||
140 | 'editinterface', |
||
141 | 'editprotected', |
||
142 | 'editmyoptions', |
||
143 | 'editmyprivateinfo', |
||
144 | 'editmyusercss', |
||
145 | 'editmyuserjs', |
||
146 | 'editmywatchlist', |
||
147 | 'editsemiprotected', |
||
148 | 'editusercssjs', # deprecated |
||
149 | 'editusercss', |
||
150 | 'edituserjs', |
||
151 | 'hideuser', |
||
152 | 'import', |
||
153 | 'importupload', |
||
154 | 'ipblock-exempt', |
||
155 | 'managechangetags', |
||
156 | 'markbotedits', |
||
157 | 'mergehistory', |
||
158 | 'minoredit', |
||
159 | 'move', |
||
160 | 'movefile', |
||
161 | 'move-categorypages', |
||
162 | 'move-rootuserpages', |
||
163 | 'move-subpages', |
||
164 | 'nominornewtalk', |
||
165 | 'noratelimit', |
||
166 | 'override-export-depth', |
||
167 | 'pagelang', |
||
168 | 'passwordreset', |
||
169 | 'patrol', |
||
170 | 'patrolmarks', |
||
171 | 'protect', |
||
172 | 'purge', |
||
173 | 'read', |
||
174 | 'reupload', |
||
175 | 'reupload-own', |
||
176 | 'reupload-shared', |
||
177 | 'rollback', |
||
178 | 'sendemail', |
||
179 | 'siteadmin', |
||
180 | 'suppressionlog', |
||
181 | 'suppressredirect', |
||
182 | 'suppressrevision', |
||
183 | 'unblockself', |
||
184 | 'undelete', |
||
185 | 'unwatchedpages', |
||
186 | 'upload', |
||
187 | 'upload_by_url', |
||
188 | 'userrights', |
||
189 | 'userrights-interwiki', |
||
190 | 'viewmyprivateinfo', |
||
191 | 'viewmywatchlist', |
||
192 | 'viewsuppressed', |
||
193 | 'writeapi', |
||
194 | ]; |
||
195 | |||
196 | /** |
||
197 | * String Cached results of getAllRights() |
||
198 | */ |
||
199 | protected static $mAllRights = false; |
||
200 | |||
201 | /** Cache variables */ |
||
202 | // @{ |
||
203 | /** @var int */ |
||
204 | public $mId; |
||
205 | /** @var string */ |
||
206 | public $mName; |
||
207 | /** @var string */ |
||
208 | public $mRealName; |
||
209 | |||
210 | /** @var string */ |
||
211 | public $mEmail; |
||
212 | /** @var string TS_MW timestamp from the DB */ |
||
213 | public $mTouched; |
||
214 | /** @var string TS_MW timestamp from cache */ |
||
215 | protected $mQuickTouched; |
||
216 | /** @var string */ |
||
217 | protected $mToken; |
||
218 | /** @var string */ |
||
219 | public $mEmailAuthenticated; |
||
220 | /** @var string */ |
||
221 | protected $mEmailToken; |
||
222 | /** @var string */ |
||
223 | protected $mEmailTokenExpires; |
||
224 | /** @var string */ |
||
225 | protected $mRegistration; |
||
226 | /** @var int */ |
||
227 | protected $mEditCount; |
||
228 | /** @var array */ |
||
229 | public $mGroups; |
||
230 | /** @var array */ |
||
231 | protected $mOptionOverrides; |
||
232 | // @} |
||
233 | |||
234 | /** |
||
235 | * Bool Whether the cache variables have been loaded. |
||
236 | */ |
||
237 | // @{ |
||
238 | public $mOptionsLoaded; |
||
239 | |||
240 | /** |
||
241 | * Array with already loaded items or true if all items have been loaded. |
||
242 | */ |
||
243 | protected $mLoadedItems = []; |
||
244 | // @} |
||
245 | |||
246 | /** |
||
247 | * String Initialization data source if mLoadedItems!==true. May be one of: |
||
248 | * - 'defaults' anonymous user initialised from class defaults |
||
249 | * - 'name' initialise from mName |
||
250 | * - 'id' initialise from mId |
||
251 | * - 'session' log in from session if possible |
||
252 | * |
||
253 | * Use the User::newFrom*() family of functions to set this. |
||
254 | */ |
||
255 | public $mFrom; |
||
256 | |||
257 | /** |
||
258 | * Lazy-initialized variables, invalidated with clearInstanceCache |
||
259 | */ |
||
260 | protected $mNewtalk; |
||
261 | /** @var string */ |
||
262 | protected $mDatePreference; |
||
263 | /** @var string */ |
||
264 | public $mBlockedby; |
||
265 | /** @var string */ |
||
266 | protected $mHash; |
||
267 | /** @var array */ |
||
268 | public $mRights; |
||
269 | /** @var string */ |
||
270 | protected $mBlockreason; |
||
271 | /** @var array */ |
||
272 | protected $mEffectiveGroups; |
||
273 | /** @var array */ |
||
274 | protected $mImplicitGroups; |
||
275 | /** @var array */ |
||
276 | protected $mFormerGroups; |
||
277 | /** @var Block */ |
||
278 | protected $mGlobalBlock; |
||
279 | /** @var bool */ |
||
280 | protected $mLocked; |
||
281 | /** @var bool */ |
||
282 | public $mHideName; |
||
283 | /** @var array */ |
||
284 | public $mOptions; |
||
285 | |||
286 | /** |
||
287 | * @var WebRequest |
||
288 | */ |
||
289 | private $mRequest; |
||
290 | |||
291 | /** @var Block */ |
||
292 | public $mBlock; |
||
293 | |||
294 | /** @var bool */ |
||
295 | protected $mAllowUsertalk; |
||
296 | |||
297 | /** @var Block */ |
||
298 | private $mBlockedFromCreateAccount = false; |
||
299 | |||
300 | /** @var integer User::READ_* constant bitfield used to load data */ |
||
301 | protected $queryFlagsUsed = self::READ_NORMAL; |
||
302 | |||
303 | public static $idCacheByName = []; |
||
304 | |||
305 | /** |
||
306 | * Lightweight constructor for an anonymous user. |
||
307 | * Use the User::newFrom* factory functions for other kinds of users. |
||
308 | * |
||
309 | * @see newFromName() |
||
310 | * @see newFromId() |
||
311 | * @see newFromConfirmationCode() |
||
312 | * @see newFromSession() |
||
313 | * @see newFromRow() |
||
314 | */ |
||
315 | public function __construct() { |
||
318 | |||
319 | /** |
||
320 | * @return string |
||
321 | */ |
||
322 | public function __toString() { |
||
325 | |||
326 | /** |
||
327 | * Test if it's safe to load this User object. |
||
328 | * |
||
329 | * You should typically check this before using $wgUser or |
||
330 | * RequestContext::getUser in a method that might be called before the |
||
331 | * system has been fully initialized. If the object is unsafe, you should |
||
332 | * use an anonymous user: |
||
333 | * \code |
||
334 | * $user = $wgUser->isSafeToLoad() ? $wgUser : new User; |
||
335 | * \endcode |
||
336 | * |
||
337 | * @since 1.27 |
||
338 | * @return bool |
||
339 | */ |
||
340 | public function isSafeToLoad() { |
||
351 | |||
352 | /** |
||
353 | * Load the user table data for this object from the source given by mFrom. |
||
354 | * |
||
355 | * @param integer $flags User::READ_* constant bitfield |
||
356 | */ |
||
357 | public function load( $flags = self::READ_NORMAL ) { |
||
414 | |||
415 | /** |
||
416 | * Load user table data, given mId has already been set. |
||
417 | * @param integer $flags User::READ_* constant bitfield |
||
418 | * @return bool False if the ID does not exist, true otherwise |
||
419 | */ |
||
420 | public function loadFromId( $flags = self::READ_NORMAL ) { |
||
421 | if ( $this->mId == 0 ) { |
||
422 | // Anonymous users are not in the database (don't need cache) |
||
423 | $this->loadDefaults(); |
||
424 | return false; |
||
425 | } |
||
426 | |||
427 | // Try cache (unless this needs data from the master DB). |
||
428 | // NOTE: if this thread called saveSettings(), the cache was cleared. |
||
429 | $latest = DBAccessObjectUtils::hasFlags( $flags, self::READ_LATEST ); |
||
430 | if ( $latest ) { |
||
431 | if ( !$this->loadFromDatabase( $flags ) ) { |
||
432 | // Can't load from ID |
||
433 | return false; |
||
434 | } |
||
435 | } else { |
||
436 | $this->loadFromCache(); |
||
437 | } |
||
438 | |||
439 | $this->mLoadedItems = true; |
||
440 | $this->queryFlagsUsed = $flags; |
||
441 | |||
442 | return true; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * @since 1.27 |
||
447 | * @param string $wikiId |
||
448 | * @param integer $userId |
||
449 | */ |
||
450 | public static function purge( $wikiId, $userId ) { |
||
455 | |||
456 | /** |
||
457 | * @since 1.27 |
||
458 | * @param WANObjectCache $cache |
||
459 | * @return string |
||
460 | */ |
||
461 | protected function getCacheKey( WANObjectCache $cache ) { |
||
464 | |||
465 | /** |
||
466 | * Load user data from shared cache, given mId has already been set. |
||
467 | * |
||
468 | * @return bool True |
||
469 | * @since 1.25 |
||
470 | */ |
||
471 | protected function loadFromCache() { |
||
472 | $cache = ObjectCache::getMainWANInstance(); |
||
473 | $data = $cache->getWithSetCallback( |
||
474 | $this->getCacheKey( $cache ), |
||
475 | $cache::TTL_HOUR, |
||
476 | function ( $oldValue, &$ttl, array &$setOpts ) use ( $cache ) { |
||
477 | $setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) ); |
||
478 | wfDebug( "User: cache miss for user {$this->mId}\n" ); |
||
479 | |||
480 | $this->loadFromDatabase( self::READ_NORMAL ); |
||
481 | $this->loadGroups(); |
||
482 | $this->loadOptions(); |
||
483 | |||
484 | $data = []; |
||
485 | foreach ( self::$mCacheVars as $name ) { |
||
486 | $data[$name] = $this->$name; |
||
487 | } |
||
488 | |||
489 | $ttl = $cache->adaptiveTTL( wfTimestamp( TS_UNIX, $this->mTouched ), $ttl ); |
||
490 | |||
491 | return $data; |
||
492 | |||
493 | }, |
||
494 | [ 'pcTTL' => $cache::TTL_PROC_LONG, 'version' => self::VERSION ] |
||
495 | ); |
||
496 | |||
497 | // Restore from cache |
||
498 | foreach ( self::$mCacheVars as $name ) { |
||
499 | $this->$name = $data[$name]; |
||
500 | } |
||
501 | |||
502 | return true; |
||
503 | } |
||
504 | |||
505 | /** @name newFrom*() static factory methods */ |
||
506 | // @{ |
||
507 | |||
508 | /** |
||
509 | * Static factory method for creation from username. |
||
510 | * |
||
511 | * This is slightly less efficient than newFromId(), so use newFromId() if |
||
512 | * you have both an ID and a name handy. |
||
513 | * |
||
514 | * @param string $name Username, validated by Title::newFromText() |
||
515 | * @param string|bool $validate Validate username. Takes the same parameters as |
||
516 | * User::getCanonicalName(), except that true is accepted as an alias |
||
517 | * for 'valid', for BC. |
||
518 | * |
||
519 | * @return User|bool User object, or false if the username is invalid |
||
520 | * (e.g. if it contains illegal characters or is an IP address). If the |
||
521 | * username is not present in the database, the result will be a user object |
||
522 | * with a name, zero user ID and default settings. |
||
523 | */ |
||
524 | public static function newFromName( $name, $validate = 'valid' ) { |
||
540 | |||
541 | /** |
||
542 | * Static factory method for creation from a given user ID. |
||
543 | * |
||
544 | * @param int $id Valid user ID |
||
545 | * @return User The corresponding User object |
||
546 | */ |
||
547 | public static function newFromId( $id ) { |
||
554 | |||
555 | /** |
||
556 | * Factory method to fetch whichever user has a given email confirmation code. |
||
557 | * This code is generated when an account is created or its e-mail address |
||
558 | * has changed. |
||
559 | * |
||
560 | * If the code is invalid or has expired, returns NULL. |
||
561 | * |
||
562 | * @param string $code Confirmation code |
||
563 | * @param int $flags User::READ_* bitfield |
||
564 | * @return User|null |
||
565 | */ |
||
566 | public static function newFromConfirmationCode( $code, $flags = 0 ) { |
||
582 | |||
583 | /** |
||
584 | * Create a new user object using data from session. If the login |
||
585 | * credentials are invalid, the result is an anonymous user. |
||
586 | * |
||
587 | * @param WebRequest|null $request Object to use; $wgRequest will be used if omitted. |
||
588 | * @return User |
||
589 | */ |
||
590 | public static function newFromSession( WebRequest $request = null ) { |
||
596 | |||
597 | /** |
||
598 | * Create a new user object from a user row. |
||
599 | * The row should have the following fields from the user table in it: |
||
600 | * - either user_name or user_id to load further data if needed (or both) |
||
601 | * - user_real_name |
||
602 | * - all other fields (email, etc.) |
||
603 | * It is useless to provide the remaining fields if either user_id, |
||
604 | * user_name and user_real_name are not provided because the whole row |
||
605 | * will be loaded once more from the database when accessing them. |
||
606 | * |
||
607 | * @param stdClass $row A row from the user table |
||
608 | * @param array $data Further data to load into the object (see User::loadFromRow for valid keys) |
||
609 | * @return User |
||
610 | */ |
||
611 | public static function newFromRow( $row, $data = null ) { |
||
616 | |||
617 | /** |
||
618 | * Static factory method for creation of a "system" user from username. |
||
619 | * |
||
620 | * A "system" user is an account that's used to attribute logged actions |
||
621 | * taken by MediaWiki itself, as opposed to a bot or human user. Examples |
||
622 | * might include the 'Maintenance script' or 'Conversion script' accounts |
||
623 | * used by various scripts in the maintenance/ directory or accounts such |
||
624 | * as 'MediaWiki message delivery' used by the MassMessage extension. |
||
625 | * |
||
626 | * This can optionally create the user if it doesn't exist, and "steal" the |
||
627 | * account if it does exist. |
||
628 | * |
||
629 | * "Stealing" an existing user is intended to make it impossible for normal |
||
630 | * authentication processes to use the account, effectively disabling the |
||
631 | * account for normal use: |
||
632 | * - Email is invalidated, to prevent account recovery by emailing a |
||
633 | * temporary password and to disassociate the account from the existing |
||
634 | * human. |
||
635 | * - The token is set to a magic invalid value, to kill existing sessions |
||
636 | * and to prevent $this->setToken() calls from resetting the token to a |
||
637 | * valid value. |
||
638 | * - SessionManager is instructed to prevent new sessions for the user, to |
||
639 | * do things like deauthorizing OAuth consumers. |
||
640 | * - AuthManager is instructed to revoke access, to invalidate or remove |
||
641 | * passwords and other credentials. |
||
642 | * |
||
643 | * @param string $name Username |
||
644 | * @param array $options Options are: |
||
645 | * - validate: As for User::getCanonicalName(), default 'valid' |
||
646 | * - create: Whether to create the user if it doesn't already exist, default true |
||
647 | * - steal: Whether to "disable" the account for normal use if it already |
||
648 | * exists, default false |
||
649 | * @return User|null |
||
650 | * @since 1.27 |
||
651 | */ |
||
652 | public static function newSystemUser( $name, $options = [] ) { |
||
699 | |||
700 | // @} |
||
701 | |||
702 | /** |
||
703 | * Get the username corresponding to a given user ID |
||
704 | * @param int $id User ID |
||
705 | * @return string|bool The corresponding username |
||
706 | */ |
||
707 | public static function whoIs( $id ) { |
||
710 | |||
711 | /** |
||
712 | * Get the real name of a user given their user ID |
||
713 | * |
||
714 | * @param int $id User ID |
||
715 | * @return string|bool The corresponding user's real name |
||
716 | */ |
||
717 | public static function whoIsReal( $id ) { |
||
720 | |||
721 | /** |
||
722 | * Get database id given a user name |
||
723 | * @param string $name Username |
||
724 | * @param integer $flags User::READ_* constant bitfield |
||
725 | * @return int|null The corresponding user's ID, or null if user is nonexistent |
||
726 | */ |
||
727 | public static function idFromName( $name, $flags = self::READ_NORMAL ) { |
||
763 | |||
764 | /** |
||
765 | * Reset the cache used in idFromName(). For use in tests. |
||
766 | */ |
||
767 | public static function resetIdByNameCache() { |
||
770 | |||
771 | /** |
||
772 | * Does the string match an anonymous IP address? |
||
773 | * |
||
774 | * This function exists for username validation, in order to reject |
||
775 | * usernames which are similar in form to IP addresses. Strings such |
||
776 | * as 300.300.300.300 will return true because it looks like an IP |
||
777 | * address, despite not being strictly valid. |
||
778 | * |
||
779 | * We match "\d{1,3}\.\d{1,3}\.\d{1,3}\.xxx" as an anonymous IP |
||
780 | * address because the usemod software would "cloak" anonymous IP |
||
781 | * addresses like this, if we allowed accounts like this to be created |
||
782 | * new users could get the old edits of these anonymous users. |
||
783 | * |
||
784 | * @param string $name Name to match |
||
785 | * @return bool |
||
786 | */ |
||
787 | public static function isIP( $name ) { |
||
791 | |||
792 | /** |
||
793 | * Is the input a valid username? |
||
794 | * |
||
795 | * Checks if the input is a valid username, we don't want an empty string, |
||
796 | * an IP address, anything that contains slashes (would mess up subpages), |
||
797 | * is longer than the maximum allowed username size or doesn't begin with |
||
798 | * a capital letter. |
||
799 | * |
||
800 | * @param string $name Name to match |
||
801 | * @return bool |
||
802 | */ |
||
803 | public static function isValidUserName( $name ) { |
||
840 | |||
841 | /** |
||
842 | * Usernames which fail to pass this function will be blocked |
||
843 | * from user login and new account registrations, but may be used |
||
844 | * internally by batch processes. |
||
845 | * |
||
846 | * If an account already exists in this form, login will be blocked |
||
847 | * by a failure to pass this function. |
||
848 | * |
||
849 | * @param string $name Name to match |
||
850 | * @return bool |
||
851 | */ |
||
852 | public static function isUsableName( $name ) { |
||
876 | |||
877 | /** |
||
878 | * Return the users who are members of the given group(s). In case of multiple groups, |
||
879 | * users who are members of at least one of them are returned. |
||
880 | * |
||
881 | * @param string|array $groups A single group name or an array of group names |
||
882 | * @param int $limit Max number of users to return. The actual limit will never exceed 5000 |
||
883 | * records; larger values are ignored. |
||
884 | * @param int $after ID the user to start after |
||
885 | * @return UserArrayFromResult |
||
886 | */ |
||
887 | public static function findUsersByGroup( $groups, $limit = 5000, $after = null ) { |
||
914 | |||
915 | /** |
||
916 | * Usernames which fail to pass this function will be blocked |
||
917 | * from new account registrations, but may be used internally |
||
918 | * either by batch processes or by user accounts which have |
||
919 | * already been created. |
||
920 | * |
||
921 | * Additional blacklisting may be added here rather than in |
||
922 | * isValidUserName() to avoid disrupting existing accounts. |
||
923 | * |
||
924 | * @param string $name String to match |
||
925 | * @return bool |
||
926 | */ |
||
927 | public static function isCreatableName( $name ) { |
||
950 | |||
951 | /** |
||
952 | * Is the input a valid password for this user? |
||
953 | * |
||
954 | * @param string $password Desired password |
||
955 | * @return bool |
||
956 | */ |
||
957 | public function isValidPassword( $password ) { |
||
961 | |||
962 | /** |
||
963 | * Given unvalidated password input, return error message on failure. |
||
964 | * |
||
965 | * @param string $password Desired password |
||
966 | * @return bool|string|array True on success, string or array of error message on failure |
||
967 | */ |
||
968 | public function getPasswordValidity( $password ) { |
||
986 | |||
987 | /** |
||
988 | * Check if this is a valid password for this user |
||
989 | * |
||
990 | * Create a Status object based on the password's validity. |
||
991 | * The Status should be set to fatal if the user should not |
||
992 | * be allowed to log in, and should have any errors that |
||
993 | * would block changing the password. |
||
994 | * |
||
995 | * If the return value of this is not OK, the password |
||
996 | * should not be checked. If the return value is not Good, |
||
997 | * the password can be checked, but the user should not be |
||
998 | * able to set their password to this. |
||
999 | * |
||
1000 | * @param string $password Desired password |
||
1001 | * @param string $purpose one of 'login', 'create', 'reset' |
||
1002 | * @return Status |
||
1003 | * @since 1.23 |
||
1004 | */ |
||
1005 | public function checkPasswordValidity( $password, $purpose = 'login' ) { |
||
1031 | |||
1032 | /** |
||
1033 | * Given unvalidated user input, return a canonical username, or false if |
||
1034 | * the username is invalid. |
||
1035 | * @param string $name User input |
||
1036 | * @param string|bool $validate Type of validation to use: |
||
1037 | * - false No validation |
||
1038 | * - 'valid' Valid for batch processes |
||
1039 | * - 'usable' Valid for batch processes and login |
||
1040 | * - 'creatable' Valid for batch processes, login and account creation |
||
1041 | * |
||
1042 | * @throws InvalidArgumentException |
||
1043 | * @return bool|string |
||
1044 | */ |
||
1045 | public static function getCanonicalName( $name, $validate = 'valid' ) { |
||
1095 | |||
1096 | /** |
||
1097 | * Count the number of edits of a user |
||
1098 | * |
||
1099 | * @param int $uid User ID to check |
||
1100 | * @return int The user's edit count |
||
1101 | * |
||
1102 | * @deprecated since 1.21 in favour of User::getEditCount |
||
1103 | */ |
||
1104 | public static function edits( $uid ) { |
||
1109 | |||
1110 | /** |
||
1111 | * Return a random password. |
||
1112 | * |
||
1113 | * @deprecated since 1.27, use PasswordFactory::generateRandomPasswordString() |
||
1114 | * @return string New random password |
||
1115 | */ |
||
1116 | public static function randomPassword() { |
||
1120 | |||
1121 | /** |
||
1122 | * Set cached properties to default. |
||
1123 | * |
||
1124 | * @note This no longer clears uncached lazy-initialised properties; |
||
1125 | * the constructor does that instead. |
||
1126 | * |
||
1127 | * @param string|bool $name |
||
1128 | */ |
||
1129 | public function loadDefaults( $name = false ) { |
||
1154 | |||
1155 | /** |
||
1156 | * Return whether an item has been loaded. |
||
1157 | * |
||
1158 | * @param string $item Item to check. Current possibilities: |
||
1159 | * - id |
||
1160 | * - name |
||
1161 | * - realname |
||
1162 | * @param string $all 'all' to check if the whole object has been loaded |
||
1163 | * or any other string to check if only the item is available (e.g. |
||
1164 | * for optimisation) |
||
1165 | * @return bool |
||
1166 | */ |
||
1167 | public function isItemLoaded( $item, $all = 'all' ) { |
||
1171 | |||
1172 | /** |
||
1173 | * Set that an item has been loaded |
||
1174 | * |
||
1175 | * @param string $item |
||
1176 | */ |
||
1177 | protected function setItemLoaded( $item ) { |
||
1182 | |||
1183 | /** |
||
1184 | * Load user data from the session. |
||
1185 | * |
||
1186 | * @return bool True if the user is logged in, false otherwise. |
||
1187 | */ |
||
1188 | private function loadFromSession() { |
||
1211 | |||
1212 | /** |
||
1213 | * Load user and user_group data from the database. |
||
1214 | * $this->mId must be set, this is how the user is identified. |
||
1215 | * |
||
1216 | * @param integer $flags User::READ_* constant bitfield |
||
1217 | * @return bool True if the user exists, false if the user is anonymous |
||
1218 | */ |
||
1219 | public function loadFromDatabase( $flags = self::READ_LATEST ) { |
||
1256 | |||
1257 | /** |
||
1258 | * Initialize this object from a row from the user table. |
||
1259 | * |
||
1260 | * @param stdClass $row Row from the user table to load. |
||
1261 | * @param array $data Further user data to load into the object |
||
1262 | * |
||
1263 | * user_groups Array with groups out of the user_groups table |
||
1264 | * user_properties Array with properties out of the user_properties table |
||
1265 | */ |
||
1266 | protected function loadFromRow( $row, $data = null ) { |
||
1345 | |||
1346 | /** |
||
1347 | * Load the data for this user object from another user object. |
||
1348 | * |
||
1349 | * @param User $user |
||
1350 | */ |
||
1351 | protected function loadFromUserObject( $user ) { |
||
1357 | |||
1358 | /** |
||
1359 | * Load the groups from the database if they aren't already loaded. |
||
1360 | */ |
||
1361 | View Code Duplication | private function loadGroups() { |
|
1376 | |||
1377 | /** |
||
1378 | * Add the user to the group if he/she meets given criteria. |
||
1379 | * |
||
1380 | * Contrary to autopromotion by \ref $wgAutopromote, the group will be |
||
1381 | * possible to remove manually via Special:UserRights. In such case it |
||
1382 | * will not be re-added automatically. The user will also not lose the |
||
1383 | * group if they no longer meet the criteria. |
||
1384 | * |
||
1385 | * @param string $event Key in $wgAutopromoteOnce (each one has groups/criteria) |
||
1386 | * |
||
1387 | * @return array Array of groups the user has been promoted to. |
||
1388 | * |
||
1389 | * @see $wgAutopromoteOnce |
||
1390 | */ |
||
1391 | public function addAutopromoteOnceGroups( $event ) { |
||
1431 | |||
1432 | /** |
||
1433 | * Builds update conditions. Additional conditions may be added to $conditions to |
||
1434 | * protected against race conditions using a compare-and-set (CAS) mechanism |
||
1435 | * based on comparing $this->mTouched with the user_touched field. |
||
1436 | * |
||
1437 | * @param DatabaseBase $db |
||
1438 | * @param array $conditions WHERE conditions for use with DatabaseBase::update |
||
1439 | * @return array WHERE conditions for use with DatabaseBase::update |
||
1440 | */ |
||
1441 | protected function makeUpdateConditions( DatabaseBase $db, array $conditions ) { |
||
1449 | |||
1450 | /** |
||
1451 | * Bump user_touched if it didn't change since this object was loaded |
||
1452 | * |
||
1453 | * On success, the mTouched field is updated. |
||
1454 | * The user serialization cache is always cleared. |
||
1455 | * |
||
1456 | * @return bool Whether user_touched was actually updated |
||
1457 | * @since 1.26 |
||
1458 | */ |
||
1459 | protected function checkAndSetTouched() { |
||
1489 | |||
1490 | /** |
||
1491 | * Clear various cached data stored in this object. The cache of the user table |
||
1492 | * data (i.e. self::$mCacheVars) is not cleared unless $reloadFrom is given. |
||
1493 | * |
||
1494 | * @param bool|string $reloadFrom Reload user and user_groups table data from a |
||
1495 | * given source. May be "name", "id", "defaults", "session", or false for no reload. |
||
1496 | */ |
||
1497 | public function clearInstanceCache( $reloadFrom = false ) { |
||
1515 | |||
1516 | /** |
||
1517 | * Combine the language default options with any site-specific options |
||
1518 | * and add the default language variants. |
||
1519 | * |
||
1520 | * @return array Array of String options |
||
1521 | */ |
||
1522 | public static function getDefaultOptions() { |
||
1555 | |||
1556 | /** |
||
1557 | * Get a given default option value. |
||
1558 | * |
||
1559 | * @param string $opt Name of option to retrieve |
||
1560 | * @return string Default option value |
||
1561 | */ |
||
1562 | public static function getDefaultOption( $opt ) { |
||
1570 | |||
1571 | /** |
||
1572 | * Get blocking information |
||
1573 | * @param bool $bFromSlave Whether to check the replica DB first. |
||
1574 | * To improve performance, non-critical checks are done against replica DBs. |
||
1575 | * Check when actually saving should be done against master. |
||
1576 | */ |
||
1577 | private function getBlockedStatus( $bFromSlave = true ) { |
||
1662 | |||
1663 | /** |
||
1664 | * Whether the given IP is in a DNS blacklist. |
||
1665 | * |
||
1666 | * @param string $ip IP to check |
||
1667 | * @param bool $checkWhitelist Whether to check the whitelist first |
||
1668 | * @return bool True if blacklisted. |
||
1669 | */ |
||
1670 | public function isDnsBlacklisted( $ip, $checkWhitelist = false ) { |
||
1683 | |||
1684 | /** |
||
1685 | * Whether the given IP is in a given DNS blacklist. |
||
1686 | * |
||
1687 | * @param string $ip IP to check |
||
1688 | * @param string|array $bases Array of Strings: URL of the DNS blacklist |
||
1689 | * @return bool True if blacklisted. |
||
1690 | */ |
||
1691 | public function inDnsBlacklist( $ip, $bases ) { |
||
1730 | |||
1731 | /** |
||
1732 | * Check if an IP address is in the local proxy list |
||
1733 | * |
||
1734 | * @param string $ip |
||
1735 | * |
||
1736 | * @return bool |
||
1737 | */ |
||
1738 | public static function isLocallyBlockedProxy( $ip ) { |
||
1762 | |||
1763 | /** |
||
1764 | * Is this user subject to rate limiting? |
||
1765 | * |
||
1766 | * @return bool True if rate limited |
||
1767 | */ |
||
1768 | public function isPingLimitable() { |
||
1778 | |||
1779 | /** |
||
1780 | * Primitive rate limits: enforce maximum actions per time period |
||
1781 | * to put a brake on flooding. |
||
1782 | * |
||
1783 | * The method generates both a generic profiling point and a per action one |
||
1784 | * (suffix being "-$action". |
||
1785 | * |
||
1786 | * @note When using a shared cache like memcached, IP-address |
||
1787 | * last-hit counters will be shared across wikis. |
||
1788 | * |
||
1789 | * @param string $action Action to enforce; 'edit' if unspecified |
||
1790 | * @param int $incrBy Positive amount to increment counter by [defaults to 1] |
||
1791 | * @return bool True if a rate limiter was tripped |
||
1792 | */ |
||
1793 | public function pingLimiter( $action = 'edit', $incrBy = 1 ) { |
||
1921 | |||
1922 | /** |
||
1923 | * Check if user is blocked |
||
1924 | * |
||
1925 | * @param bool $bFromSlave Whether to check the replica DB instead of |
||
1926 | * the master. Hacked from false due to horrible probs on site. |
||
1927 | * @return bool True if blocked, false otherwise |
||
1928 | */ |
||
1929 | public function isBlocked( $bFromSlave = true ) { |
||
1930 | return $this->getBlock( $bFromSlave ) instanceof Block && $this->getBlock()->prevents( 'edit' ); |
||
1931 | } |
||
1932 | |||
1933 | /** |
||
1934 | * Get the block affecting the user, or null if the user is not blocked |
||
1935 | * |
||
1936 | * @param bool $bFromSlave Whether to check the replica DB instead of the master |
||
1937 | * @return Block|null |
||
1938 | */ |
||
1939 | public function getBlock( $bFromSlave = true ) { |
||
1943 | |||
1944 | /** |
||
1945 | * Check if user is blocked from editing a particular article |
||
1946 | * |
||
1947 | * @param Title $title Title to check |
||
1948 | * @param bool $bFromSlave Whether to check the replica DB instead of the master |
||
1949 | * @return bool |
||
1950 | */ |
||
1951 | public function isBlockedFrom( $title, $bFromSlave = false ) { |
||
1967 | |||
1968 | /** |
||
1969 | * If user is blocked, return the name of the user who placed the block |
||
1970 | * @return string Name of blocker |
||
1971 | */ |
||
1972 | public function blockedBy() { |
||
1976 | |||
1977 | /** |
||
1978 | * If user is blocked, return the specified reason for the block |
||
1979 | * @return string Blocking reason |
||
1980 | */ |
||
1981 | public function blockedFor() { |
||
1985 | |||
1986 | /** |
||
1987 | * If user is blocked, return the ID for the block |
||
1988 | * @return int Block ID |
||
1989 | */ |
||
1990 | public function getBlockId() { |
||
1994 | |||
1995 | /** |
||
1996 | * Check if user is blocked on all wikis. |
||
1997 | * Do not use for actual edit permission checks! |
||
1998 | * This is intended for quick UI checks. |
||
1999 | * |
||
2000 | * @param string $ip IP address, uses current client if none given |
||
2001 | * @return bool True if blocked, false otherwise |
||
2002 | */ |
||
2003 | public function isBlockedGlobally( $ip = '' ) { |
||
2006 | |||
2007 | /** |
||
2008 | * Check if user is blocked on all wikis. |
||
2009 | * Do not use for actual edit permission checks! |
||
2010 | * This is intended for quick UI checks. |
||
2011 | * |
||
2012 | * @param string $ip IP address, uses current client if none given |
||
2013 | * @return Block|null Block object if blocked, null otherwise |
||
2014 | * @throws FatalError |
||
2015 | * @throws MWException |
||
2016 | */ |
||
2017 | public function getGlobalBlock( $ip = '' ) { |
||
2040 | |||
2041 | /** |
||
2042 | * Check if user account is locked |
||
2043 | * |
||
2044 | * @return bool True if locked, false otherwise |
||
2045 | */ |
||
2046 | View Code Duplication | public function isLocked() { |
|
2055 | |||
2056 | /** |
||
2057 | * Check if user account is hidden |
||
2058 | * |
||
2059 | * @return bool True if hidden, false otherwise |
||
2060 | */ |
||
2061 | View Code Duplication | public function isHidden() { |
|
2073 | |||
2074 | /** |
||
2075 | * Get the user's ID. |
||
2076 | * @return int The user's ID; 0 if the user is anonymous or nonexistent |
||
2077 | */ |
||
2078 | public function getId() { |
||
2089 | |||
2090 | /** |
||
2091 | * Set the user and reload all fields according to a given ID |
||
2092 | * @param int $v User ID to reload |
||
2093 | */ |
||
2094 | public function setId( $v ) { |
||
2098 | |||
2099 | /** |
||
2100 | * Get the user name, or the IP of an anonymous user |
||
2101 | * @return string User's name or IP address |
||
2102 | */ |
||
2103 | public function getName() { |
||
2116 | |||
2117 | /** |
||
2118 | * Set the user name. |
||
2119 | * |
||
2120 | * This does not reload fields from the database according to the given |
||
2121 | * name. Rather, it is used to create a temporary "nonexistent user" for |
||
2122 | * later addition to the database. It can also be used to set the IP |
||
2123 | * address for an anonymous user to something other than the current |
||
2124 | * remote IP. |
||
2125 | * |
||
2126 | * @note User::newFromName() has roughly the same function, when the named user |
||
2127 | * does not exist. |
||
2128 | * @param string $str New user name to set |
||
2129 | */ |
||
2130 | public function setName( $str ) { |
||
2134 | |||
2135 | /** |
||
2136 | * Get the user's name escaped by underscores. |
||
2137 | * @return string Username escaped by underscores. |
||
2138 | */ |
||
2139 | public function getTitleKey() { |
||
2142 | |||
2143 | /** |
||
2144 | * Check if the user has new messages. |
||
2145 | * @return bool True if the user has new messages |
||
2146 | */ |
||
2147 | public function getNewtalk() { |
||
2171 | |||
2172 | /** |
||
2173 | * Return the data needed to construct links for new talk page message |
||
2174 | * alerts. If there are new messages, this will return an associative array |
||
2175 | * with the following data: |
||
2176 | * wiki: The database name of the wiki |
||
2177 | * link: Root-relative link to the user's talk page |
||
2178 | * rev: The last talk page revision that the user has seen or null. This |
||
2179 | * is useful for building diff links. |
||
2180 | * If there are no new messages, it returns an empty array. |
||
2181 | * @note This function was designed to accomodate multiple talk pages, but |
||
2182 | * currently only returns a single link and revision. |
||
2183 | * @return array |
||
2184 | */ |
||
2185 | public function getNewMessageLinks() { |
||
2202 | |||
2203 | /** |
||
2204 | * Get the revision ID for the last talk page revision viewed by the talk |
||
2205 | * page owner. |
||
2206 | * @return int|null Revision ID or null |
||
2207 | */ |
||
2208 | public function getNewMessageRevisionId() { |
||
2226 | |||
2227 | /** |
||
2228 | * Internal uncached check for new messages |
||
2229 | * |
||
2230 | * @see getNewtalk() |
||
2231 | * @param string $field 'user_ip' for anonymous users, 'user_id' otherwise |
||
2232 | * @param string|int $id User's IP address for anonymous users, User ID otherwise |
||
2233 | * @return bool True if the user has new messages |
||
2234 | */ |
||
2235 | protected function checkNewtalk( $field, $id ) { |
||
2242 | |||
2243 | /** |
||
2244 | * Add or update the new messages flag |
||
2245 | * @param string $field 'user_ip' for anonymous users, 'user_id' otherwise |
||
2246 | * @param string|int $id User's IP address for anonymous users, User ID otherwise |
||
2247 | * @param Revision|null $curRev New, as yet unseen revision of the user talk page. Ignored if null. |
||
2248 | * @return bool True if successful, false otherwise |
||
2249 | */ |
||
2250 | protected function updateNewtalk( $field, $id, $curRev = null ) { |
||
2268 | |||
2269 | /** |
||
2270 | * Clear the new messages flag for the given user |
||
2271 | * @param string $field 'user_ip' for anonymous users, 'user_id' otherwise |
||
2272 | * @param string|int $id User's IP address for anonymous users, User ID otherwise |
||
2273 | * @return bool True if successful, false otherwise |
||
2274 | */ |
||
2275 | protected function deleteNewtalk( $field, $id ) { |
||
2288 | |||
2289 | /** |
||
2290 | * Update the 'You have new messages!' status. |
||
2291 | * @param bool $val Whether the user has new messages |
||
2292 | * @param Revision $curRev New, as yet unseen revision of the user talk |
||
2293 | * page. Ignored if null or !$val. |
||
2294 | */ |
||
2295 | public function setNewtalk( $val, $curRev = null ) { |
||
2321 | |||
2322 | /** |
||
2323 | * Generate a current or new-future timestamp to be stored in the |
||
2324 | * user_touched field when we update things. |
||
2325 | * @return string Timestamp in TS_MW format |
||
2326 | */ |
||
2327 | private function newTouchedTimestamp() { |
||
2337 | |||
2338 | /** |
||
2339 | * Clear user data from memcached |
||
2340 | * |
||
2341 | * Use after applying updates to the database; caller's |
||
2342 | * responsibility to update user_touched if appropriate. |
||
2343 | * |
||
2344 | * Called implicitly from invalidateCache() and saveSettings(). |
||
2345 | * |
||
2346 | * @param string $mode Use 'refresh' to clear now; otherwise before DB commit |
||
2347 | */ |
||
2348 | public function clearSharedCache( $mode = 'changed' ) { |
||
2365 | |||
2366 | /** |
||
2367 | * Immediately touch the user data cache for this account |
||
2368 | * |
||
2369 | * Calls touch() and removes account data from memcached |
||
2370 | */ |
||
2371 | public function invalidateCache() { |
||
2375 | |||
2376 | /** |
||
2377 | * Update the "touched" timestamp for the user |
||
2378 | * |
||
2379 | * This is useful on various login/logout events when making sure that |
||
2380 | * a browser or proxy that has multiple tenants does not suffer cache |
||
2381 | * pollution where the new user sees the old users content. The value |
||
2382 | * of getTouched() is checked when determining 304 vs 200 responses. |
||
2383 | * Unlike invalidateCache(), this preserves the User object cache and |
||
2384 | * avoids database writes. |
||
2385 | * |
||
2386 | * @since 1.25 |
||
2387 | */ |
||
2388 | public function touch() { |
||
2396 | |||
2397 | /** |
||
2398 | * Validate the cache for this account. |
||
2399 | * @param string $timestamp A timestamp in TS_MW format |
||
2400 | * @return bool |
||
2401 | */ |
||
2402 | public function validateCache( $timestamp ) { |
||
2405 | |||
2406 | /** |
||
2407 | * Get the user touched timestamp |
||
2408 | * |
||
2409 | * Use this value only to validate caches via inequalities |
||
2410 | * such as in the case of HTTP If-Modified-Since response logic |
||
2411 | * |
||
2412 | * @return string TS_MW Timestamp |
||
2413 | */ |
||
2414 | public function getTouched() { |
||
2430 | |||
2431 | /** |
||
2432 | * Get the user_touched timestamp field (time of last DB updates) |
||
2433 | * @return string TS_MW Timestamp |
||
2434 | * @since 1.26 |
||
2435 | */ |
||
2436 | public function getDBTouched() { |
||
2441 | |||
2442 | /** |
||
2443 | * @deprecated Removed in 1.27. |
||
2444 | * @return Password |
||
2445 | * @since 1.24 |
||
2446 | */ |
||
2447 | public function getPassword() { |
||
2450 | |||
2451 | /** |
||
2452 | * @deprecated Removed in 1.27. |
||
2453 | * @return Password |
||
2454 | * @since 1.24 |
||
2455 | */ |
||
2456 | public function getTemporaryPassword() { |
||
2459 | |||
2460 | /** |
||
2461 | * Set the password and reset the random token. |
||
2462 | * Calls through to authentication plugin if necessary; |
||
2463 | * will have no effect if the auth plugin refuses to |
||
2464 | * pass the change through or if the legal password |
||
2465 | * checks fail. |
||
2466 | * |
||
2467 | * As a special case, setting the password to null |
||
2468 | * wipes it, so the account cannot be logged in until |
||
2469 | * a new password is set, for instance via e-mail. |
||
2470 | * |
||
2471 | * @deprecated since 1.27, use AuthManager instead |
||
2472 | * @param string $str New password to set |
||
2473 | * @throws PasswordError On failure |
||
2474 | * @return bool |
||
2475 | */ |
||
2476 | public function setPassword( $str ) { |
||
2479 | |||
2480 | /** |
||
2481 | * Set the password and reset the random token unconditionally. |
||
2482 | * |
||
2483 | * @deprecated since 1.27, use AuthManager instead |
||
2484 | * @param string|null $str New password to set or null to set an invalid |
||
2485 | * password hash meaning that the user will not be able to log in |
||
2486 | * through the web interface. |
||
2487 | */ |
||
2488 | public function setInternalPassword( $str ) { |
||
2491 | |||
2492 | /** |
||
2493 | * Actually set the password and such |
||
2494 | * @since 1.27 cannot set a password for a user not in the database |
||
2495 | * @param string|null $str New password to set or null to set an invalid |
||
2496 | * password hash meaning that the user will not be able to log in |
||
2497 | * through the web interface. |
||
2498 | * @return bool Success |
||
2499 | */ |
||
2500 | private function setPasswordInternal( $str ) { |
||
2525 | |||
2526 | /** |
||
2527 | * Changes credentials of the user. |
||
2528 | * |
||
2529 | * This is a convenience wrapper around AuthManager::changeAuthenticationData. |
||
2530 | * Note that this can return a status that isOK() but not isGood() on certain types of failures, |
||
2531 | * e.g. when no provider handled the change. |
||
2532 | * |
||
2533 | * @param array $data A set of authentication data in fieldname => value format. This is the |
||
2534 | * same data you would pass the changeauthenticationdata API - 'username', 'password' etc. |
||
2535 | * @return Status |
||
2536 | * @since 1.27 |
||
2537 | */ |
||
2538 | public function changeAuthenticationData( array $data ) { |
||
2558 | |||
2559 | /** |
||
2560 | * Get the user's current token. |
||
2561 | * @param bool $forceCreation Force the generation of a new token if the |
||
2562 | * user doesn't have one (default=true for backwards compatibility). |
||
2563 | * @return string|null Token |
||
2564 | */ |
||
2565 | public function getToken( $forceCreation = true ) { |
||
2596 | |||
2597 | /** |
||
2598 | * Set the random token (used for persistent authentication) |
||
2599 | * Called from loadDefaults() among other places. |
||
2600 | * |
||
2601 | * @param string|bool $token If specified, set the token to this value |
||
2602 | */ |
||
2603 | public function setToken( $token = false ) { |
||
2614 | |||
2615 | /** |
||
2616 | * Set the password for a password reminder or new account email |
||
2617 | * |
||
2618 | * @deprecated Removed in 1.27. Use PasswordReset instead. |
||
2619 | * @param string $str New password to set or null to set an invalid |
||
2620 | * password hash meaning that the user will not be able to use it |
||
2621 | * @param bool $throttle If true, reset the throttle timestamp to the present |
||
2622 | */ |
||
2623 | public function setNewpassword( $str, $throttle = true ) { |
||
2626 | |||
2627 | /** |
||
2628 | * Has password reminder email been sent within the last |
||
2629 | * $wgPasswordReminderResendTime hours? |
||
2630 | * @deprecated Removed in 1.27. See above. |
||
2631 | * @return bool |
||
2632 | */ |
||
2633 | public function isPasswordReminderThrottled() { |
||
2636 | |||
2637 | /** |
||
2638 | * Get the user's e-mail address |
||
2639 | * @return string User's email address |
||
2640 | */ |
||
2641 | public function getEmail() { |
||
2646 | |||
2647 | /** |
||
2648 | * Get the timestamp of the user's e-mail authentication |
||
2649 | * @return string TS_MW timestamp |
||
2650 | */ |
||
2651 | public function getEmailAuthenticationTimestamp() { |
||
2656 | |||
2657 | /** |
||
2658 | * Set the user's e-mail address |
||
2659 | * @param string $str New e-mail address |
||
2660 | */ |
||
2661 | public function setEmail( $str ) { |
||
2670 | |||
2671 | /** |
||
2672 | * Set the user's e-mail address and a confirmation mail if needed. |
||
2673 | * |
||
2674 | * @since 1.20 |
||
2675 | * @param string $str New e-mail address |
||
2676 | * @return Status |
||
2677 | */ |
||
2678 | public function setEmailWithConfirmation( $str ) { |
||
2728 | |||
2729 | /** |
||
2730 | * Get the user's real name |
||
2731 | * @return string User's real name |
||
2732 | */ |
||
2733 | public function getRealName() { |
||
2740 | |||
2741 | /** |
||
2742 | * Set the user's real name |
||
2743 | * @param string $str New real name |
||
2744 | */ |
||
2745 | public function setRealName( $str ) { |
||
2749 | |||
2750 | /** |
||
2751 | * Get the user's current setting for a given option. |
||
2752 | * |
||
2753 | * @param string $oname The option to check |
||
2754 | * @param string $defaultOverride A default value returned if the option does not exist |
||
2755 | * @param bool $ignoreHidden Whether to ignore the effects of $wgHiddenPrefs |
||
2756 | * @return string User's current value for the option |
||
2757 | * @see getBoolOption() |
||
2758 | * @see getIntOption() |
||
2759 | */ |
||
2760 | public function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) { |
||
2779 | |||
2780 | /** |
||
2781 | * Get all user's options |
||
2782 | * |
||
2783 | * @param int $flags Bitwise combination of: |
||
2784 | * User::GETOPTIONS_EXCLUDE_DEFAULTS Exclude user options that are set |
||
2785 | * to the default value. (Since 1.25) |
||
2786 | * @return array |
||
2787 | */ |
||
2788 | public function getOptions( $flags = 0 ) { |
||
2811 | |||
2812 | /** |
||
2813 | * Get the user's current setting for a given option, as a boolean value. |
||
2814 | * |
||
2815 | * @param string $oname The option to check |
||
2816 | * @return bool User's current value for the option |
||
2817 | * @see getOption() |
||
2818 | */ |
||
2819 | public function getBoolOption( $oname ) { |
||
2822 | |||
2823 | /** |
||
2824 | * Get the user's current setting for a given option, as an integer value. |
||
2825 | * |
||
2826 | * @param string $oname The option to check |
||
2827 | * @param int $defaultOverride A default value returned if the option does not exist |
||
2828 | * @return int User's current value for the option |
||
2829 | * @see getOption() |
||
2830 | */ |
||
2831 | public function getIntOption( $oname, $defaultOverride = 0 ) { |
||
2838 | |||
2839 | /** |
||
2840 | * Set the given option for a user. |
||
2841 | * |
||
2842 | * You need to call saveSettings() to actually write to the database. |
||
2843 | * |
||
2844 | * @param string $oname The option to set |
||
2845 | * @param mixed $val New value to set |
||
2846 | */ |
||
2847 | public function setOption( $oname, $val ) { |
||
2857 | |||
2858 | /** |
||
2859 | * Get a token stored in the preferences (like the watchlist one), |
||
2860 | * resetting it if it's empty (and saving changes). |
||
2861 | * |
||
2862 | * @param string $oname The option name to retrieve the token from |
||
2863 | * @return string|bool User's current value for the option, or false if this option is disabled. |
||
2864 | * @see resetTokenFromOption() |
||
2865 | * @see getOption() |
||
2866 | * @deprecated since 1.26 Applications should use the OAuth extension |
||
2867 | */ |
||
2868 | public function getTokenFromOption( $oname ) { |
||
2886 | |||
2887 | /** |
||
2888 | * Reset a token stored in the preferences (like the watchlist one). |
||
2889 | * *Does not* save user's preferences (similarly to setOption()). |
||
2890 | * |
||
2891 | * @param string $oname The option name to reset the token in |
||
2892 | * @return string|bool New token value, or false if this option is disabled. |
||
2893 | * @see getTokenFromOption() |
||
2894 | * @see setOption() |
||
2895 | */ |
||
2896 | public function resetTokenFromOption( $oname ) { |
||
2906 | |||
2907 | /** |
||
2908 | * Return a list of the types of user options currently returned by |
||
2909 | * User::getOptionKinds(). |
||
2910 | * |
||
2911 | * Currently, the option kinds are: |
||
2912 | * - 'registered' - preferences which are registered in core MediaWiki or |
||
2913 | * by extensions using the UserGetDefaultOptions hook. |
||
2914 | * - 'registered-multiselect' - as above, using the 'multiselect' type. |
||
2915 | * - 'registered-checkmatrix' - as above, using the 'checkmatrix' type. |
||
2916 | * - 'userjs' - preferences with names starting with 'userjs-', intended to |
||
2917 | * be used by user scripts. |
||
2918 | * - 'special' - "preferences" that are not accessible via User::getOptions |
||
2919 | * or User::setOptions. |
||
2920 | * - 'unused' - preferences about which MediaWiki doesn't know anything. |
||
2921 | * These are usually legacy options, removed in newer versions. |
||
2922 | * |
||
2923 | * The API (and possibly others) use this function to determine the possible |
||
2924 | * option types for validation purposes, so make sure to update this when a |
||
2925 | * new option kind is added. |
||
2926 | * |
||
2927 | * @see User::getOptionKinds |
||
2928 | * @return array Option kinds |
||
2929 | */ |
||
2930 | public static function listOptionKinds() { |
||
2940 | |||
2941 | /** |
||
2942 | * Return an associative array mapping preferences keys to the kind of a preference they're |
||
2943 | * used for. Different kinds are handled differently when setting or reading preferences. |
||
2944 | * |
||
2945 | * See User::listOptionKinds for the list of valid option types that can be provided. |
||
2946 | * |
||
2947 | * @see User::listOptionKinds |
||
2948 | * @param IContextSource $context |
||
2949 | * @param array $options Assoc. array with options keys to check as keys. |
||
2950 | * Defaults to $this->mOptions. |
||
2951 | * @return array The key => kind mapping data |
||
2952 | */ |
||
2953 | public function getOptionKinds( IContextSource $context, $options = null ) { |
||
3022 | |||
3023 | /** |
||
3024 | * Reset certain (or all) options to the site defaults |
||
3025 | * |
||
3026 | * The optional parameter determines which kinds of preferences will be reset. |
||
3027 | * Supported values are everything that can be reported by getOptionKinds() |
||
3028 | * and 'all', which forces a reset of *all* preferences and overrides everything else. |
||
3029 | * |
||
3030 | * @param array|string $resetKinds Which kinds of preferences to reset. Defaults to |
||
3031 | * array( 'registered', 'registered-multiselect', 'registered-checkmatrix', 'unused' ) |
||
3032 | * for backwards-compatibility. |
||
3033 | * @param IContextSource|null $context Context source used when $resetKinds |
||
3034 | * does not contain 'all', passed to getOptionKinds(). |
||
3035 | * Defaults to RequestContext::getMain() when null. |
||
3036 | */ |
||
3037 | public function resetOptions( |
||
3077 | |||
3078 | /** |
||
3079 | * Get the user's preferred date format. |
||
3080 | * @return string User's preferred date format |
||
3081 | */ |
||
3082 | public function getDatePreference() { |
||
3095 | |||
3096 | /** |
||
3097 | * Determine based on the wiki configuration and the user's options, |
||
3098 | * whether this user must be over HTTPS no matter what. |
||
3099 | * |
||
3100 | * @return bool |
||
3101 | */ |
||
3102 | public function requiresHTTPS() { |
||
3115 | |||
3116 | /** |
||
3117 | * Get the user preferred stub threshold |
||
3118 | * |
||
3119 | * @return int |
||
3120 | */ |
||
3121 | public function getStubThreshold() { |
||
3131 | |||
3132 | /** |
||
3133 | * Get the permissions this user has. |
||
3134 | * @return array Array of String permission names |
||
3135 | */ |
||
3136 | public function getRights() { |
||
3171 | |||
3172 | /** |
||
3173 | * Get the list of explicit group memberships this user has. |
||
3174 | * The implicit * and user groups are not included. |
||
3175 | * @return array Array of String internal group names |
||
3176 | */ |
||
3177 | public function getGroups() { |
||
3182 | |||
3183 | /** |
||
3184 | * Get the list of implicit group memberships this user has. |
||
3185 | * This includes all explicit groups, plus 'user' if logged in, |
||
3186 | * '*' for all accounts, and autopromoted groups |
||
3187 | * @param bool $recache Whether to avoid the cache |
||
3188 | * @return array Array of String internal group names |
||
3189 | */ |
||
3190 | public function getEffectiveGroups( $recache = false ) { |
||
3203 | |||
3204 | /** |
||
3205 | * Get the list of implicit group memberships this user has. |
||
3206 | * This includes 'user' if logged in, '*' for all accounts, |
||
3207 | * and autopromoted groups |
||
3208 | * @param bool $recache Whether to avoid the cache |
||
3209 | * @return array Array of String internal group names |
||
3210 | */ |
||
3211 | public function getAutomaticGroups( $recache = false ) { |
||
3230 | |||
3231 | /** |
||
3232 | * Returns the groups the user has belonged to. |
||
3233 | * |
||
3234 | * The user may still belong to the returned groups. Compare with getGroups(). |
||
3235 | * |
||
3236 | * The function will not return groups the user had belonged to before MW 1.17 |
||
3237 | * |
||
3238 | * @return array Names of the groups the user has belonged to. |
||
3239 | */ |
||
3240 | View Code Duplication | public function getFormerGroups() { |
|
3259 | |||
3260 | /** |
||
3261 | * Get the user's edit count. |
||
3262 | * @return int|null Null for anonymous users |
||
3263 | */ |
||
3264 | public function getEditCount() { |
||
3287 | |||
3288 | /** |
||
3289 | * Add the user to the given group. |
||
3290 | * This takes immediate effect. |
||
3291 | * @param string $group Name of the group to add |
||
3292 | * @return bool |
||
3293 | */ |
||
3294 | public function addGroup( $group ) { |
||
3327 | |||
3328 | /** |
||
3329 | * Remove the user from the given group. |
||
3330 | * This takes immediate effect. |
||
3331 | * @param string $group Name of the group to remove |
||
3332 | * @return bool |
||
3333 | */ |
||
3334 | public function removeGroup( $group ) { |
||
3369 | |||
3370 | /** |
||
3371 | * Get whether the user is logged in |
||
3372 | * @return bool |
||
3373 | */ |
||
3374 | public function isLoggedIn() { |
||
3377 | |||
3378 | /** |
||
3379 | * Get whether the user is anonymous |
||
3380 | * @return bool |
||
3381 | */ |
||
3382 | public function isAnon() { |
||
3385 | |||
3386 | /** |
||
3387 | * @return bool Whether this user is flagged as being a bot role account |
||
3388 | * @since 1.28 |
||
3389 | */ |
||
3390 | public function isBot() { |
||
3400 | |||
3401 | /** |
||
3402 | * Check if user is allowed to access a feature / make an action |
||
3403 | * |
||
3404 | * @param string ... Permissions to test |
||
3405 | * @return bool True if user is allowed to perform *any* of the given actions |
||
3406 | */ |
||
3407 | View Code Duplication | public function isAllowedAny() { |
|
3416 | |||
3417 | /** |
||
3418 | * |
||
3419 | * @param string ... Permissions to test |
||
3420 | * @return bool True if the user is allowed to perform *all* of the given actions |
||
3421 | */ |
||
3422 | View Code Duplication | public function isAllowedAll() { |
|
3431 | |||
3432 | /** |
||
3433 | * Internal mechanics of testing a permission |
||
3434 | * @param string $action |
||
3435 | * @return bool |
||
3436 | */ |
||
3437 | public function isAllowed( $action = '' ) { |
||
3445 | |||
3446 | /** |
||
3447 | * Check whether to enable recent changes patrol features for this user |
||
3448 | * @return bool True or false |
||
3449 | */ |
||
3450 | public function useRCPatrol() { |
||
3454 | |||
3455 | /** |
||
3456 | * Check whether to enable new pages patrol features for this user |
||
3457 | * @return bool True or false |
||
3458 | */ |
||
3459 | public function useNPPatrol() { |
||
3466 | |||
3467 | /** |
||
3468 | * Check whether to enable new files patrol features for this user |
||
3469 | * @return bool True or false |
||
3470 | */ |
||
3471 | public function useFilePatrol() { |
||
3478 | |||
3479 | /** |
||
3480 | * Get the WebRequest object to use with this object |
||
3481 | * |
||
3482 | * @return WebRequest |
||
3483 | */ |
||
3484 | public function getRequest() { |
||
3492 | |||
3493 | /** |
||
3494 | * Check the watched status of an article. |
||
3495 | * @since 1.22 $checkRights parameter added |
||
3496 | * @param Title $title Title of the article to look at |
||
3497 | * @param bool $checkRights Whether to check 'viewmywatchlist'/'editmywatchlist' rights. |
||
3498 | * Pass User::CHECK_USER_RIGHTS or User::IGNORE_USER_RIGHTS. |
||
3499 | * @return bool |
||
3500 | */ |
||
3501 | public function isWatched( $title, $checkRights = self::CHECK_USER_RIGHTS ) { |
||
3507 | |||
3508 | /** |
||
3509 | * Watch an article. |
||
3510 | * @since 1.22 $checkRights parameter added |
||
3511 | * @param Title $title Title of the article to look at |
||
3512 | * @param bool $checkRights Whether to check 'viewmywatchlist'/'editmywatchlist' rights. |
||
3513 | * Pass User::CHECK_USER_RIGHTS or User::IGNORE_USER_RIGHTS. |
||
3514 | */ |
||
3515 | public function addWatch( $title, $checkRights = self::CHECK_USER_RIGHTS ) { |
||
3524 | |||
3525 | /** |
||
3526 | * Stop watching an article. |
||
3527 | * @since 1.22 $checkRights parameter added |
||
3528 | * @param Title $title Title of the article to look at |
||
3529 | * @param bool $checkRights Whether to check 'viewmywatchlist'/'editmywatchlist' rights. |
||
3530 | * Pass User::CHECK_USER_RIGHTS or User::IGNORE_USER_RIGHTS. |
||
3531 | */ |
||
3532 | public function removeWatch( $title, $checkRights = self::CHECK_USER_RIGHTS ) { |
||
3540 | |||
3541 | /** |
||
3542 | * Clear the user's notification timestamp for the given title. |
||
3543 | * If e-notif e-mails are on, they will receive notification mails on |
||
3544 | * the next change of the page if it's watched etc. |
||
3545 | * @note If the user doesn't have 'editmywatchlist', this will do nothing. |
||
3546 | * @param Title $title Title of the article to look at |
||
3547 | * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed. |
||
3548 | */ |
||
3549 | public function clearNotification( &$title, $oldid = 0 ) { |
||
3608 | |||
3609 | /** |
||
3610 | * Resets all of the given user's page-change notification timestamps. |
||
3611 | * If e-notif e-mails are on, they will receive notification mails on |
||
3612 | * the next change of any watched page. |
||
3613 | * @note If the user doesn't have 'editmywatchlist', this will do nothing. |
||
3614 | */ |
||
3615 | public function clearAllNotifications() { |
||
3680 | |||
3681 | /** |
||
3682 | * Set a cookie on the user's client. Wrapper for |
||
3683 | * WebResponse::setCookie |
||
3684 | * @deprecated since 1.27 |
||
3685 | * @param string $name Name of the cookie to set |
||
3686 | * @param string $value Value to set |
||
3687 | * @param int $exp Expiration time, as a UNIX time value; |
||
3688 | * if 0 or not specified, use the default $wgCookieExpiration |
||
3689 | * @param bool $secure |
||
3690 | * true: Force setting the secure attribute when setting the cookie |
||
3691 | * false: Force NOT setting the secure attribute when setting the cookie |
||
3692 | * null (default): Use the default ($wgCookieSecure) to set the secure attribute |
||
3693 | * @param array $params Array of options sent passed to WebResponse::setcookie() |
||
3694 | * @param WebRequest|null $request WebRequest object to use; $wgRequest will be used if null |
||
3695 | * is passed. |
||
3696 | */ |
||
3697 | protected function setCookie( |
||
3707 | |||
3708 | /** |
||
3709 | * Clear a cookie on the user's client |
||
3710 | * @deprecated since 1.27 |
||
3711 | * @param string $name Name of the cookie to clear |
||
3712 | * @param bool $secure |
||
3713 | * true: Force setting the secure attribute when setting the cookie |
||
3714 | * false: Force NOT setting the secure attribute when setting the cookie |
||
3715 | * null (default): Use the default ($wgCookieSecure) to set the secure attribute |
||
3716 | * @param array $params Array of options sent passed to WebResponse::setcookie() |
||
3717 | */ |
||
3718 | protected function clearCookie( $name, $secure = null, $params = [] ) { |
||
3722 | |||
3723 | /** |
||
3724 | * Set an extended login cookie on the user's client. The expiry of the cookie |
||
3725 | * is controlled by the $wgExtendedLoginCookieExpiration configuration |
||
3726 | * variable. |
||
3727 | * |
||
3728 | * @see User::setCookie |
||
3729 | * |
||
3730 | * @deprecated since 1.27 |
||
3731 | * @param string $name Name of the cookie to set |
||
3732 | * @param string $value Value to set |
||
3733 | * @param bool $secure |
||
3734 | * true: Force setting the secure attribute when setting the cookie |
||
3735 | * false: Force NOT setting the secure attribute when setting the cookie |
||
3736 | * null (default): Use the default ($wgCookieSecure) to set the secure attribute |
||
3737 | */ |
||
3738 | protected function setExtendedLoginCookie( $name, $value, $secure ) { |
||
3750 | |||
3751 | /** |
||
3752 | * Persist this user's session (e.g. set cookies) |
||
3753 | * |
||
3754 | * @param WebRequest|null $request WebRequest object to use; $wgRequest will be used if null |
||
3755 | * is passed. |
||
3756 | * @param bool $secure Whether to force secure/insecure cookies or use default |
||
3757 | * @param bool $rememberMe Whether to add a Token cookie for elongated sessions |
||
3758 | */ |
||
3759 | public function setCookies( $request = null, $secure = null, $rememberMe = false ) { |
||
3791 | |||
3792 | /** |
||
3793 | * Log this user out. |
||
3794 | */ |
||
3795 | public function logout() { |
||
3796 | if ( Hooks::run( 'UserLogout', [ &$this ] ) ) { |
||
3797 | $this->doLogout(); |
||
3798 | } |
||
3799 | } |
||
3800 | |||
3801 | /** |
||
3802 | * Clear the user's session, and reset the instance cache. |
||
3803 | * @see logout() |
||
3804 | */ |
||
3805 | public function doLogout() { |
||
3836 | |||
3837 | /** |
||
3838 | * Save this user's settings into the database. |
||
3839 | * @todo Only rarely do all these fields need to be set! |
||
3840 | */ |
||
3841 | public function saveSettings() { |
||
3896 | |||
3897 | /** |
||
3898 | * If only this user's username is known, and it exists, return the user ID. |
||
3899 | * |
||
3900 | * @param int $flags Bitfield of User:READ_* constants; useful for existence checks |
||
3901 | * @return int |
||
3902 | */ |
||
3903 | public function idForName( $flags = 0 ) { |
||
3922 | |||
3923 | /** |
||
3924 | * Add a user to the database, return the user object |
||
3925 | * |
||
3926 | * @param string $name Username to add |
||
3927 | * @param array $params Array of Strings Non-default parameters to save to |
||
3928 | * the database as user_* fields: |
||
3929 | * - email: The user's email address. |
||
3930 | * - email_authenticated: The email authentication timestamp. |
||
3931 | * - real_name: The user's real name. |
||
3932 | * - options: An associative array of non-default options. |
||
3933 | * - token: Random authentication token. Do not set. |
||
3934 | * - registration: Registration timestamp. Do not set. |
||
3935 | * |
||
3936 | * @return User|null User object, or null if the username already exists. |
||
3937 | */ |
||
3938 | public static function createNew( $name, $params = [] ) { |
||
3982 | |||
3983 | /** |
||
3984 | * Add this existing user object to the database. If the user already |
||
3985 | * exists, a fatal status object is returned, and the user object is |
||
3986 | * initialised with the data from the database. |
||
3987 | * |
||
3988 | * Previously, this function generated a DB error due to a key conflict |
||
3989 | * if the user already existed. Many extension callers use this function |
||
3990 | * in code along the lines of: |
||
3991 | * |
||
3992 | * $user = User::newFromName( $name ); |
||
3993 | * if ( !$user->isLoggedIn() ) { |
||
3994 | * $user->addToDatabase(); |
||
3995 | * } |
||
3996 | * // do something with $user... |
||
3997 | * |
||
3998 | * However, this was vulnerable to a race condition (bug 16020). By |
||
3999 | * initialising the user object if the user exists, we aim to support this |
||
4000 | * calling sequence as far as possible. |
||
4001 | * |
||
4002 | * Note that if the user exists, this function will acquire a write lock, |
||
4003 | * so it is still advisable to make the call conditional on isLoggedIn(), |
||
4004 | * and to commit the transaction after calling. |
||
4005 | * |
||
4006 | * @throws MWException |
||
4007 | * @return Status |
||
4008 | */ |
||
4009 | public function addToDatabase() { |
||
4067 | |||
4068 | /** |
||
4069 | * If this user is logged-in and blocked, |
||
4070 | * block any IP address they've successfully logged in from. |
||
4071 | * @return bool A block was spread |
||
4072 | */ |
||
4073 | public function spreadAnyEditBlock() { |
||
4080 | |||
4081 | /** |
||
4082 | * If this (non-anonymous) user is blocked, |
||
4083 | * block the IP address they've successfully logged in from. |
||
4084 | * @return bool A block was spread |
||
4085 | */ |
||
4086 | protected function spreadBlock() { |
||
4100 | |||
4101 | /** |
||
4102 | * Get whether the user is explicitly blocked from account creation. |
||
4103 | * @return bool|Block |
||
4104 | */ |
||
4105 | public function isBlockedFromCreateAccount() { |
||
4122 | |||
4123 | /** |
||
4124 | * Get whether the user is blocked from using Special:Emailuser. |
||
4125 | * @return bool |
||
4126 | */ |
||
4127 | public function isBlockedFromEmailuser() { |
||
4131 | |||
4132 | /** |
||
4133 | * Get whether the user is allowed to create an account. |
||
4134 | * @return bool |
||
4135 | */ |
||
4136 | public function isAllowedToCreateAccount() { |
||
4139 | |||
4140 | /** |
||
4141 | * Get this user's personal page title. |
||
4142 | * |
||
4143 | * @return Title User's personal page title |
||
4144 | */ |
||
4145 | public function getUserPage() { |
||
4148 | |||
4149 | /** |
||
4150 | * Get this user's talk page title. |
||
4151 | * |
||
4152 | * @return Title User's talk page title |
||
4153 | */ |
||
4154 | public function getTalkPage() { |
||
4158 | |||
4159 | /** |
||
4160 | * Determine whether the user is a newbie. Newbies are either |
||
4161 | * anonymous IPs, or the most recently created accounts. |
||
4162 | * @return bool |
||
4163 | */ |
||
4164 | public function isNewbie() { |
||
4167 | |||
4168 | /** |
||
4169 | * Check to see if the given clear-text password is one of the accepted passwords |
||
4170 | * @deprecated since 1.27, use AuthManager instead |
||
4171 | * @param string $password User password |
||
4172 | * @return bool True if the given password is correct, otherwise False |
||
4173 | */ |
||
4174 | public function checkPassword( $password ) { |
||
4198 | |||
4199 | /** |
||
4200 | * Check if the given clear-text password matches the temporary password |
||
4201 | * sent by e-mail for password reset operations. |
||
4202 | * |
||
4203 | * @deprecated since 1.27, use AuthManager instead |
||
4204 | * @param string $plaintext |
||
4205 | * @return bool True if matches, false otherwise |
||
4206 | */ |
||
4207 | public function checkTemporaryPassword( $plaintext ) { |
||
4211 | |||
4212 | /** |
||
4213 | * Initialize (if necessary) and return a session token value |
||
4214 | * which can be used in edit forms to show that the user's |
||
4215 | * login credentials aren't being hijacked with a foreign form |
||
4216 | * submission. |
||
4217 | * |
||
4218 | * @since 1.27 |
||
4219 | * @param string|array $salt Array of Strings Optional function-specific data for hashing |
||
4220 | * @param WebRequest|null $request WebRequest object to use or null to use $wgRequest |
||
4221 | * @return MediaWiki\Session\Token The new edit token |
||
4222 | */ |
||
4223 | public function getEditTokenObject( $salt = '', $request = null ) { |
||
4233 | |||
4234 | /** |
||
4235 | * Initialize (if necessary) and return a session token value |
||
4236 | * which can be used in edit forms to show that the user's |
||
4237 | * login credentials aren't being hijacked with a foreign form |
||
4238 | * submission. |
||
4239 | * |
||
4240 | * The $salt for 'edit' and 'csrf' tokens is the default (empty string). |
||
4241 | * |
||
4242 | * @since 1.19 |
||
4243 | * @param string|array $salt Array of Strings Optional function-specific data for hashing |
||
4244 | * @param WebRequest|null $request WebRequest object to use or null to use $wgRequest |
||
4245 | * @return string The new edit token |
||
4246 | */ |
||
4247 | public function getEditToken( $salt = '', $request = null ) { |
||
4250 | |||
4251 | /** |
||
4252 | * Get the embedded timestamp from a token. |
||
4253 | * @deprecated since 1.27, use \MediaWiki\Session\Token::getTimestamp instead. |
||
4254 | * @param string $val Input token |
||
4255 | * @return int|null |
||
4256 | */ |
||
4257 | public static function getEditTokenTimestamp( $val ) { |
||
4261 | |||
4262 | /** |
||
4263 | * Check given value against the token value stored in the session. |
||
4264 | * A match should confirm that the form was submitted from the |
||
4265 | * user's own login session, not a form submission from a third-party |
||
4266 | * site. |
||
4267 | * |
||
4268 | * @param string $val Input value to compare |
||
4269 | * @param string $salt Optional function-specific data for hashing |
||
4270 | * @param WebRequest|null $request Object to use or null to use $wgRequest |
||
4271 | * @param int $maxage Fail tokens older than this, in seconds |
||
4272 | * @return bool Whether the token matches |
||
4273 | */ |
||
4274 | public function matchEditToken( $val, $salt = '', $request = null, $maxage = null ) { |
||
4277 | |||
4278 | /** |
||
4279 | * Check given value against the token value stored in the session, |
||
4280 | * ignoring the suffix. |
||
4281 | * |
||
4282 | * @param string $val Input value to compare |
||
4283 | * @param string $salt Optional function-specific data for hashing |
||
4284 | * @param WebRequest|null $request Object to use or null to use $wgRequest |
||
4285 | * @param int $maxage Fail tokens older than this, in seconds |
||
4286 | * @return bool Whether the token matches |
||
4287 | */ |
||
4288 | public function matchEditTokenNoSuffix( $val, $salt = '', $request = null, $maxage = null ) { |
||
4292 | |||
4293 | /** |
||
4294 | * Generate a new e-mail confirmation token and send a confirmation/invalidation |
||
4295 | * mail to the user's given address. |
||
4296 | * |
||
4297 | * @param string $type Message to send, either "created", "changed" or "set" |
||
4298 | * @return Status |
||
4299 | */ |
||
4300 | public function sendConfirmationMail( $type = 'created' ) { |
||
4327 | |||
4328 | /** |
||
4329 | * Send an e-mail to this user's account. Does not check for |
||
4330 | * confirmed status or validity. |
||
4331 | * |
||
4332 | * @param string $subject Message subject |
||
4333 | * @param string $body Message body |
||
4334 | * @param User|null $from Optional sending user; if unspecified, default |
||
4335 | * $wgPasswordSender will be used. |
||
4336 | * @param string $replyto Reply-To address |
||
4337 | * @return Status |
||
4338 | */ |
||
4339 | public function sendMail( $subject, $body, $from = null, $replyto = null ) { |
||
4354 | |||
4355 | /** |
||
4356 | * Generate, store, and return a new e-mail confirmation code. |
||
4357 | * A hash (unsalted, since it's used as a key) is stored. |
||
4358 | * |
||
4359 | * @note Call saveSettings() after calling this function to commit |
||
4360 | * this change to the database. |
||
4361 | * |
||
4362 | * @param string &$expiration Accepts the expiration time |
||
4363 | * @return string New token |
||
4364 | */ |
||
4365 | protected function confirmationToken( &$expiration ) { |
||
4377 | |||
4378 | /** |
||
4379 | * Return a URL the user can use to confirm their email address. |
||
4380 | * @param string $token Accepts the email confirmation token |
||
4381 | * @return string New token URL |
||
4382 | */ |
||
4383 | protected function confirmationTokenUrl( $token ) { |
||
4386 | |||
4387 | /** |
||
4388 | * Return a URL the user can use to invalidate their email address. |
||
4389 | * @param string $token Accepts the email confirmation token |
||
4390 | * @return string New token URL |
||
4391 | */ |
||
4392 | protected function invalidationTokenUrl( $token ) { |
||
4395 | |||
4396 | /** |
||
4397 | * Internal function to format the e-mail validation/invalidation URLs. |
||
4398 | * This uses a quickie hack to use the |
||
4399 | * hardcoded English names of the Special: pages, for ASCII safety. |
||
4400 | * |
||
4401 | * @note Since these URLs get dropped directly into emails, using the |
||
4402 | * short English names avoids insanely long URL-encoded links, which |
||
4403 | * also sometimes can get corrupted in some browsers/mailers |
||
4404 | * (bug 6957 with Gmail and Internet Explorer). |
||
4405 | * |
||
4406 | * @param string $page Special page |
||
4407 | * @param string $token Token |
||
4408 | * @return string Formatted URL |
||
4409 | */ |
||
4410 | protected function getTokenUrl( $page, $token ) { |
||
4415 | |||
4416 | /** |
||
4417 | * Mark the e-mail address confirmed. |
||
4418 | * |
||
4419 | * @note Call saveSettings() after calling this function to commit the change. |
||
4420 | * |
||
4421 | * @return bool |
||
4422 | */ |
||
4423 | public function confirmEmail() { |
||
4432 | |||
4433 | /** |
||
4434 | * Invalidate the user's e-mail confirmation, and unauthenticate the e-mail |
||
4435 | * address if it was already confirmed. |
||
4436 | * |
||
4437 | * @note Call saveSettings() after calling this function to commit the change. |
||
4438 | * @return bool Returns true |
||
4439 | */ |
||
4440 | public function invalidateEmail() { |
||
4449 | |||
4450 | /** |
||
4451 | * Set the e-mail authentication timestamp. |
||
4452 | * @param string $timestamp TS_MW timestamp |
||
4453 | */ |
||
4454 | public function setEmailAuthenticationTimestamp( $timestamp ) { |
||
4459 | |||
4460 | /** |
||
4461 | * Is this user allowed to send e-mails within limits of current |
||
4462 | * site configuration? |
||
4463 | * @return bool |
||
4464 | */ |
||
4465 | public function canSendEmail() { |
||
4474 | |||
4475 | /** |
||
4476 | * Is this user allowed to receive e-mails within limits of current |
||
4477 | * site configuration? |
||
4478 | * @return bool |
||
4479 | */ |
||
4480 | public function canReceiveEmail() { |
||
4483 | |||
4484 | /** |
||
4485 | * Is this user's e-mail address valid-looking and confirmed within |
||
4486 | * limits of the current site configuration? |
||
4487 | * |
||
4488 | * @note If $wgEmailAuthentication is on, this may require the user to have |
||
4489 | * confirmed their address by returning a code or using a password |
||
4490 | * sent to the address from the wiki. |
||
4491 | * |
||
4492 | * @return bool |
||
4493 | */ |
||
4494 | public function isEmailConfirmed() { |
||
4513 | |||
4514 | /** |
||
4515 | * Check whether there is an outstanding request for e-mail confirmation. |
||
4516 | * @return bool |
||
4517 | */ |
||
4518 | public function isEmailConfirmationPending() { |
||
4525 | |||
4526 | /** |
||
4527 | * Get the timestamp of account creation. |
||
4528 | * |
||
4529 | * @return string|bool|null Timestamp of account creation, false for |
||
4530 | * non-existent/anonymous user accounts, or null if existing account |
||
4531 | * but information is not in database. |
||
4532 | */ |
||
4533 | public function getRegistration() { |
||
4540 | |||
4541 | /** |
||
4542 | * Get the timestamp of the first edit |
||
4543 | * |
||
4544 | * @return string|bool Timestamp of first edit, or false for |
||
4545 | * non-existent/anonymous user accounts. |
||
4546 | */ |
||
4547 | public function getFirstEditTimestamp() { |
||
4562 | |||
4563 | /** |
||
4564 | * Get the permissions associated with a given list of groups |
||
4565 | * |
||
4566 | * @param array $groups Array of Strings List of internal group names |
||
4567 | * @return array Array of Strings List of permission key names for given groups combined |
||
4568 | */ |
||
4569 | public static function getGroupPermissions( $groups ) { |
||
4589 | |||
4590 | /** |
||
4591 | * Get all the groups who have a given permission |
||
4592 | * |
||
4593 | * @param string $role Role to check |
||
4594 | * @return array Array of Strings List of internal group names with the given permission |
||
4595 | */ |
||
4596 | public static function getGroupsWithPermission( $role ) { |
||
4606 | |||
4607 | /** |
||
4608 | * Check, if the given group has the given permission |
||
4609 | * |
||
4610 | * If you're wanting to check whether all users have a permission, use |
||
4611 | * User::isEveryoneAllowed() instead. That properly checks if it's revoked |
||
4612 | * from anyone. |
||
4613 | * |
||
4614 | * @since 1.21 |
||
4615 | * @param string $group Group to check |
||
4616 | * @param string $role Role to check |
||
4617 | * @return bool |
||
4618 | */ |
||
4619 | public static function groupHasPermission( $group, $role ) { |
||
4624 | |||
4625 | /** |
||
4626 | * Check if all users may be assumed to have the given permission |
||
4627 | * |
||
4628 | * We generally assume so if the right is granted to '*' and isn't revoked |
||
4629 | * on any group. It doesn't attempt to take grants or other extension |
||
4630 | * limitations on rights into account in the general case, though, as that |
||
4631 | * would require it to always return false and defeat the purpose. |
||
4632 | * Specifically, session-based rights restrictions (such as OAuth or bot |
||
4633 | * passwords) are applied based on the current session. |
||
4634 | * |
||
4635 | * @since 1.22 |
||
4636 | * @param string $right Right to check |
||
4637 | * @return bool |
||
4638 | */ |
||
4639 | public static function isEveryoneAllowed( $right ) { |
||
4681 | |||
4682 | /** |
||
4683 | * Get the localized descriptive name for a group, if it exists |
||
4684 | * |
||
4685 | * @param string $group Internal group name |
||
4686 | * @return string Localized descriptive group name |
||
4687 | */ |
||
4688 | public static function getGroupName( $group ) { |
||
4692 | |||
4693 | /** |
||
4694 | * Get the localized descriptive name for a member of a group, if it exists |
||
4695 | * |
||
4696 | * @param string $group Internal group name |
||
4697 | * @param string $username Username for gender (since 1.19) |
||
4698 | * @return string Localized name for group member |
||
4699 | */ |
||
4700 | public static function getGroupMember( $group, $username = '#' ) { |
||
4704 | |||
4705 | /** |
||
4706 | * Return the set of defined explicit groups. |
||
4707 | * The implicit groups (by default *, 'user' and 'autoconfirmed') |
||
4708 | * are not included, as they are defined automatically, not in the database. |
||
4709 | * @return array Array of internal group names |
||
4710 | */ |
||
4711 | public static function getAllGroups() { |
||
4718 | |||
4719 | /** |
||
4720 | * Get a list of all available permissions. |
||
4721 | * @return string[] Array of permission names |
||
4722 | */ |
||
4723 | public static function getAllRights() { |
||
4735 | |||
4736 | /** |
||
4737 | * Get a list of implicit groups |
||
4738 | * @return array Array of Strings Array of internal group names |
||
4739 | */ |
||
4740 | public static function getImplicitGroups() { |
||
4749 | |||
4750 | /** |
||
4751 | * Get the title of a page describing a particular group |
||
4752 | * |
||
4753 | * @param string $group Internal group name |
||
4754 | * @return Title|bool Title of the page if it exists, false otherwise |
||
4755 | */ |
||
4756 | public static function getGroupPage( $group ) { |
||
4766 | |||
4767 | /** |
||
4768 | * Create a link to the group in HTML, if available; |
||
4769 | * else return the group name. |
||
4770 | * |
||
4771 | * @param string $group Internal name of the group |
||
4772 | * @param string $text The text of the link |
||
4773 | * @return string HTML link to the group |
||
4774 | */ |
||
4775 | public static function makeGroupLinkHTML( $group, $text = '' ) { |
||
4786 | |||
4787 | /** |
||
4788 | * Create a link to the group in Wikitext, if available; |
||
4789 | * else return the group name. |
||
4790 | * |
||
4791 | * @param string $group Internal name of the group |
||
4792 | * @param string $text The text of the link |
||
4793 | * @return string Wikilink to the group |
||
4794 | */ |
||
4795 | public static function makeGroupLinkWiki( $group, $text = '' ) { |
||
4807 | |||
4808 | /** |
||
4809 | * Returns an array of the groups that a particular group can add/remove. |
||
4810 | * |
||
4811 | * @param string $group The group to check for whether it can add/remove |
||
4812 | * @return array Array( 'add' => array( addablegroups ), |
||
4813 | * 'remove' => array( removablegroups ), |
||
4814 | * 'add-self' => array( addablegroups to self), |
||
4815 | * 'remove-self' => array( removable groups from self) ) |
||
4816 | */ |
||
4817 | public static function changeableByGroup( $group ) { |
||
4882 | |||
4883 | /** |
||
4884 | * Returns an array of groups that this user can add and remove |
||
4885 | * @return array Array( 'add' => array( addablegroups ), |
||
4886 | * 'remove' => array( removablegroups ), |
||
4887 | * 'add-self' => array( addablegroups to self), |
||
4888 | * 'remove-self' => array( removable groups from self) ) |
||
4889 | */ |
||
4890 | public function changeableGroups() { |
||
4925 | |||
4926 | /** |
||
4927 | * Deferred version of incEditCountImmediate() |
||
4928 | */ |
||
4929 | public function incEditCount() { |
||
4934 | |||
4935 | /** |
||
4936 | * Increment the user's edit-count field. |
||
4937 | * Will have no effect for anonymous users. |
||
4938 | * @since 1.26 |
||
4939 | */ |
||
4940 | public function incEditCountImmediate() { |
||
4980 | |||
4981 | /** |
||
4982 | * Initialize user_editcount from data out of the revision table |
||
4983 | * |
||
4984 | * @param int $add Edits to add to the count from the revision table |
||
4985 | * @return int Number of edits |
||
4986 | */ |
||
4987 | protected function initEditCount( $add = 0 ) { |
||
5009 | |||
5010 | /** |
||
5011 | * Get the description of a given right |
||
5012 | * |
||
5013 | * @param string $right Right to query |
||
5014 | * @return string Localized description of the right |
||
5015 | */ |
||
5016 | public static function getRightDescription( $right ) { |
||
5021 | |||
5022 | /** |
||
5023 | * Make a new-style password hash |
||
5024 | * |
||
5025 | * @param string $password Plain-text password |
||
5026 | * @param bool|string $salt Optional salt, may be random or the user ID. |
||
5027 | * If unspecified or false, will generate one automatically |
||
5028 | * @return string Password hash |
||
5029 | * @deprecated since 1.24, use Password class |
||
5030 | */ |
||
5031 | public static function crypt( $password, $salt = false ) { |
||
5038 | |||
5039 | /** |
||
5040 | * Compare a password hash with a plain-text password. Requires the user |
||
5041 | * ID if there's a chance that the hash is an old-style hash. |
||
5042 | * |
||
5043 | * @param string $hash Password hash |
||
5044 | * @param string $password Plain-text password to compare |
||
5045 | * @param string|bool $userId User ID for old-style password salt |
||
5046 | * |
||
5047 | * @return bool |
||
5048 | * @deprecated since 1.24, use Password class |
||
5049 | */ |
||
5050 | public static function comparePasswords( $hash, $password, $userId = false ) { |
||
5069 | |||
5070 | /** |
||
5071 | * Add a newuser log entry for this user. |
||
5072 | * Before 1.19 the return value was always true. |
||
5073 | * |
||
5074 | * @deprecated since 1.27, AuthManager handles logging |
||
5075 | * @param string|bool $action Account creation type. |
||
5076 | * - String, one of the following values: |
||
5077 | * - 'create' for an anonymous user creating an account for himself. |
||
5078 | * This will force the action's performer to be the created user itself, |
||
5079 | * no matter the value of $wgUser |
||
5080 | * - 'create2' for a logged in user creating an account for someone else |
||
5081 | * - 'byemail' when the created user will receive its password by e-mail |
||
5082 | * - 'autocreate' when the user is automatically created (such as by CentralAuth). |
||
5083 | * - Boolean means whether the account was created by e-mail (deprecated): |
||
5084 | * - true will be converted to 'byemail' |
||
5085 | * - false will be converted to 'create' if this object is the same as |
||
5086 | * $wgUser and to 'create2' otherwise |
||
5087 | * @param string $reason User supplied reason |
||
5088 | * @return bool true |
||
5089 | */ |
||
5090 | public function addNewUserLogEntry( $action = false, $reason = '' ) { |
||
5093 | |||
5094 | /** |
||
5095 | * Add an autocreate newuser log entry for this user |
||
5096 | * Used by things like CentralAuth and perhaps other authplugins. |
||
5097 | * Consider calling addNewUserLogEntry() directly instead. |
||
5098 | * |
||
5099 | * @deprecated since 1.27, AuthManager handles logging |
||
5100 | * @return bool |
||
5101 | */ |
||
5102 | public function addNewUserLogEntryAutoCreate() { |
||
5107 | |||
5108 | /** |
||
5109 | * Load the user options either from cache, the database or an array |
||
5110 | * |
||
5111 | * @param array $data Rows for the current user out of the user_properties table |
||
5112 | */ |
||
5113 | protected function loadOptions( $data = null ) { |
||
5173 | |||
5174 | /** |
||
5175 | * Saves the non-default options for this user, as previously set e.g. via |
||
5176 | * setOption(), in the database's "user_properties" (preferences) table. |
||
5177 | * Usually used via saveSettings(). |
||
5178 | */ |
||
5179 | protected function saveOptions() { |
||
5238 | |||
5239 | /** |
||
5240 | * Lazily instantiate and return a factory object for making passwords |
||
5241 | * |
||
5242 | * @deprecated since 1.27, create a PasswordFactory directly instead |
||
5243 | * @return PasswordFactory |
||
5244 | */ |
||
5245 | public static function getPasswordFactory() { |
||
5251 | |||
5252 | /** |
||
5253 | * Provide an array of HTML5 attributes to put on an input element |
||
5254 | * intended for the user to enter a new password. This may include |
||
5255 | * required, title, and/or pattern, depending on $wgMinimalPasswordLength. |
||
5256 | * |
||
5257 | * Do *not* use this when asking the user to enter his current password! |
||
5258 | * Regardless of configuration, users may have invalid passwords for whatever |
||
5259 | * reason (e.g., they were set before requirements were tightened up). |
||
5260 | * Only use it when asking for a new password, like on account creation or |
||
5261 | * ResetPass. |
||
5262 | * |
||
5263 | * Obviously, you still need to do server-side checking. |
||
5264 | * |
||
5265 | * NOTE: A combination of bugs in various browsers means that this function |
||
5266 | * actually just returns array() unconditionally at the moment. May as |
||
5267 | * well keep it around for when the browser bugs get fixed, though. |
||
5268 | * |
||
5269 | * @todo FIXME: This does not belong here; put it in Html or Linker or somewhere |
||
5270 | * |
||
5271 | * @deprecated since 1.27 |
||
5272 | * @return array Array of HTML attributes suitable for feeding to |
||
5273 | * Html::element(), directly or indirectly. (Don't feed to Xml::*()! |
||
5274 | * That will get confused by the boolean attribute syntax used.) |
||
5275 | */ |
||
5276 | public static function passwordChangeInputAttribs() { |
||
5310 | |||
5311 | /** |
||
5312 | * Return the list of user fields that should be selected to create |
||
5313 | * a new user object. |
||
5314 | * @return array |
||
5315 | */ |
||
5316 | public static function selectFields() { |
||
5331 | |||
5332 | /** |
||
5333 | * Factory function for fatal permission-denied errors |
||
5334 | * |
||
5335 | * @since 1.22 |
||
5336 | * @param string $permission User right required |
||
5337 | * @return Status |
||
5338 | */ |
||
5339 | static function newFatalPermissionDeniedStatus( $permission ) { |
||
5353 | |||
5354 | /** |
||
5355 | * Get a new instance of this user that was loaded from the master via a locking read |
||
5356 | * |
||
5357 | * Use this instead of the main context User when updating that user. This avoids races |
||
5358 | * where that user was loaded from a replica DB or even the master but without proper locks. |
||
5359 | * |
||
5360 | * @return User|null Returns null if the user was not found in the DB |
||
5361 | * @since 1.27 |
||
5362 | */ |
||
5363 | public function getInstanceForUpdate() { |
||
5375 | |||
5376 | /** |
||
5377 | * Checks if two user objects point to the same user. |
||
5378 | * |
||
5379 | * @since 1.25 |
||
5380 | * @param User $user |
||
5381 | * @return bool |
||
5382 | */ |
||
5383 | public function equals( User $user ) { |
||
5386 | } |
||
5387 |
This class constant has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.