Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / search / SearchEngineConfig.php
blob89d6827f53664131bd2d57e477f5b7b96df7d381
1 <?php
3 use MediaWiki\Config\Config;
4 use MediaWiki\Config\ServiceOptions;
5 use MediaWiki\HookContainer\HookContainer;
6 use MediaWiki\HookContainer\HookRunner;
7 use MediaWiki\Language\Language;
8 use MediaWiki\MainConfigNames;
9 use MediaWiki\User\Options\UserOptionsLookup;
10 use MediaWiki\User\UserIdentity;
12 /**
13 * Configuration handling class for SearchEngine.
14 * Provides added service over plain configuration.
16 * @since 1.27
18 class SearchEngineConfig {
20 /** @internal For use by ServiceWiring.php ONLY */
21 public const CONSTRUCTOR_OPTIONS = [
22 MainConfigNames::NamespacesToBeSearchedDefault,
23 MainConfigNames::SearchTypeAlternatives,
24 MainConfigNames::SearchType,
27 /**
28 * Config object from which the settings will be derived.
29 * @var Config
31 private $config;
32 private ServiceOptions $options;
34 /**
35 * Current language
36 * @var Language
38 private $language;
40 /**
41 * Search Engine Mappings
43 * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives).
44 * Value is a specification for ObjectFactory.
46 * @var array
48 private $engineMappings;
50 /**
51 * @var HookRunner
53 private $hookRunner;
55 /**
56 * @var UserOptionsLookup
58 private $userOptionsLookup;
60 /**
61 * @param ServiceOptions $options
62 * @param Language $lang
63 * @param HookContainer $hookContainer
64 * @param array $mappings
65 * @param UserOptionsLookup $userOptionsLookup
67 public function __construct(
68 ServiceOptions $options,
69 Language $lang,
70 HookContainer $hookContainer,
71 array $mappings,
72 UserOptionsLookup $userOptionsLookup
73 ) {
74 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
75 $this->options = $options;
76 $this->language = $lang;
77 $this->engineMappings = $mappings;
78 $this->hookRunner = new HookRunner( $hookContainer );
79 $this->userOptionsLookup = $userOptionsLookup;
82 /**
83 * Retrieve original config.
84 * @deprecated since 1.43, use ServiceOptions instead with DI.
85 * @return Config
87 public function getConfig() {
88 wfDeprecated( __METHOD__, '1.43' );
89 return $this->config;
92 /**
93 * Make a list of searchable namespaces and their localized names.
94 * @return string[] Namespace ID => name
95 * @phan-return array<int,string>
97 public function searchableNamespaces() {
98 $arr = [];
99 foreach ( $this->language->getNamespaces() as $ns => $name ) {
100 if ( $ns >= NS_MAIN ) {
101 $arr[$ns] = $name;
105 $this->hookRunner->onSearchableNamespaces( $arr );
106 return $arr;
110 * Extract default namespaces to search from the given user's
111 * settings, returning a list of index numbers.
113 * @param UserIdentity $user
114 * @return int[]
116 public function userNamespaces( $user ) {
117 $arr = [];
118 foreach ( $this->searchableNamespaces() as $ns => $name ) {
119 if ( $this->userOptionsLookup->getOption( $user, 'searchNs' . $ns ) ) {
120 $arr[] = $ns;
124 return $arr;
128 * An array of namespaces indexes to be searched by default
130 * @return int[] Namespace IDs
132 public function defaultNamespaces() {
133 return array_keys( $this->options->get( MainConfigNames::NamespacesToBeSearchedDefault ),
134 true );
138 * Return the search engines we support. If only $wgSearchType
139 * is set, it'll be an array of just that one item.
141 * @return array
143 public function getSearchTypes() {
144 $alternatives = $this->options->get( MainConfigNames::SearchTypeAlternatives ) ?: [];
145 array_unshift( $alternatives, $this->options->get( MainConfigNames::SearchType ) );
147 return $alternatives;
151 * Return the search engine configured in $wgSearchType, etc.
153 * @return string|null
155 public function getSearchType() {
156 return $this->options->get( MainConfigNames::SearchType );
160 * Returns the mappings between canonical search name and underlying PHP class
162 * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives).
163 * Value is a specification for ObjectFactory.
165 * For example to be able to use 'foobarsearch' in $wgSearchType and
166 * $wgSearchTypeAlternatives but the PHP class for 'foobarsearch'
167 * is 'MediaWiki\Extension\FoobarSearch\FoobarSearch' set:
169 * @par extension.json Example:
170 * @code
171 * "SearchMappings": {
172 * "foobarsearch": { "class": "MediaWiki\\Extension\\FoobarSearch\\FoobarSearch" }
174 * @endcode
176 * @since 1.35
177 * @return array
179 public function getSearchMappings() {
180 return $this->engineMappings;
184 * Get a list of namespace names useful for showing in tooltips
185 * and preferences.
187 * @param int[] $namespaces
188 * @return string[] List of names
190 public function namespacesAsText( $namespaces ) {
191 $formatted = array_map( [ $this->language, 'getFormattedNsText' ], $namespaces );
192 foreach ( $formatted as $key => $ns ) {
193 if ( !$ns ) {
194 $formatted[$key] = wfMessage( 'blanknamespace' )->text();
197 return $formatted;