MediaWiki REL1_37
DefaultOptionsLookup.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\User;
22
23use Language;
28use Skin;
29use Wikimedia\Assert\Assert;
30
36
40 public const CONSTRUCTOR_OPTIONS = [
41 'DefaultSkin',
42 'DefaultUserOptions',
43 'NamespacesToBeSearchedDefault'
44 ];
45
48
50 private $contentLang;
51
53 private $defaultOptions = null;
54
56 private $hookRunner;
57
63 public function __construct(
64 ServiceOptions $options,
66 HookContainer $hookContainer
67 ) {
68 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
69 $this->serviceOptions = $options;
70 $this->contentLang = $contentLang;
71 $this->hookRunner = new HookRunner( $hookContainer );
72 }
73
77 public function getDefaultOptions(): array {
78 if ( $this->defaultOptions !== null ) {
80 }
81
82 $this->defaultOptions = $this->serviceOptions->get( 'DefaultUserOptions' );
83
84 // Default language setting
85 $this->defaultOptions['language'] = $this->contentLang->getCode();
86 foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
87 if ( $langCode === $this->contentLang->getCode() ) {
88 $this->defaultOptions['variant'] = $langCode;
89 } else {
90 $this->defaultOptions["variant-$langCode"] = $langCode;
91 }
92 }
93
94 // NOTE: don't use SearchEngineConfig::getSearchableNamespaces here,
95 // since extensions may change the set of searchable namespaces depending
96 // on user groups/permissions.
97 foreach ( $this->serviceOptions->get( 'NamespacesToBeSearchedDefault' ) as $nsnum => $val ) {
98 $this->defaultOptions['searchNs' . $nsnum] = (bool)$val;
99 }
100 $this->defaultOptions['skin'] = Skin::normalizeKey( $this->serviceOptions->get( 'DefaultSkin' ) );
101
102 $this->hookRunner->onUserGetDefaultOptions( $this->defaultOptions );
103
105 }
106
110 public function getOption(
111 UserIdentity $user,
112 string $oname,
113 $defaultOverride = null,
114 bool $ignoreHidden = false,
115 int $queryFlags = self::READ_NORMAL
116 ) {
117 $this->verifyUsable( $user, __METHOD__ );
118 return $this->getDefaultOption( $oname ) ?? $defaultOverride;
119 }
120
124 public function getOptions(
125 UserIdentity $user,
126 int $flags = 0,
127 int $queryFlags = self::READ_NORMAL
128 ): array {
129 $this->verifyUsable( $user, __METHOD__ );
130 if ( $flags & self::EXCLUDE_DEFAULTS ) {
131 return [];
132 }
133 return $this->getDefaultOptions();
134 }
135
144 private function verifyUsable( UserIdentity $user, string $fname ) {
145 Assert::precondition( !$user->isRegistered(), "$fname called on a registered user " );
146 }
147}
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:88
Base class for multi-variant language conversion.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition Language.php:42
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.
__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:44
static normalizeKey( $key)
Normalize a skin preference value to a form that can be loaded.
Definition Skin.php:128
Interface for objects representing user identity.