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
;
13 * Configuration handling class for SearchEngine.
14 * Provides added service over plain configuration.
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
,
28 * Config object from which the settings will be derived.
32 private ServiceOptions
$options;
41 * Search Engine Mappings
43 * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives).
44 * Value is a specification for ObjectFactory.
48 private $engineMappings;
56 * @var UserOptionsLookup
58 private $userOptionsLookup;
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,
70 HookContainer
$hookContainer,
72 UserOptionsLookup
$userOptionsLookup
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;
83 * Retrieve original config.
84 * @deprecated since 1.43, use ServiceOptions instead with DI.
87 public function getConfig() {
88 wfDeprecated( __METHOD__
, '1.43' );
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() {
99 foreach ( $this->language
->getNamespaces() as $ns => $name ) {
100 if ( $ns >= NS_MAIN
) {
105 $this->hookRunner
->onSearchableNamespaces( $arr );
110 * Extract default namespaces to search from the given user's
111 * settings, returning a list of index numbers.
113 * @param UserIdentity $user
116 public function userNamespaces( $user ) {
118 foreach ( $this->searchableNamespaces() as $ns => $name ) {
119 if ( $this->userOptionsLookup
->getOption( $user, 'searchNs' . $ns ) ) {
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
),
138 * Return the search engines we support. If only $wgSearchType
139 * is set, it'll be an array of just that one item.
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:
171 * "SearchMappings": {
172 * "foobarsearch": { "class": "MediaWiki\\Extension\\FoobarSearch\\FoobarSearch" }
179 public function getSearchMappings() {
180 return $this->engineMappings
;
184 * Get a list of namespace names useful for showing in tooltips
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 ) {
194 $formatted[$key] = wfMessage( 'blanknamespace' )->text();