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.
34 * Search Engine Mappings
36 * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives).
37 * Value is a specification for ObjectFactory.
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,
51 HookContainer
$hookContainer,
52 array $engineMappings,
53 UserOptionsLookup
$userOptionsLookup
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;
64 * Retrieve original config.
65 * @deprecated since 1.43, use ServiceOptions instead with DI.
68 public function getConfig() {
69 wfDeprecated( __METHOD__
, '1.43' );
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() {
80 foreach ( $this->language
->getNamespaces() as $ns => $name ) {
81 if ( $ns >= NS_MAIN
) {
86 $this->hookRunner
->onSearchableNamespaces( $arr );
91 * Extract default namespaces to search from the given user's
92 * settings, returning a list of index numbers.
94 * @param UserIdentity $user
97 public function userNamespaces( $user ) {
99 foreach ( $this->searchableNamespaces() as $ns => $name ) {
100 if ( $this->userOptionsLookup
->getOption( $user, 'searchNs' . $ns ) ) {
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
),
119 * Return the search engines we support. If only $wgSearchType
120 * is set, it'll be an array of just that one item.
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:
152 * "SearchMappings": {
153 * "foobarsearch": { "class": "MediaWiki\\Extension\\FoobarSearch\\FoobarSearch" }
160 public function getSearchMappings() {
161 return $this->engineMappings
;
165 * Get a list of namespace names useful for showing in tooltips
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 ) {
175 $formatted[$key] = wfMessage( 'blanknamespace' )->text();