MediaWiki  master
DefaultOptionsLookup.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\User;
22 
23 use Language;
29 use NamespaceInfo;
30 use Skin;
31 use Wikimedia\Assert\Assert;
32 
38 
42  public const CONSTRUCTOR_OPTIONS = [
46  ];
47 
49  private $serviceOptions;
50 
52  private $contentLang;
53 
55  protected $nsInfo;
56 
58  private $defaultOptions = null;
59 
61  private $hookRunner;
62 
69  public function __construct(
70  ServiceOptions $options,
71  Language $contentLang,
72  HookContainer $hookContainer,
74  ) {
75  $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
76  $this->serviceOptions = $options;
77  $this->contentLang = $contentLang;
78  $this->hookRunner = new HookRunner( $hookContainer );
79  $this->nsInfo = $nsInfo;
80  }
81 
85  public function getDefaultOptions(): array {
86  if ( $this->defaultOptions !== null ) {
87  return $this->defaultOptions;
88  }
89 
90  $this->defaultOptions = $this->serviceOptions->get( MainConfigNames::DefaultUserOptions );
91 
92  // Default language setting
93  $contentLangCode = $this->contentLang->getCode();
94  $this->defaultOptions['language'] = $contentLangCode;
95  $this->defaultOptions['variant'] = $contentLangCode;
96  foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
97  $this->defaultOptions["variant-$langCode"] = $langCode;
98  }
99 
100  // NOTE: don't use SearchEngineConfig::getSearchableNamespaces here,
101  // since extensions may change the set of searchable namespaces depending
102  // on user groups/permissions.
103  $nsSearchDefault = $this->serviceOptions->get( MainConfigNames::NamespacesToBeSearchedDefault );
104  foreach ( $this->nsInfo->getValidNamespaces() as $n ) {
105  $this->defaultOptions['searchNs' . $n] = ( $nsSearchDefault[$n] ?? false ) ? 1 : 0;
106  }
107  $this->defaultOptions['skin'] = Skin::normalizeKey(
108  $this->serviceOptions->get( MainConfigNames::DefaultSkin ) );
109 
110  $this->hookRunner->onUserGetDefaultOptions( $this->defaultOptions );
111 
112  return $this->defaultOptions;
113  }
114 
118  public function getOption(
119  UserIdentity $user,
120  string $oname,
121  $defaultOverride = null,
122  bool $ignoreHidden = false,
123  int $queryFlags = self::READ_NORMAL
124  ) {
125  $this->verifyUsable( $user, __METHOD__ );
126  return $this->getDefaultOption( $oname ) ?? $defaultOverride;
127  }
128 
132  public function getOptions(
133  UserIdentity $user,
134  int $flags = 0,
135  int $queryFlags = self::READ_NORMAL
136  ): array {
137  $this->verifyUsable( $user, __METHOD__ );
138  if ( $flags & self::EXCLUDE_DEFAULTS ) {
139  return [];
140  }
141  return $this->getDefaultOptions();
142  }
143 
152  private function verifyUsable( UserIdentity $user, string $fname ) {
153  Assert::precondition( !$user->isRegistered(), "$fname called on a registered user " );
154  }
155 }
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition: WebStart.php:82
Base class for multi-variant language conversion.
static array $languagesWithVariants
languages supporting variants
Base class for language-specific code.
Definition: Language.php:53
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...
Definition: HookRunner.php:561
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()
A service class to control default user options.
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.The user to get the option for The option to check ...
getDefaultOptions()
Combine the language default options with any site-specific options and add the default language vari...
__construct(ServiceOptions $options, Language $contentLang, HookContainer $hookContainer, NamespaceInfo $nsInfo)
getOptions(UserIdentity $user, int $flags=0, int $queryFlags=self::READ_NORMAL)
Get all user's options.The user to get the option for Bitwise combination of: UserOptionsManager::EXC...
Provides access to user options.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
The main skin class which provides methods and properties for all other skins.
Definition: Skin.php:48
static normalizeKey( $key)
Normalize a skin preference value to a form that can be loaded.
Definition: Skin.php:192
Interface for objects representing user identity.