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 
40  public const CONSTRUCTOR_OPTIONS = [
41  'DefaultSkin',
42  'DefaultUserOptions',
43  'NamespacesToBeSearchedDefault'
44  ];
45 
47  private $serviceOptions;
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 ) {
79  return $this->defaultOptions;
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 
104  return $this->defaultOptions;
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 }
MediaWiki\User\DefaultOptionsLookup\$serviceOptions
ServiceOptions $serviceOptions
Definition: DefaultOptionsLookup.php:47
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:110
MediaWiki\User\DefaultOptionsLookup\getDefaultOptions
getDefaultOptions()
Combine the language default options with any site-specific options and add the default language vari...
Definition: DefaultOptionsLookup.php:77
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\User\DefaultOptionsLookup
A service class to control default user options.
Definition: DefaultOptionsLookup.php:35
MediaWiki\User\DefaultOptionsLookup\$contentLang
Language $contentLang
Definition: DefaultOptionsLookup.php:50
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:27
Skin\normalizeKey
static normalizeKey( $key)
Normalize a skin preference value to a form that can be loaded.
Definition: Skin.php:99
MediaWiki\User\UserIdentity\isRegistered
isRegistered()
MediaWiki\User\DefaultOptionsLookup\$defaultOptions
array null $defaultOptions
Cached default options.
Definition: DefaultOptionsLookup.php:53
MediaWiki\User\UserOptionsLookup\getDefaultOption
getDefaultOption(string $opt)
Get a given default option value.
Definition: UserOptionsLookup.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:144
MediaWiki\User\DefaultOptionsLookup\$hookRunner
HookRunner $hookRunner
Definition: DefaultOptionsLookup.php:56
MediaWiki\User
Definition: ActorCache.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:44
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:124
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:556
Skin
The main skin class which provides methods and properties for all other skins.
Definition: Skin.php:44
MediaWiki\User\DefaultOptionsLookup\__construct
__construct(ServiceOptions $options, Language $contentLang, HookContainer $hookContainer)
Definition: DefaultOptionsLookup.php:63
MediaWiki\User\DefaultOptionsLookup\CONSTRUCTOR_OPTIONS
const CONSTRUCTOR_OPTIONS
Definition: DefaultOptionsLookup.php:40
LanguageConverter
Base class for multi-variant language conversion.
Definition: LanguageConverter.php:36
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:71