MediaWiki  master
MediaWiki\User\UserOptionsManager Class Reference

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

Inheritance diagram for MediaWiki\User\UserOptionsManager:
Collaboration diagram for MediaWiki\User\UserOptionsManager:

Public Member Functions

 __construct (ServiceOptions $options, DefaultOptionsLookup $defaultOptionsLookup, LanguageConverterFactory $languageConverterFactory, ILoadBalancer $loadBalancer, LoggerInterface $logger, HookContainer $hookContainer)
 
 clearUserOptionsCache (UserIdentity $user)
 Clears cached user options. More...
 
 getDefaultOptions ()
 Combine the language default options with any site-specific options and add the default language variants.
Returns
array
More...
 
 getOption (UserIdentity $user, string $oname, $defaultOverride=null, bool $ignoreHidden=false, int $queryFlags=self::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
See also
getBoolOption()
getIntOption()
More...
 
 getOptionKinds (UserIdentity $userIdentity, IContextSource $context, $options=null)
 Return an associative array mapping preferences keys to the kind of a preference they're used for. More...
 
 getOptions (UserIdentity $user, int $flags=0, int $queryFlags=self::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.
int$queryFlagsA bit field composed of READ_XXX flags
Returns
array
More...
 
 listOptionKinds ()
 Return a list of the types of user options currently returned by UserOptionsManager::getOptionKinds(). More...
 
 loadUserOptions (UserIdentity $user, int $queryFlags=self::READ_NORMAL, array $data=null)
 Loads user options either from cache or from the database. More...
 
 resetOptions (UserIdentity $user, IContextSource $context, $resetKinds=[ 'registered', 'registered-multiselect', 'registered-checkmatrix', 'unused'])
 Reset certain (or all) options to the site defaults. More...
 
 saveOptions (UserIdentity $user)
 Saves the non-default options for this user, as previously set e.g. More...
 
 setOption (UserIdentity $user, string $oname, $val)
 Set the given option for a user. More...
 
- Public Member Functions inherited from MediaWiki\User\UserOptionsLookup
 getBoolOption (UserIdentity $user, string $oname, int $queryFlags=self::READ_NORMAL)
 Get the user's current setting for a given option, as a boolean value. More...
 
 getDefaultOption (string $opt)
 Get a given default option value. More...
 
 getIntOption (UserIdentity $user, string $oname, int $defaultOverride=0, int $queryFlags=self::READ_NORMAL)
 Get the user's current setting for a given option, as an integer value. More...
 

Public Attributes

const CONSTRUCTOR_OPTIONS
 
- Public Attributes inherited from MediaWiki\User\UserOptionsLookup
const EXCLUDE_DEFAULTS = 1
 Exclude user options that are set to their default value. More...
 
- Public Attributes inherited from IDBAccessObject
const READ_NONE = -1
 Constants for object loading bitfield flags (higher => higher QoS) More...
 

Private Member Functions

 canUseCachedValues (UserIdentity $user, int $queryFlags)
 Determines if it's ok to use cached options values for a given user and query flags. More...
 
 getCacheKey (UserIdentity $user)
 Gets a key for various caches. More...
 
 getDBAndOptionsForQueryFlags ( $queryFlags)
 
 isValueEqual ( $a, $b)
 Determines whether two values are sufficiently similar that the database does not need to be updated to reflect the change. More...
 
 loadOptionsFromDb (UserIdentity $user, int $queryFlags, array $prefetchedOptions=null)
 Fetches the options directly from the database with no caches. More...
 
 loadOriginalOptions (UserIdentity $user, int $queryFlags=self::READ_NORMAL, array $data=null)
 Loads the original user options from the database and applies various transforms, like timecorrection. More...
 
 setOptionsFromDb (UserIdentity $user, int $queryFlags, iterable $rows)
 Builds associative options array from rows fetched from DB. More...
 

Private Attributes

DefaultOptionsLookup $defaultOptionsLookup
 
HookRunner $hookRunner
 
LanguageConverterFactory $languageConverterFactory
 
ILoadBalancer $loadBalancer
 
LoggerInterface $logger
 
array $modifiedOptions = []
 options modified withing this request More...
 
array $optionsFromDb = []
 Cached original user options as fetched from database, no adjustments applied. More...
 
array $originalOptionsCache = []
 Cached original user options with all the adjustments like time correction and hook changes applied. More...
 
array $queryFlagsUsedForCaching = []
 Query flags used to retrieve options from database. More...
 
ServiceOptions $serviceOptions
 

Detailed Description

A service class to control user options.

Since
1.35

Definition at line 43 of file UserOptionsManager.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\User\UserOptionsManager::__construct ( ServiceOptions  $options,
DefaultOptionsLookup  $defaultOptionsLookup,
LanguageConverterFactory  $languageConverterFactory,
ILoadBalancer  $loadBalancer,
LoggerInterface  $logger,
HookContainer  $hookContainer 
)
Parameters
ServiceOptions$options
DefaultOptionsLookup$defaultOptionsLookup
LanguageConverterFactory$languageConverterFactory
ILoadBalancer$loadBalancer
LoggerInterface$logger
HookContainer$hookContainer

Definition at line 98 of file UserOptionsManager.php.

References MediaWiki\User\UserOptionsManager\$defaultOptionsLookup, MediaWiki\User\UserOptionsManager\$languageConverterFactory, MediaWiki\User\UserOptionsManager\$loadBalancer, MediaWiki\User\UserOptionsManager\$logger, and MediaWiki\Config\ServiceOptions\assertRequiredOptions().

Member Function Documentation

◆ canUseCachedValues()

MediaWiki\User\UserOptionsManager::canUseCachedValues ( UserIdentity  $user,
int  $queryFlags 
)
private

Determines if it's ok to use cached options values for a given user and query flags.

Parameters
UserIdentity$user
int$queryFlags
Returns
bool

Definition at line 663 of file UserOptionsManager.php.

References MediaWiki\User\UserOptionsManager\getCacheKey(), MediaWiki\User\UserIdentity\isRegistered(), and IDBAccessObject\READ_NONE.

Referenced by MediaWiki\User\UserOptionsManager\loadOriginalOptions().

◆ clearUserOptionsCache()

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

Clears cached user options.

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

Definition at line 503 of file UserOptionsManager.php.

References MediaWiki\User\UserOptionsManager\getCacheKey().

◆ getCacheKey()

◆ getDBAndOptionsForQueryFlags()

MediaWiki\User\UserOptionsManager::getDBAndOptionsForQueryFlags (   $queryFlags)
private
Parameters
int$queryFlagsa bit field composed of READ_XXX flags
Returns
array [ IDatabase $db, array $options ]

Definition at line 652 of file UserOptionsManager.php.

References DBAccessObjectUtils\getDBOptions().

Referenced by MediaWiki\User\UserOptionsManager\loadOptionsFromDb().

◆ getDefaultOptions()

MediaWiki\User\UserOptionsManager::getDefaultOptions ( )

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

Returns
array

Reimplemented from MediaWiki\User\UserOptionsLookup.

Definition at line 118 of file UserOptionsManager.php.

◆ getOption()

MediaWiki\User\UserOptionsManager::getOption ( UserIdentity  $user,
string  $oname,
  $defaultOverride = null,
bool  $ignoreHidden = false,
int  $queryFlags = self::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
See also
getBoolOption()
getIntOption()

Reimplemented from MediaWiki\User\UserOptionsLookup.

Definition at line 125 of file UserOptionsManager.php.

References MediaWiki\User\UserOptionsManager\loadUserOptions().

◆ getOptionKinds()

MediaWiki\User\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 295 of file UserOptionsManager.php.

References MediaWiki\$context, HTMLFormField\flattenOptions(), MediaWiki\MediaWikiServices\getInstance(), MediaWiki\User\UserOptionsManager\loadUserOptions(), and User\newFromIdentity().

Referenced by MediaWiki\User\UserOptionsManager\resetOptions().

◆ getOptions()

MediaWiki\User\UserOptionsManager::getOptions ( UserIdentity  $user,
int  $flags = 0,
int  $queryFlags = self::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.
int$queryFlagsA bit field composed of READ_XXX flags
Returns
array

Reimplemented from MediaWiki\User\UserOptionsLookup.

Definition at line 151 of file UserOptionsManager.php.

References MediaWiki\User\UserOptionsManager\loadUserOptions().

◆ isValueEqual()

MediaWiki\User\UserOptionsManager::isValueEqual (   $a,
  $b 
)
private

Determines whether two values are sufficiently similar that the database does not need to be updated to reflect the change.

This is basically the same as comparing the result of Database::addQuotes().

Parameters
mixed$a
mixed$b
Returns
bool

Definition at line 683 of file UserOptionsManager.php.

Referenced by MediaWiki\User\UserOptionsManager\saveOptions().

◆ listOptionKinds()

MediaWiki\User\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 271 of file UserOptionsManager.php.

Referenced by MediaWiki\User\UserOptionsManager\resetOptions().

◆ loadOptionsFromDb()

MediaWiki\User\UserOptionsManager::loadOptionsFromDb ( UserIdentity  $user,
int  $queryFlags,
array  $prefetchedOptions = null 
)
private

Fetches the options directly from the database with no caches.

Parameters
UserIdentity$user
int$queryFlags
array | null$prefetchedOptions
Returns
array

Definition at line 519 of file UserOptionsManager.php.

References $dbr, $res, MediaWiki\User\UserOptionsManager\getDBAndOptionsForQueryFlags(), MediaWiki\User\UserIdentity\getId(), and MediaWiki\User\UserOptionsManager\setOptionsFromDb().

Referenced by MediaWiki\User\UserOptionsManager\loadOriginalOptions().

◆ loadOriginalOptions()

MediaWiki\User\UserOptionsManager::loadOriginalOptions ( UserIdentity  $user,
int  $queryFlags = self::READ_NORMAL,
array  $data = null 
)
private

Loads the original user options from the database and applies various transforms, like timecorrection.

Runs hooks.

Parameters
UserIdentity$user
int$queryFlags
array | null$dataassociative array of non-default options
Returns
array

Definition at line 586 of file UserOptionsManager.php.

References MediaWiki\User\UserOptionsManager\canUseCachedValues(), MediaWiki\User\UserOptionsManager\getCacheKey(), MediaWiki\User\UserIdentity\isRegistered(), MediaWiki\User\UserOptionsManager\loadOptionsFromDb(), User\newFromIdentity(), and LanguageCode\replaceDeprecatedCodes().

Referenced by MediaWiki\User\UserOptionsManager\loadUserOptions(), and MediaWiki\User\UserOptionsManager\saveOptions().

◆ loadUserOptions()

MediaWiki\User\UserOptionsManager::loadUserOptions ( UserIdentity  $user,
int  $queryFlags = self::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 488 of file UserOptionsManager.php.

References MediaWiki\User\UserOptionsManager\getCacheKey(), and MediaWiki\User\UserOptionsManager\loadOriginalOptions().

Referenced by MediaWiki\User\UserOptionsManager\getOption(), MediaWiki\User\UserOptionsManager\getOptionKinds(), MediaWiki\User\UserOptionsManager\getOptions(), and MediaWiki\User\UserOptionsManager\resetOptions().

◆ resetOptions()

MediaWiki\User\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 208 of file UserOptionsManager.php.

References MediaWiki\$context, MediaWiki\User\UserOptionsManager\getCacheKey(), MediaWiki\User\UserOptionsManager\getOptionKinds(), MediaWiki\User\UserOptionsManager\listOptionKinds(), MediaWiki\User\UserOptionsManager\loadUserOptions(), and User\newFromIdentity().

◆ saveOptions()

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

◆ setOption()

MediaWiki\User\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.

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

Definition at line 186 of file UserOptionsManager.php.

References MediaWiki\User\UserOptionsManager\getCacheKey().

◆ setOptionsFromDb()

MediaWiki\User\UserOptionsManager::setOptionsFromDb ( UserIdentity  $user,
int  $queryFlags,
iterable  $rows 
)
private

Builds associative options array from rows fetched from DB.

Parameters
UserIdentity$user
int$queryFlags
iterable<object|array>$rows
Returns
array

Definition at line 554 of file UserOptionsManager.php.

References MediaWiki\User\UserOptionsManager\getCacheKey().

Referenced by MediaWiki\User\UserOptionsManager\loadOptionsFromDb(), and MediaWiki\User\UserOptionsManager\saveOptions().

Member Data Documentation

◆ $defaultOptionsLookup

DefaultOptionsLookup MediaWiki\User\UserOptionsManager::$defaultOptionsLookup
private

◆ $hookRunner

HookRunner MediaWiki\User\UserOptionsManager::$hookRunner
private

Definition at line 85 of file UserOptionsManager.php.

◆ $languageConverterFactory

LanguageConverterFactory MediaWiki\User\UserOptionsManager::$languageConverterFactory
private

◆ $loadBalancer

ILoadBalancer MediaWiki\User\UserOptionsManager::$loadBalancer
private

◆ $logger

LoggerInterface MediaWiki\User\UserOptionsManager::$logger
private

◆ $modifiedOptions

array MediaWiki\User\UserOptionsManager::$modifiedOptions = []
private

options modified withing this request

Definition at line 69 of file UserOptionsManager.php.

Referenced by MediaWiki\User\UserOptionsManager\saveOptions().

◆ $optionsFromDb

array MediaWiki\User\UserOptionsManager::$optionsFromDb = []
private

Cached original user options as fetched from database, no adjustments applied.

Definition at line 82 of file UserOptionsManager.php.

◆ $originalOptionsCache

array MediaWiki\User\UserOptionsManager::$originalOptionsCache = []
private

Cached original user options with all the adjustments like time correction and hook changes applied.

Ready to be returned.

Definition at line 76 of file UserOptionsManager.php.

◆ $queryFlagsUsedForCaching

array MediaWiki\User\UserOptionsManager::$queryFlagsUsedForCaching = []
private

Query flags used to retrieve options from database.

Definition at line 88 of file UserOptionsManager.php.

◆ $serviceOptions

ServiceOptions MediaWiki\User\UserOptionsManager::$serviceOptions
private

Definition at line 54 of file UserOptionsManager.php.

◆ CONSTRUCTOR_OPTIONS

const MediaWiki\User\UserOptionsManager::CONSTRUCTOR_OPTIONS
Initial value:
= [
'HiddenPrefs',
'LocalTZoffset',
]
Access: internal
For use by ServiceWiring

Definition at line 48 of file UserOptionsManager.php.


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