MediaWiki master
DefaultOptionsLookup.php
Go to the documentation of this file.
1<?php
22
32use Skin;
33use Wikimedia\Assert\Assert;
35
41
45 public const CONSTRUCTOR_OPTIONS = [
49 ];
50
51 private ServiceOptions $serviceOptions;
52 private LanguageCode $contentLang;
53 private NamespaceInfo $nsInfo;
54 private ConditionalDefaultsLookup $conditionalDefaultsLookup;
55 private UserIdentityLookup $userIdentityLookup;
56
58 private $defaultOptions = null;
59
60 private HookRunner $hookRunner;
61
70 public function __construct(
71 ServiceOptions $options,
72 LanguageCode $contentLang,
73 HookContainer $hookContainer,
74 NamespaceInfo $nsInfo,
75 ConditionalDefaultsLookup $conditionalUserOptionsDefaultsLookup,
76 UserIdentityLookup $userIdentityLookup
77 ) {
78 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
79 $this->serviceOptions = $options;
80 $this->contentLang = $contentLang;
81 $this->hookRunner = new HookRunner( $hookContainer );
82 $this->nsInfo = $nsInfo;
83 $this->conditionalDefaultsLookup = $conditionalUserOptionsDefaultsLookup;
84 $this->userIdentityLookup = $userIdentityLookup;
85 }
86
90 private function getGenericDefaultOptions(): array {
91 if ( $this->defaultOptions !== null ) {
92 return $this->defaultOptions;
93 }
94
95 $this->defaultOptions = $this->serviceOptions->get( MainConfigNames::DefaultUserOptions );
96
97 // Default language setting
98 // NOTE: don't use the content language code since the static default variant would
99 // NOT always be the same as the content language code.
100 $contentLangCode = $this->contentLang->toString();
101 $LangsWithStaticDefaultVariant = LanguageConverter::$languagesWithStaticDefaultVariant;
102 $staticDefaultVariant = $LangsWithStaticDefaultVariant[$contentLangCode] ?? $contentLangCode;
103 $this->defaultOptions['language'] = $contentLangCode;
104 $this->defaultOptions['variant'] = $staticDefaultVariant;
105 foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
106 $staticDefaultVariant = $LangsWithStaticDefaultVariant[$langCode] ?? $langCode;
107 $this->defaultOptions["variant-$langCode"] = $staticDefaultVariant;
108 }
109
110 // NOTE: don't use SearchEngineConfig::getSearchableNamespaces here,
111 // since extensions may change the set of searchable namespaces depending
112 // on user groups/permissions.
113 $nsSearchDefault = $this->serviceOptions->get( MainConfigNames::NamespacesToBeSearchedDefault );
114 foreach ( $this->nsInfo->getValidNamespaces() as $n ) {
115 $this->defaultOptions['searchNs' . $n] = ( $nsSearchDefault[$n] ?? false ) ? 1 : 0;
116 }
117 $this->defaultOptions['skin'] = Skin::normalizeKey(
118 $this->serviceOptions->get( MainConfigNames::DefaultSkin ) );
119
120 $this->hookRunner->onUserGetDefaultOptions( $this->defaultOptions );
121
122 return $this->defaultOptions;
123 }
124
128 public function getDefaultOptions( ?UserIdentity $userIdentity = null ): array {
129 $defaultOptions = $this->getGenericDefaultOptions();
130
131 // If requested, process any conditional defaults
132 if ( $userIdentity ) {
133 $conditionallyDefaultOptions = $this->conditionalDefaultsLookup->getConditionallyDefaultOptions();
134 foreach ( $conditionallyDefaultOptions as $optionName ) {
135 $conditionalDefault = $this->conditionalDefaultsLookup->getOptionDefaultForUser(
136 $optionName, $userIdentity
137 );
138 if ( $conditionalDefault !== null ) {
139 $defaultOptions[$optionName] = $conditionalDefault;
140 }
141 }
142 }
143
144 return $defaultOptions;
145 }
146
150 public function getOption(
151 UserIdentity $user,
152 string $oname,
153 $defaultOverride = null,
154 bool $ignoreHidden = false,
155 int $queryFlags = IDBAccessObject::READ_NORMAL
156 ) {
157 $this->verifyUsable( $user, __METHOD__ );
158 return $this->getDefaultOption( $oname ) ?? $defaultOverride;
159 }
160
164 public function getOptions(
165 UserIdentity $user,
166 int $flags = 0,
167 int $queryFlags = IDBAccessObject::READ_NORMAL
168 ): array {
169 $this->verifyUsable( $user, __METHOD__ );
170 if ( $flags & self::EXCLUDE_DEFAULTS ) {
171 return [];
172 }
173 return $this->getDefaultOptions();
174 }
175
190 private function verifyUsable( UserIdentity $user, string $fname ) {
191 if ( defined( 'MEDIAWIKI_INSTALL' ) ) {
192 return;
193 }
194 Assert::precondition( !$user->isRegistered(), "$fname called on a registered user" );
195 }
196
197 public function getOptionBatchForUserNames( array $users, string $key ) {
198 $genericDefault = $this->getGenericDefaultOptions()[$key] ?? '';
199 $options = array_fill_keys( $users, $genericDefault );
200 if ( $this->conditionalDefaultsLookup->hasConditionalDefault( $key ) ) {
201 $userIdentities = $this->userIdentityLookup->newSelectQueryBuilder()
202 ->whereUserNames( $users )
203 ->caller( __METHOD__ )
204 ->fetchUserIdentities();
205 foreach ( $userIdentities as $user ) {
206 $options[$user->getName()] = $this->conditionalDefaultsLookup
207 ->getOptionDefaultForUser( $key, $user );
208 }
209 }
210 return $options;
211 }
212}
213
215class_alias( DefaultOptionsLookup::class, 'MediaWiki\\User\\DefaultOptionsLookup' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
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()
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.
__construct(ServiceOptions $options, LanguageCode $contentLang, HookContainer $hookContainer, NamespaceInfo $nsInfo, ConditionalDefaultsLookup $conditionalUserOptionsDefaultsLookup, UserIdentityLookup $userIdentityLookup)
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.
Provides access to user options.
The base class for all skins.
Definition Skin.php:63
static normalizeKey(string $key)
Normalize a skin preference value to a form that can be loaded.
Definition Skin.php:227
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.