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  // NOTE: don't use the content language code since the static default variant would
94  // NOT always be the same as the content language code.
95  $contentLangCode = $this->contentLang->getCode();
96  $LangsWithStaticDefaultVariant = LanguageConverter::$languagesWithStaticDefaultVariant;
97  $staticDefaultVariant = $LangsWithStaticDefaultVariant[$contentLangCode] ?? $contentLangCode;
98  $this->defaultOptions['language'] = $contentLangCode;
99  $this->defaultOptions['variant'] = $staticDefaultVariant;
100  foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
101  $staticDefaultVariant = $LangsWithStaticDefaultVariant[$langCode] ?? $langCode;
102  $this->defaultOptions["variant-$langCode"] = $staticDefaultVariant;
103  }
104 
105  // NOTE: don't use SearchEngineConfig::getSearchableNamespaces here,
106  // since extensions may change the set of searchable namespaces depending
107  // on user groups/permissions.
108  $nsSearchDefault = $this->serviceOptions->get( MainConfigNames::NamespacesToBeSearchedDefault );
109  foreach ( $this->nsInfo->getValidNamespaces() as $n ) {
110  $this->defaultOptions['searchNs' . $n] = ( $nsSearchDefault[$n] ?? false ) ? 1 : 0;
111  }
112  $this->defaultOptions['skin'] = Skin::normalizeKey(
113  $this->serviceOptions->get( MainConfigNames::DefaultSkin ) );
114 
115  $this->hookRunner->onUserGetDefaultOptions( $this->defaultOptions );
116 
117  return $this->defaultOptions;
118  }
119 
123  public function getOption(
124  UserIdentity $user,
125  string $oname,
126  $defaultOverride = null,
127  bool $ignoreHidden = false,
128  int $queryFlags = self::READ_NORMAL
129  ) {
130  $this->verifyUsable( $user, __METHOD__ );
131  return $this->getDefaultOption( $oname ) ?? $defaultOverride;
132  }
133 
137  public function getOptions(
138  UserIdentity $user,
139  int $flags = 0,
140  int $queryFlags = self::READ_NORMAL
141  ): array {
142  $this->verifyUsable( $user, __METHOD__ );
143  if ( $flags & self::EXCLUDE_DEFAULTS ) {
144  return [];
145  }
146  return $this->getDefaultOptions();
147  }
148 
157  private function verifyUsable( UserIdentity $user, string $fname ) {
158  Assert::precondition( !$user->isRegistered(), "$fname called on a registered user " );
159  }
160 }
if(!defined('MW_SETUP_CALLBACK'))
Definition: WebStart.php:88
Base class for multi-variant language conversion.
static array< string, string > $languagesWithStaticDefaultVariant
static default variant of languages supporting variants for use with DefaultOptionsLookup....
static string[] $languagesWithVariants
languages supporting variants
Base class for language-specific code.
Definition: Language.php:56
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:568
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:56
static normalizeKey( $key)
Normalize a skin preference value to a form that can be loaded.
Definition: Skin.php:209
Interface for objects representing user identity.