MediaWiki master
DefaultOptionsLookup.php
Go to the documentation of this file.
1<?php
22
24use Language;
32use Skin;
33use Wikimedia\Assert\Assert;
34
40
44 public const CONSTRUCTOR_OPTIONS = [
48 ];
49
50 private ServiceOptions $serviceOptions;
51 private Language $contentLang;
52 private NamespaceInfo $nsInfo;
53 private ConditionalDefaultsLookup $conditionalDefaultsLookup;
54
56 private $defaultOptions = null;
57
58 private HookRunner $hookRunner;
59
67 public function __construct(
68 ServiceOptions $options,
69 Language $contentLang,
70 HookContainer $hookContainer,
71 NamespaceInfo $nsInfo,
72 ConditionalDefaultsLookup $conditionalUserOptionsDefaultsLookup
73 ) {
74 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
75 $this->serviceOptions = $options;
76 $this->contentLang = $contentLang;
77 $this->hookRunner = new HookRunner( $hookContainer );
78 $this->nsInfo = $nsInfo;
79 $this->conditionalDefaultsLookup = $conditionalUserOptionsDefaultsLookup;
80 }
81
87 private function getGenericDefaultOptions(): array {
88 if ( $this->defaultOptions !== null ) {
89 return $this->defaultOptions;
90 }
91
92 $this->defaultOptions = $this->serviceOptions->get( MainConfigNames::DefaultUserOptions );
93
94 // Default language setting
95 // NOTE: don't use the content language code since the static default variant would
96 // NOT always be the same as the content language code.
97 $contentLangCode = $this->contentLang->getCode();
98 $LangsWithStaticDefaultVariant = LanguageConverter::$languagesWithStaticDefaultVariant;
99 $staticDefaultVariant = $LangsWithStaticDefaultVariant[$contentLangCode] ?? $contentLangCode;
100 $this->defaultOptions['language'] = $contentLangCode;
101 $this->defaultOptions['variant'] = $staticDefaultVariant;
102 foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
103 $staticDefaultVariant = $LangsWithStaticDefaultVariant[$langCode] ?? $langCode;
104 $this->defaultOptions["variant-$langCode"] = $staticDefaultVariant;
105 }
106
107 // NOTE: don't use SearchEngineConfig::getSearchableNamespaces here,
108 // since extensions may change the set of searchable namespaces depending
109 // on user groups/permissions.
110 $nsSearchDefault = $this->serviceOptions->get( MainConfigNames::NamespacesToBeSearchedDefault );
111 foreach ( $this->nsInfo->getValidNamespaces() as $n ) {
112 $this->defaultOptions['searchNs' . $n] = ( $nsSearchDefault[$n] ?? false ) ? 1 : 0;
113 }
114 $this->defaultOptions['skin'] = Skin::normalizeKey(
115 $this->serviceOptions->get( MainConfigNames::DefaultSkin ) );
116
117 $this->hookRunner->onUserGetDefaultOptions( $this->defaultOptions );
118
119 return $this->defaultOptions;
120 }
121
125 public function getDefaultOptions( ?UserIdentity $userIdentity = null ): array {
126 $defaultOptions = $this->getGenericDefaultOptions();
127
128 // If requested, process any conditional defaults
129 if ( $userIdentity ) {
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 }
140
141 return $defaultOptions;
142 }
143
147 public function getOption(
148 UserIdentity $user,
149 string $oname,
150 $defaultOverride = null,
151 bool $ignoreHidden = false,
152 int $queryFlags = IDBAccessObject::READ_NORMAL
153 ) {
154 $this->verifyUsable( $user, __METHOD__ );
155 return $this->getDefaultOption( $oname ) ?? $defaultOverride;
156 }
157
161 public function getOptions(
162 UserIdentity $user,
163 int $flags = 0,
164 int $queryFlags = IDBAccessObject::READ_NORMAL
165 ): array {
166 $this->verifyUsable( $user, __METHOD__ );
167 if ( $flags & self::EXCLUDE_DEFAULTS ) {
168 return [];
169 }
170 return $this->getDefaultOptions();
171 }
172
187 private function verifyUsable( UserIdentity $user, string $fname ) {
188 if ( defined( 'MEDIAWIKI_INSTALL' ) ) {
189 return;
190 }
191 Assert::precondition( !$user->isRegistered(), "$fname called on a registered user" );
192 }
193}
194
196class_alias( DefaultOptionsLookup::class, 'MediaWiki\\User\\DefaultOptionsLookup' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Base class for multi-variant language conversion.
Base class for language-specific code.
Definition Language.php:65
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...
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.
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(ServiceOptions $options, Language $contentLang, HookContainer $hookContainer, NamespaceInfo $nsInfo, ConditionalDefaultsLookup $conditionalUserOptionsDefaultsLookup)
Provides access to user options.
The base class for all skins.
Definition Skin.php:59
static normalizeKey(string $key)
Normalize a skin preference value to a form that can be loaded.
Definition Skin.php:222
Interface for database access objects.
Interface for objects representing user identity.
isRegistered()
This must be equivalent to getId() != 0 and is provided for code readability.