1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\User\Options;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\HookContainer\HookRunner;
use MediaWiki\Language\LanguageCode;
use MediaWiki\Language\LanguageConverter;
use MediaWiki\MainConfigNames;
use MediaWiki\Skin\Skin;
use MediaWiki\Title\NamespaceInfo;
use MediaWiki\User\UserIdentity;
use MediaWiki\User\UserIdentityLookup;
use MediaWiki\User\UserNameUtils;
use Wikimedia\Assert\Assert;
use Wikimedia\Rdbms\IDBAccessObject;
/**
* A service class to control default user options
* @since 1.35
*/
class DefaultOptionsLookup extends UserOptionsLookup {
/**
* @internal For use by ServiceWiring
*/
public const CONSTRUCTOR_OPTIONS = [
MainConfigNames::DefaultSkin,
MainConfigNames::DefaultUserOptions,
MainConfigNames::NamespacesToBeSearchedDefault
];
private ServiceOptions $serviceOptions;
private LanguageCode $contentLang;
private NamespaceInfo $nsInfo;
private ConditionalDefaultsLookup $conditionalDefaultsLookup;
private UserIdentityLookup $userIdentityLookup;
/** @var array Cache of default options by user */
private $cache = [];
/** @var array|null Cached default options */
private $defaultOptions = null;
private HookRunner $hookRunner;
/**
* @param ServiceOptions $options
* @param LanguageCode $contentLang
* @param HookContainer $hookContainer
* @param NamespaceInfo $nsInfo
* @param ConditionalDefaultsLookup $conditionalUserOptionsDefaultsLookup
* @param UserIdentityLookup $userIdentityLookup
* @param UserNameUtils $userNameUtils
*/
public function __construct(
ServiceOptions $options,
LanguageCode $contentLang,
HookContainer $hookContainer,
NamespaceInfo $nsInfo,
ConditionalDefaultsLookup $conditionalUserOptionsDefaultsLookup,
UserIdentityLookup $userIdentityLookup,
UserNameUtils $userNameUtils
) {
parent::__construct( $userNameUtils );
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->serviceOptions = $options;
$this->contentLang = $contentLang;
$this->hookRunner = new HookRunner( $hookContainer );
$this->nsInfo = $nsInfo;
$this->conditionalDefaultsLookup = $conditionalUserOptionsDefaultsLookup;
$this->userIdentityLookup = $userIdentityLookup;
}
/**
* Get default user options from $wgDefaultUserOptions (ignoring any conditional defaults)
*/
private function getGenericDefaultOptions(): array {
if ( $this->defaultOptions !== null ) {
return $this->defaultOptions;
}
$this->defaultOptions = $this->serviceOptions->get( MainConfigNames::DefaultUserOptions );
// Default language setting
// NOTE: don't use the content language code since the static default variant would
// NOT always be the same as the content language code.
$contentLangCode = $this->contentLang->toString();
$LangsWithStaticDefaultVariant = LanguageConverter::$languagesWithStaticDefaultVariant;
$staticDefaultVariant = $LangsWithStaticDefaultVariant[$contentLangCode] ?? $contentLangCode;
$this->defaultOptions['language'] = $contentLangCode;
$this->defaultOptions['variant'] = $staticDefaultVariant;
foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
$staticDefaultVariant = $LangsWithStaticDefaultVariant[$langCode] ?? $langCode;
$this->defaultOptions["variant-$langCode"] = $staticDefaultVariant;
}
// NOTE: don't use SearchEngineConfig::getSearchableNamespaces here,
// since extensions may change the set of searchable namespaces depending
// on user groups/permissions.
$nsSearchDefault = $this->serviceOptions->get( MainConfigNames::NamespacesToBeSearchedDefault );
foreach ( $this->nsInfo->getValidNamespaces() as $n ) {
$this->defaultOptions['searchNs' . $n] = ( $nsSearchDefault[$n] ?? false ) ? 1 : 0;
}
$this->defaultOptions['skin'] = Skin::normalizeKey(
$this->serviceOptions->get( MainConfigNames::DefaultSkin ) );
$this->hookRunner->onUserGetDefaultOptions( $this->defaultOptions );
return $this->defaultOptions;
}
/**
* @inheritDoc
*/
public function getDefaultOptions( ?UserIdentity $userIdentity = null ): array {
$defaultOptions = $this->getGenericDefaultOptions();
// If requested, process any conditional defaults
if ( $userIdentity ) {
$cacheKey = $this->getCacheKey( $userIdentity );
if ( isset( $this->cache[$cacheKey] ) ) {
return $this->cache[$cacheKey];
}
$conditionallyDefaultOptions = $this->conditionalDefaultsLookup->getConditionallyDefaultOptions();
foreach ( $conditionallyDefaultOptions as $optionName ) {
$conditionalDefault = $this->conditionalDefaultsLookup->getOptionDefaultForUser(
$optionName, $userIdentity
);
if ( $conditionalDefault !== null ) {
$defaultOptions[$optionName] = $conditionalDefault;
}
}
$this->cache[$cacheKey] = $defaultOptions;
}
return $defaultOptions;
}
/**
* @inheritDoc
*/
public function getOption(
UserIdentity $user,
string $oname,
$defaultOverride = null,
bool $ignoreHidden = false,
int $queryFlags = IDBAccessObject::READ_NORMAL
) {
$this->verifyUsable( $user, __METHOD__ );
return $this->getDefaultOption( $oname ) ?? $defaultOverride;
}
/**
* @inheritDoc
*/
public function getOptions(
UserIdentity $user,
int $flags = 0,
int $queryFlags = IDBAccessObject::READ_NORMAL
): array {
$this->verifyUsable( $user, __METHOD__ );
if ( $flags & self::EXCLUDE_DEFAULTS ) {
return [];
}
return $this->getDefaultOptions();
}
/**
* Checks if the DefaultOptionsLookup is usable as an instance of UserOptionsLookup.
*
* It only makes sense in an installer context when UserOptionsManager cannot be yet instantiated
* as the database is not available. Thus, this can only be called for an anon user,
* calling under different circumstances indicates a bug, or that a system user is being used.
*
* The only exception to this is database-less PHPUnit tests, where sometimes fake registered users are
* used and end up being passed to this class. This should not be considered a bug, and using the default
* preferences in this scenario is probably the intended behaviour.
*
* @param UserIdentity $user
* @param string $fname
*/
private function verifyUsable( UserIdentity $user, string $fname ) {
if ( defined( 'MEDIAWIKI_INSTALL' ) ) {
return;
}
Assert::precondition( !$user->isRegistered(), "$fname called on a registered user" );
}
public function getOptionBatchForUserNames( array $users, string $key ) {
$genericDefault = $this->getGenericDefaultOptions()[$key] ?? '';
$options = array_fill_keys( $users, $genericDefault );
if ( $this->conditionalDefaultsLookup->hasConditionalDefault( $key ) ) {
$userIdentities = $this->userIdentityLookup->newSelectQueryBuilder()
->whereUserNames( $users )
->caller( __METHOD__ )
->fetchUserIdentities();
foreach ( $userIdentities as $user ) {
$options[$user->getName()] = $this->conditionalDefaultsLookup
->getOptionDefaultForUser( $key, $user );
}
}
return $options;
}
}
/** @deprecated class alias since 1.42 */
class_alias( DefaultOptionsLookup::class, 'MediaWiki\\User\\DefaultOptionsLookup' );
|