MediaWiki master
MediaWiki\User\Options\UserOptionsManager Class Reference

A service class to control user options. More...

Inherits MediaWiki\User\Options\UserOptionsLookup.

Collaboration diagram for MediaWiki\User\Options\UserOptionsManager:

Public Member Functions

 __construct (ServiceOptions $options, DefaultOptionsLookup $defaultOptionsLookup, LanguageConverterFactory $languageConverterFactory, IConnectionProvider $dbProvider, LoggerInterface $logger, HookContainer $hookContainer, UserFactory $userFactory, UserNameUtils $userNameUtils)
 
 clearUserOptionsCache (UserIdentity $user)
 Clears cached user options.
 
 getDefaultOption (string $opt, ?UserIdentity $userIdentity=null)
 Get a given default option value.
Parameters
string$optName of option to retrieve
UserIdentity | null$userIdentityUser to look the defaults up for; set to null to ignore any user-specific defaults (since 1.42)
Returns
mixed|null Default option value

 
 getDefaultOptions (?UserIdentity $userIdentity=null)
 Combine the language default options with any site-specific and user-specific defaults and add the default language variants.
Parameters
UserIdentity | null$userIdentityUser to look the default up for; set to null to ignore any user-specific defaults (since 1.42)
Returns
array

 
 getOption (UserIdentity $user, string $oname, $defaultOverride=null, bool $ignoreHidden=false, int $queryFlags=IDBAccessObject::READ_NORMAL)
 Get the user's current setting for a given option.
Parameters
UserIdentity$userThe user to get the option for
string$onameThe option to check
mixed | null$defaultOverrideA default value returned if the option does not exist
bool$ignoreHiddenWhether to ignore the effects of $wgHiddenPrefs
int$queryFlagsA bit field composed of READ_XXX flags
Returns
mixed|null User's current value for the option, Note that while option values retrieved from the database are always strings, default values and values set within the current request and not yet saved may be of another type.
See also
getBoolOption()
getIntOption()

 
 getOptionKinds (UserIdentity $userIdentity, IContextSource $context, $options=null)
 Return an associative array mapping preferences keys to the kind of a preference they're used for.
 
 getOptions (UserIdentity $user, int $flags=0, int $queryFlags=IDBAccessObject::READ_NORMAL)
 Get all user's options.
Parameters
UserIdentity$userThe user to get the option for
int$flagsBitwise combination of: UserOptionsManager::EXCLUDE_DEFAULTS Exclude user options that are set to the default value. Options that are set to their conditionally default value are not excluded.
int$queryFlagsA bit field composed of READ_XXX flags
Returns
array

 
 listOptionKinds ()
 Return a list of the types of user options currently returned by UserOptionsManager::getOptionKinds().
 
 loadUserOptions (UserIdentity $user, int $queryFlags=IDBAccessObject::READ_NORMAL, array $data=null)
 Loads user options either from cache or from the database.
 
 resetOptions (UserIdentity $user, IContextSource $context, $resetKinds=[ 'registered', 'registered-multiselect', 'registered-checkmatrix', 'unused'])
 Reset certain (or all) options to the site defaults.
 
 saveOptions (UserIdentity $user)
 Saves the non-default options for this user, as previously set e.g.
 
 saveOptionsInternal (UserIdentity $user, IDatabase $dbw)
 Saves the non-default options for this user, as previously set e.g.
 
 setOption (UserIdentity $user, string $oname, $val)
 Set the given option for a user.
 
- Public Member Functions inherited from MediaWiki\User\Options\UserOptionsLookup
 getBoolOption (UserIdentity $user, string $oname, int $queryFlags=IDBAccessObject::READ_NORMAL)
 Get the user's current setting for a given option, as a boolean value.
 
 getIntOption (UserIdentity $user, string $oname, int $defaultOverride=0, int $queryFlags=IDBAccessObject::READ_NORMAL)
 Get the user's current setting for a given option, as an integer value.
 

Public Attributes

const CONSTRUCTOR_OPTIONS
 
const MAX_BYTES_OPTION_VALUE = 65530
 
- Public Attributes inherited from MediaWiki\User\Options\UserOptionsLookup
const EXCLUDE_DEFAULTS = 1
 Exclude user options that are set to their default value.
 

Detailed Description

A service class to control user options.

Since
1.35

Definition at line 50 of file UserOptionsManager.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\User\Options\UserOptionsManager::__construct ( ServiceOptions $options,
DefaultOptionsLookup $defaultOptionsLookup,
LanguageConverterFactory $languageConverterFactory,
IConnectionProvider $dbProvider,
LoggerInterface $logger,
HookContainer $hookContainer,
UserFactory $userFactory,
UserNameUtils $userNameUtils )
Parameters
ServiceOptions$options
DefaultOptionsLookup$defaultOptionsLookup
LanguageConverterFactory$languageConverterFactory
IConnectionProvider$dbProvider
LoggerInterface$logger
HookContainer$hookContainer
UserFactory$userFactory
UserNameUtils$userNameUtils

