]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/search/SearchEngineConfig.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.git] / includes / search / SearchEngineConfig.php
1 <?php
2
3 /**
4  * Configuration handling class for SearchEngine.
5  * Provides added service over plain configuration.
6  *
7  * @since 1.27
8  */
9 class SearchEngineConfig {
10
11         /**
12          * Config object from which the settings will be derived.
13          * @var Config
14          */
15         private $config;
16
17         /**
18          * Current language
19          * @var Language
20          */
21         private $language;
22
23         public function __construct( Config $config, Language $lang ) {
24                 $this->config = $config;
25                 $this->language = $lang;
26         }
27
28         /**
29          * Retrieve original config.
30          * @return Config
31          */
32         public function getConfig() {
33                 return $this->config;
34         }
35
36         /**
37          * Make a list of searchable namespaces and their canonical names.
38          * @return array Namespace ID => name
39          */
40         public function searchableNamespaces() {
41                 $arr = [];
42                 foreach ( $this->language->getNamespaces() as $ns => $name ) {
43                         if ( $ns >= NS_MAIN ) {
44                                 $arr[$ns] = $name;
45                         }
46                 }
47
48                 Hooks::run( 'SearchableNamespaces', [ &$arr ] );
49                 return $arr;
50         }
51
52         /**
53          * Extract default namespaces to search from the given user's
54          * settings, returning a list of index numbers.
55          *
56          * @param user $user
57          * @return int[]
58          */
59         public function userNamespaces( $user ) {
60                 $arr = [];
61                 foreach ( $this->searchableNamespaces() as $ns => $name ) {
62                         if ( $user->getOption( 'searchNs' . $ns ) ) {
63                                 $arr[] = $ns;
64                         }
65                 }
66
67                 return $arr;
68         }
69
70         /**
71          * An array of namespaces indexes to be searched by default
72          *
73          * @return int[] Namespace IDs
74          */
75         public function defaultNamespaces() {
76                 return array_keys( $this->config->get( 'NamespacesToBeSearchedDefault' ), true );
77         }
78
79         /**
80          * Return the search engines we support. If only $wgSearchType
81          * is set, it'll be an array of just that one item.
82          *
83          * @return array
84          */
85         public function getSearchTypes() {
86                 $alternatives = $this->config->get( 'SearchTypeAlternatives' ) ?: [];
87                 array_unshift( $alternatives, $this->config->get( 'SearchType' ) );
88
89                 return $alternatives;
90         }
91
92         /**
93          * Return the search engine configured in $wgSearchType, etc.
94          *
95          * @return string|null
96          */
97         public function getSearchType() {
98                 return $this->config->get( 'SearchType' );
99         }
100
101         /**
102          * Get a list of namespace names useful for showing in tooltips
103          * and preferences.
104          *
105          * @param int[] $namespaces
106          * @return string[] List of names
107          */
108         public function namespacesAsText( $namespaces ) {
109                 $formatted = array_map( [ $this->language, 'getFormattedNsText' ], $namespaces );
110                 foreach ( $formatted as $key => $ns ) {
111                         if ( empty( $ns ) ) {
112                                 $formatted[$key] = wfMessage( 'blanknamespace' )->text();
113                         }
114                 }
115                 return $formatted;
116         }
117 }