MediaWiki  master
DefaultOptionsLookup.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\User;
22 
23 use Language;
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 
66  private bool $isDatabaselessTest;
67 
75  public function __construct(
76  ServiceOptions $options,
77  Language $contentLang,
78  HookContainer $hookContainer,
80  bool $isDatabaselessTest
81  ) {
82  $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
83  $this->serviceOptions = $options;
84  $this->contentLang = $contentLang;
85  $this->hookRunner = new HookRunner( $hookContainer );
86  $this->nsInfo = $nsInfo;
87  $this->isDatabaselessTest = $isDatabaselessTest;
88  }
89 
93  public function getDefaultOptions(): array {
94  if ( $this->defaultOptions !== null ) {
95  return $this->defaultOptions;
96  }
97 
98  $this->defaultOptions = $this->serviceOptions->get( MainConfigNames::DefaultUserOptions );
99 
100  // Default language setting
101  // NOTE: don't use the content language code since the static default variant would
102  // NOT always be the same as the content language code.
103  $contentLangCode = $this->contentLang->getCode();
104  $LangsWithStaticDefaultVariant = LanguageConverter::$languagesWithStaticDefaultVariant;
105  $staticDefaultVariant = $LangsWithStaticDefaultVariant[$contentLangCode] ?? $contentLangCode;
106  $this->defaultOptions['language'] = $contentLangCode;
107  $this->defaultOptions['variant'] = $staticDefaultVariant;
108  foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
109  $staticDefaultVariant = $LangsWithStaticDefaultVariant[$langCode] ?? $langCode;
110  $this->defaultOptions["variant-$langCode"] = $staticDefaultVariant;
111  }
112 
113  // NOTE: don't use SearchEngineConfig::getSearchableNamespaces here,
114  // since extensions may change the set of searchable namespaces depending
115  // on user groups/permissions.
116  $nsSearchDefault = $this->serviceOptions->get( MainConfigNames::NamespacesToBeSearchedDefault );
117  foreach ( $this->nsInfo->getValidNamespaces() as $n ) {
118  $this->defaultOptions['searchNs' . $n] = ( $nsSearchDefault[$n] ?? false ) ? 1 : 0;
119  }
120  $this->defaultOptions['skin'] = Skin::normalizeKey(
121  $this->serviceOptions->get( MainConfigNames::DefaultSkin ) );
122 
123  $this->hookRunner->onUserGetDefaultOptions( $this->defaultOptions );
124 
125  return $this->defaultOptions;
126  }
127 
131  public function getOption(
132  UserIdentity $user,
133  string $oname,
134  $defaultOverride = null,
135  bool $ignoreHidden = false,
136  int $queryFlags = self::READ_NORMAL
137  ) {
138  $this->verifyUsable( $user, __METHOD__ );
139  return $this->getDefaultOption( $oname ) ?? $defaultOverride;
140  }
141 
145  public function getOptions(
146  UserIdentity $user,
147  int $flags = 0,
148  int $queryFlags = self::READ_NORMAL
149  ): array {
150  $this->verifyUsable( $user, __METHOD__ );
151  if ( $flags & self::EXCLUDE_DEFAULTS ) {
152  return [];
153  }
154  return $this->getDefaultOptions();
155  }
156 
169  private function verifyUsable( UserIdentity $user, string $fname ) {
170  Assert::precondition(
171  $this->isDatabaselessTest || !$user->isRegistered(),
172  "$fname called on a registered user "
173  );
174  }
175 }
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:61
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()
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.
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 ...
__construct(ServiceOptions $options, Language $contentLang, HookContainer $hookContainer, NamespaceInfo $nsInfo, bool $isDatabaselessTest)
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.The user to get the option for Bitwise combination of: UserOptionsManager::EXC...
Provides access to user options.
The base class for all skins.
Definition: Skin.php:60
static normalizeKey(string $key)
Normalize a skin preference value to a form that can be loaded.
Definition: Skin.php:213
Interface for objects representing user identity.
isRegistered()
This must be equivalent to getId() != 0 and is provided for code readability.
Utility class for bot passwords.
Definition: ActorCache.php:21