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 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\User\Options;
22
23use MediaWiki\User\UserIdentity;
24use MediaWiki\User\UserNameUtils;
25use Wikimedia\Rdbms\IDBAccessObject;
26
27/**
28 * Provides access to user options
29 * @since 1.35
30 */
31abstract class UserOptionsLookup {
32
33    /**
34     * Exclude user options that are set to their default value.
35     */
36    public const EXCLUDE_DEFAULTS = 1;
37
38    /**
39     * The suffix appended to preference names for the associated preference
40     * that tracks whether they have a local override.
41     * @since 1.43
42     */
43    public const LOCAL_EXCEPTION_SUFFIX = '-local-exception';
44
45    private UserNameUtils $userNameUtils;
46
47    public function __construct( UserNameUtils $userNameUtils ) {
48        $this->userNameUtils = $userNameUtils;
49    }
50
51    /**
52     * Combine the language default options with any site-specific and user-specific defaults
53     * and add the default language variants.
54     *
55     * @param UserIdentity|null $userIdentity User to look the default up for; set to null to
56     * ignore any user-specific defaults (since 1.42)
57     * @return array
58     */
59    abstract public function getDefaultOptions( ?UserIdentity $userIdentity = null ): array;
60
61    /**
62     * Get a given default option value.
63     *
64     * @param string $opt Name of option to retrieve
65     * @param UserIdentity|null $userIdentity User to look the defaults up for; set to null to
66     * ignore any user-specific defaults (since 1.42)
67     * @return mixed|null Default option value
68     */
69    public function getDefaultOption(
70        string $opt,
71        ?UserIdentity $userIdentity = null
72    ) {
73        $defaultOptions = $this->getDefaultOptions( $userIdentity );
74        return $defaultOptions[$opt] ?? null;
75    }
76
77    /**
78     * Get the user's current setting for a given option.
79     *
80     * @param UserIdentity $user The user to get the option for
81     * @param string $oname The option to check
82     * @param mixed|null $defaultOverride A default value returned if the option does not exist
83     * @param bool $ignoreHidden Whether to ignore the effects of $wgHiddenPrefs
84     * @param int $queryFlags A bit field composed of READ_XXX flags
85     * @return mixed|null User's current value for the option,
86     *   Note that while option values retrieved from the database are always strings, default
87     *   values and values set within the current request and not yet saved may be of another type.
88     * @see getBoolOption()
89     * @see getIntOption()
90     */
91    abstract public function getOption(
92        UserIdentity $user,
93        string $oname,
94        $defaultOverride = null,
95        bool $ignoreHidden = false,
96        int $queryFlags = IDBAccessObject::READ_NORMAL
97    );
98
99    /**
100     * Get all user's options
101     *
102     * @param UserIdentity $user The user to get the option for
103     * @param int $flags Bitwise combination of:
104     *   UserOptionsManager::EXCLUDE_DEFAULTS  Exclude user options that are set
105     *                                         to the default value. Options
106     *                                         that are set to their conditionally
107     *                                         default value are not excluded.
108     * @param int $queryFlags A bit field composed of READ_XXX flags
109     * @return array
110     */
111    abstract public function getOptions(
112        UserIdentity $user,
113        int $flags = 0,
114        int $queryFlags = IDBAccessObject::READ_NORMAL
115    ): array;
116
117    /**
118     * Get the user's current setting for a given option, as a boolean value.
119     *
120     * @param UserIdentity $user The user to get the option for
121     * @param string $oname The option to check
122     * @param int $queryFlags A bit field composed of READ_XXX flags
123     * @return bool User's current value for the option
124     * @see getOption()
125     */
126    public function getBoolOption(
127        UserIdentity $user,
128        string $oname,
129        int $queryFlags = IDBAccessObject::READ_NORMAL
130    ): bool {
131        return (bool)$this->getOption(
132            $user, $oname, null, false, $queryFlags );
133    }
134
135    /**
136     * Get the user's current setting for a given option, as an integer value.
137     *
138     * @param UserIdentity $user The user to get the option for
139     * @param string $oname The option to check
140     * @param int $defaultOverride A default value returned if the option does not exist
141     * @param int $queryFlags A bit field composed of READ_XXX flags
142     * @return int User's current value for the option
143     * @see getOption()
144     */
145    public function getIntOption(
146        UserIdentity $user,
147        string $oname,
148        int $defaultOverride = 0,
149        int $queryFlags = IDBAccessObject::READ_NORMAL
150    ): int {
151        $val = $this->getOption(
152            $user, $oname, $defaultOverride, false, $queryFlags );
153        if ( $val == '' ) {
154            $val = $defaultOverride;
155        }
156        return intval( $val );
157    }
158
159    /**
160     * Get a cache key for a user
161     * @param UserIdentity $user
162     * @return string
163     */
164    protected function getCacheKey( UserIdentity $user ): string {
165        $name = $user->getName();
166        if ( $this->userNameUtils->isIP( $name ) || $this->userNameUtils->isTemp( $name ) ) {
167            // IP and temporary users may not have custom preferences, so they can share a key
168            return 'anon';
169        } elseif ( $user->isRegistered() ) {
170            return "u:{$user->getId()}";
171        } else {
172            // Allow users with no local account to have preferences provided by alternative
173            // UserOptionsStore implementations (e.g. in GlobalPreferences)
174            $canonical = $this->userNameUtils->getCanonical( $name ) ?: $name;
175            return "a:$canonical";
176        }
177    }
178
179    /**
180     * Determine if a user option came from a source other than the local store
181     * or the defaults. If this is true, setting the option will be ignored
182     * unless GLOBAL_OVERRIDE or GLOBAL_UPDATE is passed to setOption().
183     *
184     * @param UserIdentity $user
185     * @param string $key
186     * @return bool
187     */
188    public function isOptionGlobal( UserIdentity $user, string $key ) {
189        return false;
190    }
191
192    /**
193     * Get a single option for a batch of users, given their names.
194     *
195     * Results are uncached. Use getOption() to get options with a per-user
196     * cache.
197     *
198     * User names are used because that's what GenderCache has. If you're
199     * calling this and you're not GenderCache, consider adding a method
200     * taking an array of UserIdentity objects instead.
201     *
202     * @since 1.44
203     * @param string[] $users A normalized list of usernames
204     * @param string $key The option to get
205     * @return array The option values, indexed by the provided usernames
206     */
207    abstract public function getOptionBatchForUserNames( array $users, string $key );
208}
209/** @deprecated class alias since 1.42 */
210class_alias( UserOptionsLookup::class, 'MediaWiki\\User\\UserOptionsLookup' );