Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
42.11% covered (danger)
42.11%
8 / 19
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserOptionsLookup
44.44% covered (danger)
44.44%
8 / 18
33.33% covered (danger)
33.33%
2 / 6
31.75
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDefaultOptions
n/a
0 / 0
n/a
0 / 0
0
 getDefaultOption
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getOption
n/a
0 / 0
n/a
0 / 0
0
 getOptions
n/a
0 / 0
n/a
0 / 0
0
 getBoolOption
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getIntOption
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 getCacheKey
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 isOptionGlobal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOptionBatchForUserNames
n/a
0 / 0
n/a
0 / 0
0
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\User\Options;
8
9use MediaWiki\User\UserIdentity;
10use MediaWiki\User\UserNameUtils;
11use Wikimedia\Rdbms\IDBAccessObject;
12
13/**
14 * Provides access to user options
15 * @since 1.35
16 */
17abstract class UserOptionsLookup {
18
19    /**
20     * Exclude user options that are set to their default value.
21     */
22    public const EXCLUDE_DEFAULTS = 1;
23
24    /**
25     * The suffix appended to preference names for the associated preference
26     * that tracks whether they have a local override.
27     * @since 1.43
28     */
29    public const LOCAL_EXCEPTION_SUFFIX = '-local-exception';
30
31    public function __construct(
32        private readonly UserNameUtils $userNameUtils,
33    ) {
34    }
35
36    /**
37     * Combine the language default options with any site-specific and user-specific defaults
38     * and add the default language variants.
39     *
40     * @param UserIdentity|null $userIdentity User to look the default up for; set to null to
41     * ignore any user-specific defaults (since 1.42)
42     * @return array
43     */
44    abstract public function getDefaultOptions( ?UserIdentity $userIdentity = null ): array;
45
46    /**
47     * Get a given default option value.
48     *
49     * @param string $opt Name of option to retrieve
50     * @param UserIdentity|null $userIdentity User to look the defaults up for; set to null to
51     * ignore any user-specific defaults (since 1.42)
52     * @return mixed|null Default option value
53     */
54    public function getDefaultOption(
55        string $opt,
56        ?UserIdentity $userIdentity = null
57    ) {
58        $defaultOptions = $this->getDefaultOptions( $userIdentity );
59        return $defaultOptions[$opt] ?? null;
60    }
61
62    /**
63     * Get the user's current setting for a given option.
64     *
65     * @param UserIdentity $user The user to get the option for
66     * @param string $oname The option to check
67     * @param mixed|null $defaultOverride A default value returned if the option does not exist
68     * @param bool $ignoreHidden Whether to ignore the effects of $wgHiddenPrefs
69     * @param int $queryFlags A bit field composed of READ_XXX flags
70     * @return mixed|null User's current value for the option,
71     *   Note that while option values retrieved from the database are always strings, default
72     *   values and values set within the current request and not yet saved may be of another type.
73     * @see getBoolOption()
74     * @see getIntOption()
75     */
76    abstract public function getOption(
77        UserIdentity $user,
78        string $oname,
79        $defaultOverride = null,
80        bool $ignoreHidden = false,
81        int $queryFlags = IDBAccessObject::READ_NORMAL
82    );
83
84    /**
85     * Get all user's options
86     *
87     * @param UserIdentity $user The user to get the option for
88     * @param int $flags Bitwise combination of:
89     *   - UserOptionsManager::EXCLUDE_DEFAULTS: Exclude user options that are set to the default
90     *     value. Options that are set to their conditionally default value are not excluded.
91     * @param int $queryFlags A bit field composed of READ_XXX flags
92     * @return array
93     */
94    abstract public function getOptions(
95        UserIdentity $user,
96        int $flags = 0,
97        int $queryFlags = IDBAccessObject::READ_NORMAL
98    ): array;
99
100    /**
101     * Get the user's current setting for a given option, as a boolean value.
102     *
103     * @param UserIdentity $user The user to get the option for
104     * @param string $oname The option to check
105     * @param int $queryFlags A bit field composed of READ_XXX flags
106     * @return bool User's current value for the option
107     * @see getOption()
108     */
109    public function getBoolOption(
110        UserIdentity $user,
111        string $oname,
112        int $queryFlags = IDBAccessObject::READ_NORMAL
113    ): bool {
114        return (bool)$this->getOption(
115            $user, $oname, null, false, $queryFlags );
116    }
117
118    /**
119     * Get the user's current setting for a given option, as an integer value.
120     *
121     * @param UserIdentity $user The user to get the option for
122     * @param string $oname The option to check
123     * @param int $defaultOverride A default value returned if the option does not exist
124     * @param int $queryFlags A bit field composed of READ_XXX flags
125     * @return int User's current value for the option
126     * @see getOption()
127     */
128    public function getIntOption(
129        UserIdentity $user,
130        string $oname,
131        int $defaultOverride = 0,
132        int $queryFlags = IDBAccessObject::READ_NORMAL
133    ): int {
134        $val = $this->getOption(
135            $user, $oname, $defaultOverride, false, $queryFlags );
136        if ( $val == '' ) {
137            $val = $defaultOverride;
138        }
139        return intval( $val );
140    }
141
142    /**
143     * Get a cache key for a user
144     * @param UserIdentity $user
145     * @return string
146     */
147    protected function getCacheKey( UserIdentity $user ): string {
148        $name = $user->getName();
149        if ( $this->userNameUtils->isIP( $name ) || $this->userNameUtils->isTemp( $name ) ) {
150            // IP and temporary users may not have custom preferences, so they can share a key
151            return 'anon';
152        } elseif ( $user->isRegistered() ) {
153            return "u:{$user->getId()}";
154        } else {
155            // Allow users with no local account to have preferences provided by alternative
156            // UserOptionsStore implementations (e.g. in GlobalPreferences)
157            $canonical = $this->userNameUtils->getCanonical( $name ) ?: $name;
158            return "a:$canonical";
159        }
160    }
161
162    /**
163     * Determine if a user option came from a source other than the local store
164     * or the defaults. If this is true, setting the option will be ignored
165     * unless GLOBAL_OVERRIDE or GLOBAL_UPDATE is passed to setOption().
166     *
167     * @param UserIdentity $user
168     * @param string $key
169     * @return bool
170     */
171    public function isOptionGlobal( UserIdentity $user, string $key ) {
172        return false;
173    }
174
175    /**
176     * Get a single option for a batch of users, given their names.
177     *
178     * Results are uncached. Use getOption() to get options with a per-user
179     * cache.
180     *
181     * User names are used because that's what GenderCache has. If you're
182     * calling this and you're not GenderCache, consider adding a method
183     * taking an array of UserIdentity objects instead.
184     *
185     * @since 1.44
186     * @param string[] $users A normalized list of usernames
187     * @param string $key The option to get
188     * @return array The option values, indexed by the provided usernames
189     */
190    abstract public function getOptionBatchForUserNames( array $users, string $key );
191}
192/** @deprecated class alias since 1.42 */
193class_alias( UserOptionsLookup::class, 'MediaWiki\\User\\UserOptionsLookup' );