MediaWiki  master
DefaultOptionsLookup.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\User;
22 
23 use Language;
28 use Skin;
29 use Wikimedia\Assert\Assert;
30 
36 
37  public const CONSTRUCTOR_OPTIONS = [
38  'DefaultSkin',
39  'DefaultUserOptions',
40  'NamespacesToBeSearchedDefault'
41  ];
42 
44  private $serviceOptions;
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 ) {
76  return $this->defaultOptions;
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 
101  return $this->defaultOptions;
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 }
MediaWiki\User\DefaultOptionsLookup\$serviceOptions
ServiceOptions $serviceOptions
Definition: DefaultOptionsLookup.php:44
MediaWiki\User\DefaultOptionsLookup\getOption
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 ...
Definition: DefaultOptionsLookup.php:115
MediaWiki\User\DefaultOptionsLookup\getDefaultOptions
getDefaultOptions()
Combine the language default options with any site-specific options and add the default language vari...
Definition: DefaultOptionsLookup.php:74
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
MediaWiki\User\DefaultOptionsLookup
A service class to control default user options.
Definition: DefaultOptionsLookup.php:35
MediaWiki\User\DefaultOptionsLookup\$contentLang
Language $contentLang
Definition: DefaultOptionsLookup.php:47
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:25
Skin\normalizeKey
static normalizeKey( $key)
Normalize a skin preference value to a form that can be loaded.
Definition: Skin.php:103
MediaWiki\User\UserIdentity\isRegistered
isRegistered()
MediaWiki\User\DefaultOptionsLookup\$defaultOptions
array null $defaultOptions
Cached default options.
Definition: DefaultOptionsLookup.php:50
MediaWiki\User\DefaultOptionsLookup\verifyUsable
verifyUsable(UserIdentity $user, string $fname)
Checks if the DefaultOptionsLookup is usable as an instance of UserOptionsLookup.
Definition: DefaultOptionsLookup.php:149
MediaWiki\User\DefaultOptionsLookup\$hookRunner
HookRunner $hookRunner
Definition: DefaultOptionsLookup.php:53
MediaWiki\User
Definition: DefaultOptionsLookup.php:21
MediaWiki\User\UserOptionsLookup
Provides access to user options.
Definition: UserOptionsLookup.php:29
LanguageConverter\$languagesWithVariants
static array $languagesWithVariants
languages supporting variants
Definition: LanguageConverter.php:43
MediaWiki\User\DefaultOptionsLookup\getDefaultOption
getDefaultOption(string $opt)
Get a given default option value.Name of option to retrieve string|null Default option value
Definition: DefaultOptionsLookup.php:107
MediaWiki\User\DefaultOptionsLookup\getOptions
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...
Definition: DefaultOptionsLookup.php:129
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:44
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:569
Skin
The main skin class which provides methods and properties for all other skins.
Definition: Skin.php:41
MediaWiki\User\DefaultOptionsLookup\__construct
__construct(ServiceOptions $options, Language $contentLang, HookContainer $hookContainer)
Definition: DefaultOptionsLookup.php:60
MediaWiki\User\DefaultOptionsLookup\CONSTRUCTOR_OPTIONS
const CONSTRUCTOR_OPTIONS
Definition: DefaultOptionsLookup.php:37
LanguageConverter
Base class for multi-variant language conversion.
Definition: LanguageConverter.php:35
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:41
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:62