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
39 private $cache = [];
40
42 private $defaultOptions = null;
43
44 private readonly HookRunner $hookRunner;
45
46 public function __construct(
47 private readonly ServiceOptions $serviceOptions,
48 private readonly LanguageCode $contentLang,
49 HookContainer $hookContainer,
50 private readonly NamespaceInfo $nsInfo,
51 private readonly ConditionalDefaultsLookup $conditionalDefaultsLookup,
52 private readonly UserIdentityLookup $userIdentityLookup,
53 UserNameUtils $userNameUtils,
54 ) {
55 parent::__construct( $userNameUtils );
56 $serviceOptions->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
57 $this->hookRunner = new HookRunner( $hookContainer );
58 }
59
63 private function getGenericDefaultOptions(): array {
64 if ( $this->defaultOptions !== null ) {
65 return $this->defaultOptions;
66 }
67
68 $this->defaultOptions = $this->serviceOptions->get( MainConfigNames::DefaultUserOptions );
69
70 // Default language setting
71 // NOTE: don't use the content language code since the static default variant would
72 // NOT always be the same as the content language code.
73 $contentLangCode = $this->contentLang->toString();
74 $LangsWithStaticDefaultVariant = LanguageConverter::$languagesWithStaticDefaultVariant;
75 $staticDefaultVariant = $LangsWithStaticDefaultVariant[$contentLangCode] ?? $contentLangCode;
76 $this->defaultOptions['language'] = $contentLangCode;
77 $this->defaultOptions['variant'] = $staticDefaultVariant;
78 foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
79 $staticDefaultVariant = $LangsWithStaticDefaultVariant[$langCode] ?? $langCode;
80 $this->defaultOptions["variant-$langCode"] = $staticDefaultVariant;
81 }
82
83 // NOTE: don't use SearchEngineConfig::getSearchableNamespaces here,
84 // since extensions may change the set of searchable namespaces depending
85 // on user groups/permissions.
86 $nsSearchDefault = $this->serviceOptions->get( MainConfigNames::NamespacesToBeSearchedDefault );
87 foreach ( $this->nsInfo->getValidNamespaces() as $n ) {
88 $this->defaultOptions['searchNs' . $n] = ( $nsSearchDefault[$n] ?? false ) ? 1 : 0;
89 }
90 $this->defaultOptions['skin'] = Skin::normalizeKey(
91 $this->serviceOptions->get( MainConfigNames::DefaultSkin ) );
92
93 $this->hookRunner->onUserGetDefaultOptions( $this->defaultOptions );
94
95 return $this->defaultOptions;
96 }
97
101 public function getDefaultOptions( ?UserIdentity $userIdentity = null ): array {
102 $defaultOptions = $this->getGenericDefaultOptions();
103
104 // If requested, process any conditional defaults
105 if ( $userIdentity ) {
106 $cacheKey = $this->getCacheKey( $userIdentity );
107 if ( isset( $this->cache[$cacheKey] ) ) {
108 return $this->cache[$cacheKey];
109 }
110 $conditionallyDefaultOptions = $this->conditionalDefaultsLookup->getConditionallyDefaultOptions();
111 foreach ( $conditionallyDefaultOptions as $optionName ) {
112 $conditionalDefault = $this->conditionalDefaultsLookup->getOptionDefaultForUser(
113 $optionName, $userIdentity
114 );
115 if ( $conditionalDefault !== null ) {
116 $defaultOptions[$optionName] = $conditionalDefault;
117 }
118 }
119 $this->cache[$cacheKey] = $defaultOptions;
120 }
121
122 return $defaultOptions;
123 }
124
128 public function getOption(
129 UserIdentity $user,
130 string $oname,
131 $defaultOverride = null,
132 bool $ignoreHidden = false,
133 int $queryFlags = IDBAccessObject::READ_NORMAL
134 ) {
135 $this->verifyUsable( $user, __METHOD__ );
136 return $this->getDefaultOption( $oname ) ?? $defaultOverride;
137 }
138
142 public function getOptions(
143 UserIdentity $user,
144 int $flags = 0,
145 int $queryFlags = IDBAccessObject::READ_NORMAL
146 ): array {
147 $this->verifyUsable( $user, __METHOD__ );
148 if ( $flags & self::EXCLUDE_DEFAULTS ) {
149 return [];
150 }
151 return $this->getDefaultOptions();
152 }
153
168 private function verifyUsable( UserIdentity $user, string $fname ) {
169 if ( defined( 'MEDIAWIKI_INSTALL' ) ) {
170 return;
171 }
172 Assert::precondition( !$user->isRegistered(), "$fname called on a registered user" );
173 }
174
176 public function getOptionBatchForUserNames( array $users, string $key ) {
177 $genericDefault = $this->getGenericDefaultOptions()[$key] ?? '';
178 $options = array_fill_keys( $users, $genericDefault );
179 if ( $this->conditionalDefaultsLookup->hasConditionalDefault( $key ) ) {
180 $userIdentities = $this->userIdentityLookup->newSelectQueryBuilder()
181 ->whereUserNames( $users )
182 ->caller( __METHOD__ )
183 ->fetchUserIdentities();
184 foreach ( $userIdentities as $user ) {
185 $options[$user->getName()] = $this->conditionalDefaultsLookup
186 ->getOptionDefaultForUser( $key, $user );
187 }
188 }
189 return $options;
190 }
191}
192
194class_alias( DefaultOptionsLookup::class, 'MediaWiki\\User\\DefaultOptionsLookup' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:71
A class for passing options to services.
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:54
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
__construct(private readonly ServiceOptions $serviceOptions, private readonly LanguageCode $contentLang, HookContainer $hookContainer, private readonly NamespaceInfo $nsInfo, private readonly ConditionalDefaultsLookup $conditionalDefaultsLookup, private readonly UserIdentityLookup $userIdentityLookup, UserNameUtils $userNameUtils,)
getOptionBatchForUserNames(array $users, string $key)
Get a single option for a batch of users, given their names.Results are uncached. Use getOption() to ...
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.