Definition at line 105 of file UserOptionsManager.php.

References MediaWiki\Config\ServiceOptions\assertRequiredOptions().

Member Function Documentation

◆ clearUserOptionsCache()

MediaWiki\User\Options\UserOptionsManager::clearUserOptionsCache ( UserIdentity $user)

Clears cached user options.

Access: internal
To be used by User::clearInstanceCache
Parameters
UserIdentity$user

Definition at line 541 of file UserOptionsManager.php.

References getCacheKey().

◆ getDefaultOption()

MediaWiki\User\Options\UserOptionsManager::getDefaultOption ( string $opt,
?UserIdentity $userIdentity = null )

Get a given default option value.

Parameters
string$optName of option to retrieve
UserIdentity | null$userIdentityUser to look the defaults up for; set to null to ignore any user-specific defaults (since 1.42)
Returns
mixed|null Default option value

Reimplemented from MediaWiki\User\Options\UserOptionsLookup.

Definition at line 136 of file UserOptionsManager.php.

◆ getDefaultOptions()

MediaWiki\User\Options\UserOptionsManager::getDefaultOptions ( ?UserIdentity $userIdentity = null)

Combine the language default options with any site-specific and user-specific defaults and add the default language variants.

Parameters
UserIdentity | null$userIdentityUser to look the default up for; set to null to ignore any user-specific defaults (since 1.42)
Returns
array

Reimplemented from MediaWiki\User\Options\UserOptionsLookup.

Definition at line 129 of file UserOptionsManager.php.

◆ getOption()

MediaWiki\User\Options\UserOptionsManager::getOption ( UserIdentity $user,
string $oname,
$defaultOverride = null,
bool $ignoreHidden = false,
int $queryFlags = IDBAccessObject::READ_NORMAL )

Get the user's current setting for a given option.

Parameters
UserIdentity$userThe user to get the option for
string$onameThe option to check
mixed | null$defaultOverrideA default value returned if the option does not exist
bool$ignoreHiddenWhether to ignore the effects of $wgHiddenPrefs
int$queryFlagsA bit field composed of READ_XXX flags
Returns
mixed|null User's current value for the option, Note that while option values retrieved from the database are always strings, default values and values set within the current request and not yet saved may be of another type.
See also
getBoolOption()
getIntOption()

Reimplemented from MediaWiki\User\Options\UserOptionsLookup.

Definition at line 143 of file UserOptionsManager.php.

References MediaWiki\MainConfigNames\HiddenPrefs.

◆ getOptionKinds()

MediaWiki\User\Options\UserOptionsManager::getOptionKinds ( UserIdentity $userIdentity,
IContextSource $context,
$options = null )

Return an associative array mapping preferences keys to the kind of a preference they're used for.

Different kinds are handled differently when setting or reading preferences.

See UserOptionsManager::listOptionKinds for the list of valid option types that can be provided.

See also
UserOptionsManager::listOptionKinds
Parameters
UserIdentity$userIdentity
IContextSource$context
array | null$optionsAssoc. array with options keys to check as keys. Defaults user options.
Returns
string[] The key => kind mapping data

Definition at line 333 of file UserOptionsManager.php.

◆ getOptions()

MediaWiki\User\Options\UserOptionsManager::getOptions ( UserIdentity $user,
int $flags = 0,
int $queryFlags = IDBAccessObject::READ_NORMAL )

Get all user's options.

Parameters
UserIdentity$userThe user to get the option for
int$flagsBitwise combination of: UserOptionsManager::EXCLUDE_DEFAULTS Exclude user options that are set to the default value. Options that are set to their conditionally default value are not excluded.
int$queryFlagsA bit field composed of READ_XXX flags
Returns
array

Reimplemented from MediaWiki\User\Options\UserOptionsLookup.

Definition at line 169 of file UserOptionsManager.php.

◆ listOptionKinds()

MediaWiki\User\Options\UserOptionsManager::listOptionKinds ( )

Return a list of the types of user options currently returned by UserOptionsManager::getOptionKinds().

Currently, the option kinds are:

  • 'registered' - preferences which are registered in core MediaWiki or by extensions using the UserGetDefaultOptions hook.
  • 'registered-multiselect' - as above, using the 'multiselect' type.
  • 'registered-checkmatrix' - as above, using the 'checkmatrix' type.
  • 'userjs' - preferences with names starting with 'userjs-', intended to be used by user scripts.
  • 'special' - "preferences" that are not accessible via UserOptionsLookup::getOptions or UserOptionsManager::setOptions.
  • 'unused' - preferences about which MediaWiki doesn't know anything. These are usually legacy options, removed in newer versions.

