Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchEngineConfig
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 9
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getConfig
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 searchableNamespaces
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 userNamespaces
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 defaultNamespaces
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getSearchTypes
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getSearchType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSearchMappings
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 namespacesAsText
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3use MediaWiki\Config\Config;
4use MediaWiki\HookContainer\HookContainer;
5use MediaWiki\HookContainer\HookRunner;
6use MediaWiki\MainConfigNames;
7use MediaWiki\User\Options\UserOptionsLookup;
8use MediaWiki\User\UserIdentity;
9
10/**
11 * Configuration handling class for SearchEngine.
12 * Provides added service over plain configuration.
13 *
14 * @since 1.27
15 */
16class SearchEngineConfig {
17
18    /**
19     * Config object from which the settings will be derived.
20     * @var Config
21     */
22    private $config;
23
24    /**
25     * Current language
26     * @var Language
27     */
28    private $language;
29
30    /**
31     * Search Engine Mappings
32     *
33     * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives).
34     * Value is a specification for ObjectFactory.
35     *
36     * @var array
37     */
38    private $engineMappings;
39
40    /**
41     * @var HookRunner
42     */
43    private $hookRunner;
44
45    /**
46     * @var UserOptionsLookup
47     */
48    private $userOptionsLookup;
49
50    /**
51     * @param Config $config
52     * @param Language $lang
53     * @param HookContainer $hookContainer
54     * @param array $mappings
55     * @param UserOptionsLookup $userOptionsLookup
56     */
57    public function __construct(
58        Config $config,
59        Language $lang,
60        HookContainer $hookContainer,
61        array $mappings,
62        UserOptionsLookup $userOptionsLookup
63    ) {
64        $this->config = $config;
65        $this->language = $lang;
66        $this->engineMappings = $mappings;
67        $this->hookRunner = new HookRunner( $hookContainer );
68        $this->userOptionsLookup = $userOptionsLookup;
69    }
70
71    /**
72     * Retrieve original config.
73     * @return Config
74     */
75    public function getConfig() {
76        return $this->config;
77    }
78
79    /**
80     * Make a list of searchable namespaces and their localized names.
81     * @return string[] Namespace ID => name
82     * @phan-return array<int,string>
83     */
84    public function searchableNamespaces() {
85        $arr = [];
86        foreach ( $this->language->getNamespaces() as $ns => $name ) {
87            if ( $ns >= NS_MAIN ) {
88                $arr[$ns] = $name;
89            }
90        }
91
92        $this->hookRunner->onSearchableNamespaces( $arr );
93        return $arr;
94    }
95
96    /**
97     * Extract default namespaces to search from the given user's
98     * settings, returning a list of index numbers.
99     *
100     * @param UserIdentity $user
101     * @return int[]
102     */
103    public function userNamespaces( $user ) {
104        $arr = [];
105        foreach ( $this->searchableNamespaces() as $ns => $name ) {
106            if ( $this->userOptionsLookup->getOption( $user, 'searchNs' . $ns ) ) {
107                $arr[] = $ns;
108            }
109        }
110
111        return $arr;
112    }
113
114    /**
115     * An array of namespaces indexes to be searched by default
116     *
117     * @return int[] Namespace IDs
118     */
119    public function defaultNamespaces() {
120        return array_keys( $this->config->get( MainConfigNames::NamespacesToBeSearchedDefault ),
121            true );
122    }
123
124    /**
125     * Return the search engines we support. If only $wgSearchType
126     * is set, it'll be an array of just that one item.
127     *
128     * @return array
129     */
130    public function getSearchTypes() {
131        $alternatives = $this->config->get( MainConfigNames::SearchTypeAlternatives ) ?: [];
132        array_unshift( $alternatives, $this->config->get( MainConfigNames::SearchType ) );
133
134        return $alternatives;
135    }
136
137    /**
138     * Return the search engine configured in $wgSearchType, etc.
139     *
140     * @return string|null
141     */
142    public function getSearchType() {
143        return $this->config->get( MainConfigNames::SearchType );
144    }
145
146    /**
147     * Returns the mappings between canonical search name and underlying PHP class
148     *
149     * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives).
150     * Value is a specification for ObjectFactory.
151     *
152     * For example to be able to use 'foobarsearch' in $wgSearchType and
153     * $wgSearchTypeAlternatives but the PHP class for 'foobarsearch'
154     * is 'MediaWiki\Extension\FoobarSearch\FoobarSearch' set:
155     *
156     * @par extension.json Example:
157     * @code
158     * "SearchMappings": {
159     *     "foobarsearch": { "class": "MediaWiki\\Extension\\FoobarSearch\\FoobarSearch" }
160     * }
161     * @endcode
162     *
163     * @since 1.35
164     * @return array
165     */
166    public function getSearchMappings() {
167        return $this->engineMappings;
168    }
169
170    /**
171     * Get a list of namespace names useful for showing in tooltips
172     * and preferences.
173     *
174     * @param int[] $namespaces
175     * @return string[] List of names
176     */
177    public function namespacesAsText( $namespaces ) {
178        $formatted = array_map( [ $this->language, 'getFormattedNsText' ], $namespaces );
179        foreach ( $formatted as $key => $ns ) {
180            if ( !$ns ) {
181                $formatted[$key] = wfMessage( 'blanknamespace' )->text();
182            }
183        }
184        return $formatted;
185    }
186}