MediaWiki REL1_35
DefaultOptionsLookup.php
Go to the documentation of this file.
1<?php
22
23use Language;
28use Skin;
29use Wikimedia\Assert\Assert;
30
36
37 public const CONSTRUCTOR_OPTIONS = [
38 'DefaultSkin',
39 'DefaultUserOptions',
40 'NamespacesToBeSearchedDefault'
41 ];
42
45
47 private $contentLang;
48
50 private $defaultOptions = null;
51
53 private $hookRunner;
54
60 public function __construct(
61 ServiceOptions $options,
63 HookContainer $hookContainer
64 ) {
65 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
66 $this->serviceOptions = $options;
67 $this->contentLang = $contentLang;
68 $this->hookRunner = new HookRunner( $hookContainer );
69 }
70
74 public function getDefaultOptions(): array {
75 if ( $this->defaultOptions !== null ) {
77 }
78
79 $this->defaultOptions = $this->serviceOptions->get( 'DefaultUserOptions' );
80
81 // Default language setting
82 $this->defaultOptions['language'] = $this->contentLang->getCode();
83 foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
84 if ( $langCode === $this->contentLang->getCode() ) {
85 $this->defaultOptions['variant'] = $langCode;
86 } else {
87 $this->defaultOptions["variant-$langCode"] = $langCode;
88 }
89 }
90
91 // NOTE: don't use SearchEngineConfig::getSearchableNamespaces here,
92 // since extensions may change the set of searchable namespaces depending
93 // on user groups/permissions.
94 foreach ( $this->serviceOptions->get( 'NamespacesToBeSearchedDefault' ) as $nsnum => $val ) {
95 $this->defaultOptions['searchNs' . $nsnum] = (bool)$val;
96 }
97 $this->defaultOptions['skin'] = Skin::normalizeKey( $this->serviceOptions->get( 'DefaultSkin' ) );
98
99 $this->hookRunner->onUserGetDefaultOptions( $this->defaultOptions );
100
102 }
103
107 public function getDefaultOption( string $opt ) {
108 $defOpts = $this->getDefaultOptions();
109 return $defOpts[$opt] ?? null;
110 }
111
115 public function getOption(
116 UserIdentity $user,
117 string $oname,
118 $defaultOverride = null,
119 bool $ignoreHidden = false,
120 int $queryFlags = self::READ_NORMAL
121 ) {
122 $this->verifyUsable( $user, __METHOD__ );
123 return $this->getDefaultOption( $oname ) ?? $defaultOverride;
124 }
125
129 public function getOptions(
130 UserIdentity $user,
131 int $flags = 0,
132 int $queryFlags = self::READ_NORMAL
133 ): array {
134 $this->verifyUsable( $user, __METHOD__ );
135 if ( $flags & self::EXCLUDE_DEFAULTS ) {
136 return [];
137 }
138 return $this->getDefaultOptions();
139 }
140
149 private function verifyUsable( UserIdentity $user, string $fname ) {
150 Assert::precondition( !$user->isRegistered(), "$fname called on a registered user " );
151 }
152}
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:85
Base class for multi-variant language conversion.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition Language.php:41
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 service class to control default user options.
getDefaultOption(string $opt)
Get a given default option value.string|null Default option value
__construct(ServiceOptions $options, Language $contentLang, HookContainer $hookContainer)
verifyUsable(UserIdentity $user, string $fname)
Checks if the DefaultOptionsLookup is usable as an instance of UserOptionsLookup.
getOption(UserIdentity $user, string $oname, $defaultOverride=null, bool $ignoreHidden=false, int $queryFlags=self::READ_NORMAL)
Get the user's current setting for a given option.mixed|null User's current value for the option getB...
array null $defaultOptions
Cached default options.
getDefaultOptions()
Combine the language default options with any site-specific options and add the default language vari...
getOptions(UserIdentity $user, int $flags=0, int $queryFlags=self::READ_NORMAL)
Get all user's options.array
Provides access to user options.
The main skin class which provides methods and properties for all other skins.
Definition Skin.php:41
static normalizeKey( $key)
Normalize a skin preference value to a form that can be loaded.
Definition Skin.php:103
Interface for objects representing user identity.