The API (and possibly others) use this function to determine the possible option types for validation purposes, so make sure to update this when a new option kind is added.

See also
getOptionKinds
Returns
string[] Option kinds

Definition at line 309 of file UserOptionsManager.php.

◆ loadUserOptions()

MediaWiki\User\Options\UserOptionsManager::loadUserOptions ( UserIdentity $user,
int $queryFlags = IDBAccessObject::READ_NORMAL,
array $data = null )

Loads user options either from cache or from the database.

Note
Query flags are ignored for anons, since they do not have any options stored in the database. If the UserIdentity was itself obtained from a replica and doesn't have ID set due to replication lag, it will be treated as anon regardless of the query flags passed here.
Parameters
UserIdentity$user
int$queryFlags
array | null$dataassociative array of non-default options.
Returns
array
Access: internal
To be called by User loading code to provide the $data

Definition at line 526 of file UserOptionsManager.php.

◆ resetOptions()

MediaWiki\User\Options\UserOptionsManager::resetOptions ( UserIdentity $user,
IContextSource $context,
$resetKinds = [ 'registered', 'registered-multiselect', 'registered-checkmatrix', 'unused' ] )

Reset certain (or all) options to the site defaults.

The optional parameter determines which kinds of preferences will be reset. Supported values are everything that can be reported by getOptionKinds() and 'all', which forces a reset of all preferences and overrides everything else.

Note
You need to call saveOptions() to actually write to the database.
Parameters
UserIdentity$user
IContextSource$contextContext source used when $resetKinds does not contain 'all'.
array | string$resetKindsWhich kinds of preferences to reset. Defaults to [ 'registered', 'registered-multiselect', 'registered-checkmatrix', 'unused' ]

Definition at line 252 of file UserOptionsManager.php.

References getCacheKey().

◆ saveOptions()

MediaWiki\User\Options\UserOptionsManager::saveOptions ( UserIdentity $user)

Saves the non-default options for this user, as previously set e.g.

via setOption(), in the database's "user_properties" (preferences) table.

Since
1.38, this method was internal before that.
Parameters
UserIdentity$user

Definition at line 419 of file UserOptionsManager.php.

◆ saveOptionsInternal()

MediaWiki\User\Options\UserOptionsManager::saveOptionsInternal ( UserIdentity $user,
IDatabase $dbw )

Saves the non-default options for this user, as previously set e.g.

via setOption(), in the database's "user_properties" (preferences) table.

Parameters
UserIdentity$user
IDatabase$dbw
Returns
bool true if options were changed and new options successfully saved.
Access: internal
only public for use in User::saveSettings

Definition at line 445 of file UserOptionsManager.php.

◆ setOption()

MediaWiki\User\Options\UserOptionsManager::setOption ( UserIdentity $user,
string $oname,
$val )

Set the given option for a user.

You need to call saveOptions() to actually write to the database.

$val should be null or a string. Other types are accepted for B/C with legacy code but can result in surprising behavior and are discouraged. Values are always stored as strings in the database, so if you pass a non-string value, it will be eventually converted; but before the call to saveOptions(), getOption() will return the passed value from instance cache without any type conversion.

A null value means resetting the option to its default value (removing the user_properties row). Passing in the same value as the default value fo the user has the same result. This behavior supports some level of type juggling - e.g. if the default value is 1, and you pass in '1', the option will be reset to its default value.

When an option is reset to its default value, that means whenever the default value is changed in the site configuration, the user preference for this user will also change. There is no way to set a user preference to be the same as the default but avoid it changing when the default changes. You can instead use $wgConditionalUserOptions to split the default based on user registration date.

Parameters
UserIdentity$user
string$onameThe option to set
mixed$valNew value to set.

Definition at line 230 of file UserOptionsManager.php.

References getCacheKey().

Member Data Documentation

◆ CONSTRUCTOR_OPTIONS

const MediaWiki\User\Options\UserOptionsManager::CONSTRUCTOR_OPTIONS
Initial value:
= [
]
const HiddenPrefs
Name constant for the HiddenPrefs setting, for use with Config::get()
const LocalTZoffset
Name constant for the LocalTZoffset setting, for use with Config::get()
Access: internal
For use by ServiceWiring

Definition at line 55 of file UserOptionsManager.php.

◆ MAX_BYTES_OPTION_VALUE

const MediaWiki\User\Options\UserOptionsManager::MAX_BYTES_OPTION_VALUE = 65530
Since
1.39.5, 1.40

Definition at line 63 of file UserOptionsManager.php.


The documentation for this class was generated from the following file: