Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
SearchEngineConfig | |
0.00% |
0 / 31 |
|
0.00% |
0 / 9 |
272 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getConfig | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
searchableNamespaces | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
userNamespaces | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
defaultNamespaces | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getSearchTypes | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getSearchType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSearchMappings | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
namespacesAsText | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 |
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 | |
33 | /** |
34 | * Search Engine Mappings |
35 | * |
36 | * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives). |
37 | * Value is a specification for ObjectFactory. |
38 | * |
39 | * @var array |
40 | */ |
41 | private $engineMappings; |
42 | |
43 | private ServiceOptions $options; |
44 | private Language $language; |
45 | private HookRunner $hookRunner; |
46 | private UserOptionsLookup $userOptionsLookup; |
47 | |
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; |
61 | } |
62 | |
63 | /** |
64 | * Retrieve original config. |
65 | * @deprecated since 1.43, use ServiceOptions instead with DI. |
66 | * @return Config |
67 | */ |
68 | public function getConfig() { |
69 | wfDeprecated( __METHOD__, '1.43' ); |
70 | return $this->config; |
71 | } |
72 | |
73 | /** |
74 | * Make a list of searchable namespaces and their localized names. |
75 | * @return string[] Namespace ID => name |
76 | * @phan-return array<int,string> |
77 | */ |
78 | public function searchableNamespaces() { |
79 | $arr = []; |
80 | foreach ( $this->language->getNamespaces() as $ns => $name ) { |
81 | if ( $ns >= NS_MAIN ) { |
82 | $arr[$ns] = $name; |
83 | } |
84 | } |
85 | |
86 | $this->hookRunner->onSearchableNamespaces( $arr ); |
87 | return $arr; |
88 | } |
89 | |
90 | /** |
91 | * Extract default namespaces to search from the given user's |
92 | * settings, returning a list of index numbers. |
93 | * |
94 | * @param UserIdentity $user |
95 | * @return int[] |
96 | */ |
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; |
102 | } |
103 | } |
104 | |
105 | return $arr; |
106 | } |
107 | |
108 | /** |
109 | * An array of namespaces indexes to be searched by default |
110 | * |
111 | * @return int[] Namespace IDs |
112 | */ |
113 | public function defaultNamespaces() { |
114 | return array_keys( $this->options->get( MainConfigNames::NamespacesToBeSearchedDefault ), |
115 | true ); |
116 | } |
117 | |
118 | /** |
119 | * Return the search engines we support. If only $wgSearchType |
120 | * is set, it'll be an array of just that one item. |
121 | * |
122 | * @return array |
123 | */ |
124 | public function getSearchTypes() { |
125 | $alternatives = $this->options->get( MainConfigNames::SearchTypeAlternatives ) ?: []; |
126 | array_unshift( $alternatives, $this->options->get( MainConfigNames::SearchType ) ); |
127 | |
128 | return $alternatives; |
129 | } |
130 | |
131 | /** |
132 | * Return the search engine configured in $wgSearchType, etc. |
133 | * |
134 | * @return string|null |
135 | */ |
136 | public function getSearchType() { |
137 | return $this->options->get( MainConfigNames::SearchType ); |
138 | } |
139 | |
140 | /** |
141 | * Returns the mappings between canonical search name and underlying PHP class |
142 | * |
143 | * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives). |
144 | * Value is a specification for ObjectFactory. |
145 | * |
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: |
149 | * |
150 | * @par extension.json Example: |
151 | * @code |
152 | * "SearchMappings": { |
153 | * "foobarsearch": { "class": "MediaWiki\\Extension\\FoobarSearch\\FoobarSearch" } |
154 | * } |
155 | * @endcode |
156 | * |
157 | * @since 1.35 |
158 | * @return array |
159 | */ |
160 | public function getSearchMappings() { |
161 | return $this->engineMappings; |
162 | } |
163 | |
164 | /** |
165 | * Get a list of namespace names useful for showing in tooltips |
166 | * and preferences. |
167 | * |
168 | * @param int[] $namespaces |
169 | * @return string[] List of names |
170 | */ |
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(); |
176 | } |
177 | } |
178 | return $formatted; |
179 | } |
180 | } |