MediaWiki master
DefaultOptionsLookup.php
Go to the documentation of this file.
1<?php
22
34use Wikimedia\Assert\Assert;
36
42
46 public const CONSTRUCTOR_OPTIONS = [
50 ];
51
52 private ServiceOptions $serviceOptions;
53 private LanguageCode $contentLang;
54 private NamespaceInfo $nsInfo;
55 private ConditionalDefaultsLookup $conditionalDefaultsLookup;
56 private UserIdentityLookup $userIdentityLookup;
57
59 private $cache = [];
60
62 private $defaultOptions = null;
63
64 private HookRunner $hookRunner;
65
75 public function __construct(
76 ServiceOptions $options,
77 LanguageCode $contentLang,
78 HookContainer $hookContainer,
79 NamespaceInfo $nsInfo,
80 ConditionalDefaultsLookup $conditionalUserOptionsDefaultsLookup,
81 UserIdentityLookup $userIdentityLookup,
82 UserNameUtils $userNameUtils
83 ) {
84 parent::__construct( $userNameUtils );
85 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
86 $this->serviceOptions = $options;
87 $this->contentLang = $contentLang;
88 $this->hookRunner = new HookRunner( $hookContainer );
89 $this->nsInfo = $nsInfo;
90 $this->conditionalDefaultsLookup = $conditionalUserOptionsDefaultsLookup;
91 $this->userIdentityLookup = $userIdentityLookup;
92 }
93
97 private function getGenericDefaultOptions(): array {
98 if ( $this->defaultOptions !== null ) {
99 return $this->defaultOptions;
100 }
101
102 $this->defaultOptions = $this->serviceOptions->get( MainConfigNames::DefaultUserOptions );
103
104 // Default language setting
105 // NOTE: don't use the content language code since the static default variant would
106 // NOT always be the same as the content language code.
107 $contentLangCode = $this->contentLang->toString();
108 $LangsWithStaticDefaultVariant = LanguageConverter::$languagesWithStaticDefaultVariant;
109 $staticDefaultVariant = $LangsWithStaticDefaultVariant[$contentLangCode] ?? $contentLangCode;
110 $this->defaultOptions['language'] = $contentLangCode;
111 $this->defaultOptions['variant'] = $staticDefaultVariant;
112 foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
113 $staticDefaultVariant = $LangsWithStaticDefaultVariant[$langCode] ?? $langCode;
114 $this->defaultOptions["variant-$langCode"] = $staticDefaultVariant;
115 }
116
117 // NOTE: don't use SearchEngineConfig::getSearchableNamespaces here,
118 // since extensions may change the set of searchable namespaces depending
119 // on user groups/permissions.
120 $nsSearchDefault = $this->serviceOptions->get( MainConfigNames::NamespacesToBeSearchedDefault );
121 foreach ( $this->nsInfo->getValidNamespaces() as $n ) {
122 $this->defaultOptions['searchNs' . $n] = ( $nsSearchDefault[$n] ?? false ) ? 1 : 0;
123 }
124 $this->defaultOptions['skin'] = Skin::normalizeKey(
125 $this->serviceOptions->get( MainConfigNames::DefaultSkin ) );
126
127 $this->hookRunner->onUserGetDefaultOptions( $this->defaultOptions );
128
129 return $this->defaultOptions;
130 }
131
135 public function getDefaultOptions( ?UserIdentity $userIdentity = null ): array {
136 $defaultOptions = $this->getGenericDefaultOptions();
137
138 // If requested, process any conditional defaults
139 if ( $userIdentity ) {
140 $cacheKey = $this->getCacheKey( $userIdentity );
141 if ( isset( $this->cache[$cacheKey] ) ) {
142 return $this->cache[$cacheKey];
143 }
144 $conditionallyDefaultOptions = $this->conditionalDefaultsLookup->getConditionallyDefaultOptions();
145 foreach ( $conditionallyDefaultOptions as $optionName ) {
146 $conditionalDefault = $this->conditionalDefaultsLookup->getOptionDefaultForUser(
147 $optionName, $userIdentity
148 );
149 if ( $conditionalDefault !== null ) {
150 $defaultOptions[$optionName] = $conditionalDefault;
151 }
152 }
153 $this->cache[$cacheKey] = $defaultOptions;
154 }
155
156 return $defaultOptions;
157 }
158
162 public function getOption(
163 UserIdentity $user,
164 string $oname,
165 $defaultOverride = null,
166 bool $ignoreHidden = false,
167 int $queryFlags = IDBAccessObject::READ_NORMAL
168 ) {
169 $this->verifyUsable( $user, __METHOD__ );
170 return $this->getDefaultOption( $oname ) ?? $defaultOverride;
171 }
172
176 public function getOptions(
177 UserIdentity $user,
178 int $flags = 0,
179 int $queryFlags = IDBAccessObject::READ_NORMAL
180 ): array {
181 $this->verifyUsable( $user, __METHOD__ );
182 if ( $flags & self::EXCLUDE_DEFAULTS ) {
183 return [];
184 }
185 return $this->getDefaultOptions();
186 }
187
202 private function verifyUsable( UserIdentity $user, string $fname ) {
203 if ( defined( 'MEDIAWIKI_INSTALL' ) ) {
204 return;
205 }
206 Assert::precondition( !$user->isRegistered(), "$fname called on a registered user" );
207 }
208
209 public function getOptionBatchForUserNames( array $users, string $key ) {
210 $genericDefault = $this->getGenericDefaultOptions()[$key] ?? '';
211 $options = array_fill_keys( $users, $genericDefault );
212 if ( $this->conditionalDefaultsLookup->hasConditionalDefault( $key ) ) {
213 $userIdentities = $this->userIdentityLookup->newSelectQueryBuilder()
214 ->whereUserNames( $users )
215 ->caller( __METHOD__ )
216 ->fetchUserIdentities();
217 foreach ( $userIdentities as $user ) {
218 $options[$user->getName()] = $this->conditionalDefaultsLookup
219 ->getOptionDefaultForUser( $key, $user );
220 }
221 }
222 return $options;
223 }
224}
225
227class_alias( DefaultOptionsLookup::class, 'MediaWiki\\User\\DefaultOptionsLookup' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Methods for dealing with language codes.
Base class for multi-variant language conversion.
A class containing constants representing the names of configuration variables.
const DefaultUserOptions
Name constant for the DefaultUserOptions setting, for use with Config::get()
const DefaultSkin
Name constant for the DefaultSkin setting, for use with Config::get()
const NamespacesToBeSearchedDefault
Name constant for the NamespacesToBeSearchedDefault setting, for use with Config::get()
The base class for all skins.
Definition Skin.php:58
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
A service class to control default user options.
getDefaultOptions(?UserIdentity $userIdentity=null)
Combine the language default options with any site-specific and user-specific defaults and add the de...
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.mixed|null User's current value for the option,...
getOptions(UserIdentity $user, int $flags=0, int $queryFlags=IDBAccessObject::READ_NORMAL)
Get all user's options.array
getOptionBatchForUserNames(array $users, string $key)
Get a single option for a batch of users, given their names.
__construct(ServiceOptions $options, LanguageCode $contentLang, HookContainer $hookContainer, NamespaceInfo $nsInfo, ConditionalDefaultsLookup $conditionalUserOptionsDefaultsLookup, UserIdentityLookup $userIdentityLookup, UserNameUtils $userNameUtils)
Provides access to user options.
UserNameUtils service.
Service for looking up UserIdentity.
Interface for objects representing user identity.
isRegistered()
This must be equivalent to getId() != 0 and is provided for code readability.
Interface for database access objects.