Debian Codegraph @

includes/search/SearchEngineConfig.php

raw symbols SCIP
1 <?php
2
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;
11
12 /**
13 * Configuration handling class for SearchEngine.
14 * Provides added service over plain configuration.
15 *
16 * @since 1.27
17 */
18 class SearchEngineConfig {
19
20 /** @internal For use by ServiceWiring.php ONLY */
21 public const CONSTRUCTOR_OPTIONS = [
22 MainConfigNames::NamespacesToBeSearchedDefault,
23 MainConfigNames::SearchTypeAlternatives,
24 MainConfigNames::SearchType,
25 ];
26
27 /**
28 * Config object from which the settings will be derived.
29 * @var Config
30 */
31 private $config;
32 private ServiceOptions $options;
33
34 /**
35 * Current language
36 * @var Language
37 */
38 private $language;
39
40 /**
41 * Search Engine Mappings
42 *
43 * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives).
44 * Value is a specification for ObjectFactory.
45 *
46 * @var array
47 */
48 private $engineMappings;
49
50 /**
51 * @var HookRunner
52 */
53 private $hookRunner;
54
55 /**
56 * @var UserOptionsLookup
57 */
58 private $userOptionsLookup;
59
60 /**
61 * @param ServiceOptions $options
62 * @param Language $lang
63 * @param HookContainer $hookContainer
64 * @param array $mappings
65 * @param UserOptionsLookup $userOptionsLookup
66 */
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;
80 }
81
82 /**
83 * Retrieve original config.
84 * @deprecated since 1.43, use ServiceOptions instead with DI.
85 * @return Config
86 */
87 public function getConfig() {
88 wfDeprecated( __METHOD__, '1.43' );
89 return $this->config;
90 }
91
92 /**
93 * Make a list of searchable namespaces and their localized names.
94 * @return string[] Namespace ID => name
95 * @phan-return array<int,string>
96 */
97 public function searchableNamespaces() {
98 $arr = [];
99 foreach ( $this->language->getNamespaces() as $ns => $name ) {
100 if ( $ns >= NS_MAIN ) {
101 $arr[$ns] = $name;
102 }
103 }
104
105 $this->hookRunner->onSearchableNamespaces( $arr );
106 return $arr;
107 }
108
109 /**
110 * Extract default namespaces to search from the given user's
111 * settings, returning a list of index numbers.
112 *
113 * @param UserIdentity $user
114 * @return int[]
115 */
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;
121 }
122 }
123
124 return $arr;
125 }
126
127 /**
128 * An array of namespaces indexes to be searched by default
129 *
130 * @return int[] Namespace IDs
131 */
132 public function defaultNamespaces() {
133 return array_keys( $this->options->get( MainConfigNames::NamespacesToBeSearchedDefault ),
134 true );
135 }
136
137 /**
138 * Return the search engines we support. If only $wgSearchType
139 * is set, it'll be an array of just that one item.
140 *
141 * @return array
142 */
143 public function getSearchTypes() {
144 $alternatives = $this->options->get( MainConfigNames::SearchTypeAlternatives ) ?: [];
145 array_unshift( $alternatives, $this->options->get( MainConfigNames::SearchType ) );
146
147 return $alternatives;
148 }
149
150 /**
151 * Return the search engine configured in $wgSearchType, etc.
152 *
153 * @return string|null
154 */
155 public function getSearchType() {
156 return $this->options->get( MainConfigNames::SearchType );
157 }
158
159 /**
160 * Returns the mappings between canonical search name and underlying PHP class
161 *
162 * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives).
163 * Value is a specification for ObjectFactory.
164 *
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:
168 *
169 * @par extension.json Example:
170 * @code
171 * "SearchMappings": {
172 * "foobarsearch": { "class": "MediaWiki\\Extension\\FoobarSearch\\FoobarSearch" }
173 * }
174 * @endcode
175 *
176 * @since 1.35
177 * @return array
178 */
179 public function getSearchMappings() {
180 return $this->engineMappings;
181 }
182
183 /**
184 * Get a list of namespace names useful for showing in tooltips
185 * and preferences.
186 *
187 * @param int[] $namespaces
188 * @return string[] List of names
189 */
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();
195 }
196 }
197 return $formatted;
198 }
199 }