Merge "mediawiki.content.json: Remove file and author annotations"
[mediawiki.git] / includes / search / SearchEngineConfig.php
blob0554c7c2bf0156219fe5a8251881343da9b2a764
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;
33 /**
34 * Search Engine Mappings
36 * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives).
37 * Value is a specification for ObjectFactory.
39 * @var array
41 private $engineMappings;
43 private ServiceOptions $options;
44 private Language $language;
45 private HookRunner $hookRunner;
46 private UserOptionsLookup $userOptionsLookup;
48 public function __construct(
49 ServiceOptions $options,
50 Language $language,
51 HookContainer $hookContainer,
52 array $engineMappings,
53 UserOptionsLookup $userOptionsLookup
54 ) {
55 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
56 $this->options = $options;
57 $this->language = $language;
58 $this->engineMappings = $engineMappings;
59 $this->hookRunner = new HookRunner( $hookContainer );
60 $this->userOptionsLookup = $userOptionsLookup;
63 /**
64 * Retrieve original config.
65 * @deprecated since 1.43, use ServiceOptions instead with DI.
66 * @return Config
68 public function getConfig() {
69 wfDeprecated( __METHOD__, '1.43' );
70 return $this->config;
73 /**
74 * Make a list of searchable namespaces and their localized names.
75 * @return string[] Namespace ID => name
76 * @phan-return array<int,string>
78 public function searchableNamespaces() {
79 $arr = [];
80 foreach ( $this->language->getNamespaces() as $ns => $name ) {
81 if ( $ns >= NS_MAIN ) {
82 $arr[$ns] = $name;
86 $this->hookRunner->onSearchableNamespaces( $arr );
87 return $arr;
90 /**
91 * Extract default namespaces to search from the given user's
92 * settings, returning a list of index numbers.
94 * @param UserIdentity $user
95 * @return int[]
97 public function userNamespaces( $user ) {
98 $arr = [];
99 foreach ( $this->searchableNamespaces() as $ns => $name ) {
100 if ( $this->userOptionsLookup->getOption( $user, 'searchNs' . $ns ) ) {
101 $arr[] = $ns;
105 return $arr;
109 * An array of namespaces indexes to be searched by default
111 * @return int[] Namespace IDs
113 public function defaultNamespaces() {
114 return array_keys( $this->options->get( MainConfigNames::NamespacesToBeSearchedDefault ),
115 true );
119 * Return the search engines we support. If only $wgSearchType
120 * is set, it'll be an array of just that one item.
122 * @return array
124 public function getSearchTypes() {
125 $alternatives = $this->options->get( MainConfigNames::SearchTypeAlternatives ) ?: [];
126 array_unshift( $alternatives, $this->options->get( MainConfigNames::SearchType ) );
128 return $alternatives;
132 * Return the search engine configured in $wgSearchType, etc.
134 * @return string|null
136 public function getSearchType() {
137 return $this->options->get( MainConfigNames::SearchType );
141 * Returns the mappings between canonical search name and underlying PHP class
143 * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives).
144 * Value is a specification for ObjectFactory.
146 * For example to be able to use 'foobarsearch' in $wgSearchType and
147 * $wgSearchTypeAlternatives but the PHP class for 'foobarsearch'
148 * is 'MediaWiki\Extension\FoobarSearch\FoobarSearch' set:
150 * @par extension.json Example:
151 * @code
152 * "SearchMappings": {
153 * "foobarsearch": { "class": "MediaWiki\\Extension\\FoobarSearch\\FoobarSearch" }
155 * @endcode
157 * @since 1.35
158 * @return array
160 public function getSearchMappings() {
161 return $this->engineMappings;
165 * Get a list of namespace names useful for showing in tooltips
166 * and preferences.
168 * @param int[] $namespaces
169 * @return string[] List of names
171 public function namespacesAsText( $namespaces ) {
172 $formatted = array_map( [ $this->language, 'getFormattedNsText' ], $namespaces );
173 foreach ( $formatted as $key => $ns ) {
174 if ( !$ns ) {
175 $formatted[$key] = wfMessage( 'blanknamespace' )->text();
178 return $formatted;