MediaWiki master
DefaultOptionsLookup.php
Go to the documentation of this file.
1<?php
8
20use Wikimedia\Assert\Assert;
22
28
32 public const CONSTRUCTOR_OPTIONS = [
36 ];
37
38 private ServiceOptions $serviceOptions;
39 private LanguageCode $contentLang;
40 private NamespaceInfo $nsInfo;
41 private ConditionalDefaultsLookup $conditionalDefaultsLookup;
42 private UserIdentityLookup $userIdentityLookup;
43
45 private $cache = [];
46
48 private $defaultOptions = null;
49
50 private HookRunner $hookRunner;
51
61 public function __construct(
62 ServiceOptions $options,
63 LanguageCode $contentLang,
64 HookContainer $hookContainer,
65 NamespaceInfo $nsInfo,
66 ConditionalDefaultsLookup $conditionalUserOptionsDefaultsLookup,
67 UserIdentityLookup $userIdentityLookup,
68 UserNameUtils $userNameUtils
69 ) {
70 parent::__construct( $userNameUtils );
71 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
72 $this->serviceOptions = $options;
73 $this->contentLang = $contentLang;
74 $this->hookRunner = new HookRunner( $hookContainer );
75 $this->nsInfo = $nsInfo;
76 $this->conditionalDefaultsLookup = $conditionalUserOptionsDefaultsLookup;
77 $this->userIdentityLookup = $userIdentityLookup;
78 }
79
83 private function getGenericDefaultOptions(): array {
84 if ( $this->defaultOptions !== null ) {
85 return $this->defaultOptions;
86 }
87
88 $this->defaultOptions = $this->serviceOptions->get( MainConfigNames::DefaultUserOptions );
89
90 // Default language setting
91 // NOTE: don't use the content language code since the static default variant would
92 // NOT always be the same as the content language code.
93 $contentLangCode = $this->contentLang->toString();
94 $LangsWithStaticDefaultVariant = LanguageConverter::$languagesWithStaticDefaultVariant;
95 $staticDefaultVariant = $LangsWithStaticDefaultVariant[$contentLangCode] ?? $contentLangCode;
96 $this->defaultOptions['language'] = $contentLangCode;
97 $this->defaultOptions['variant'] = $staticDefaultVariant;
98 foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
99 $staticDefaultVariant = $LangsWithStaticDefaultVariant[$langCode] ?? $langCode;
100 $this->defaultOptions["variant-$langCode"] = $staticDefaultVariant;
101 }
102
103 // NOTE: don't use SearchEngineConfig::getSearchableNamespaces here,
104 // since extensions may change the set of searchable namespaces depending
105 // on user groups/permissions.
106 $nsSearchDefault = $this->serviceOptions->get( MainConfigNames::NamespacesToBeSearchedDefault );
107 foreach ( $this->nsInfo->getValidNamespaces() as $n ) {
108 $this->defaultOptions['searchNs' . $n] = ( $nsSearchDefault[$n] ?? false ) ? 1 : 0;
109 }
110 $this->defaultOptions['skin'] = Skin::normalizeKey(
111 $this->serviceOptions->get( MainConfigNames::DefaultSkin ) );
112
113 $this->hookRunner->onUserGetDefaultOptions( $this->defaultOptions );
114
115 return $this->defaultOptions;
116 }
117
121 public function getDefaultOptions( ?UserIdentity $userIdentity = null ): array {
122 $defaultOptions = $this->getGenericDefaultOptions();
123
124 // If requested, process any conditional defaults
125 if ( $userIdentity ) {
126 $cacheKey = $this->getCacheKey( $userIdentity );
127 if ( isset( $this->cache[$cacheKey] ) ) {
128 return $this->cache[$cacheKey];
129 }
130 $conditionallyDefaultOptions = $this->conditionalDefaultsLookup->getConditionallyDefaultOptions();
131 foreach ( $conditionallyDefaultOptions as $optionName ) {
132 $conditionalDefault = $this->conditionalDefaultsLookup->getOptionDefaultForUser(
133 $optionName, $userIdentity
134 );
135 if ( $conditionalDefault !== null ) {
136 $defaultOptions[$optionName] = $conditionalDefault;
137 }
138 }
139 $this->cache[$cacheKey] = $defaultOptions;
140 }
141
142 return $defaultOptions;
143 }
144
148 public function getOption(
149 UserIdentity $user,
150 string $oname,
151 $defaultOverride = null,
152 bool $ignoreHidden = false,
153 int $queryFlags = IDBAccessObject::READ_NORMAL
154 ) {
155 $this->verifyUsable( $user, __METHOD__ );
156 return $this->getDefaultOption( $oname ) ?? $defaultOverride;
157 }
158
162 public function getOptions(
163 UserIdentity $user,
164 int $flags = 0,
165 int $queryFlags = IDBAccessObject::READ_NORMAL
166 ): array {
167 $this->verifyUsable( $user, __METHOD__ );
168 if ( $flags & self::EXCLUDE_DEFAULTS ) {
169 return [];
170 }
171 return $this->getDefaultOptions();
172 }
173
188 private function verifyUsable( UserIdentity $user, string $fname ) {
189 if ( defined( 'MEDIAWIKI_INSTALL' ) ) {
190 return;
191 }
192 Assert::precondition( !$user->isRegistered(), "$fname called on a registered user" );
193 }
194
196 public function getOptionBatchForUserNames( array $users, string $key ) {
197 $genericDefault = $this->getGenericDefaultOptions()[$key] ?? '';
198 $options = array_fill_keys( $users, $genericDefault );
199 if ( $this->conditionalDefaultsLookup->hasConditionalDefault( $key ) ) {
200 $userIdentities = $this->userIdentityLookup->newSelectQueryBuilder()
201 ->whereUserNames( $users )
202 ->caller( __METHOD__ )
203 ->fetchUserIdentities();
204 foreach ( $userIdentities as $user ) {
205 $options[$user->getName()] = $this->conditionalDefaultsLookup
206 ->getOptionDefaultForUser( $key, $user );
207 }
208 }
209 return $options;
210 }
211}
212
214class_alias( DefaultOptionsLookup::class, 'MediaWiki\\User\\DefaultOptionsLookup' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
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:52
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.Results are uncached. Use getOption() to ...
__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.