3d996baa6a23a2a474cec31f9aed58999d001e28
[lhc/web/wiklou.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 public function __construct( Config $config ) {
18 $this->config = $config;
19 }
20
21 /**
22 * Retrieve original config.
23 * @return Config
24 */
25 public function getConfig() {
26 return $this->config;
27 }
28
29 /**
30 * Make a list of searchable namespaces and their canonical names.
31 * @return array Namespace ID => name
32 */
33 public function searchableNamespaces() {
34 $arr = [];
35 foreach ( $this->config->get( 'ContLang' )->getNamespaces() as $ns => $name ) {
36 if ( $ns >= NS_MAIN ) {
37 $arr[$ns] = $name;
38 }
39 }
40
41 Hooks::run( 'SearchableNamespaces', [ &$arr ] );
42 return $arr;
43 }
44
45 /**
46 * Extract default namespaces to search from the given user's
47 * settings, returning a list of index numbers.
48 *
49 * @param user $user
50 * @return int[]
51 */
52 public function userNamespaces( $user ) {
53 $arr = [];
54 foreach ( $this->searchableNamespaces() as $ns => $name ) {
55 if ( $user->getOption( 'searchNs' . $ns ) ) {
56 $arr[] = $ns;
57 }
58 }
59
60 return $arr;
61 }
62
63 /**
64 * An array of namespaces indexes to be searched by default
65 *
66 * @return int[] Namespace IDs
67 */
68 public function defaultNamespaces() {
69 return array_keys( $this->config->get( 'NamespacesToBeSearchedDefault' ), true );
70 }
71
72 /**
73 * Return the search engines we support. If only $wgSearchType
74 * is set, it'll be an array of just that one item.
75 *
76 * @return array
77 */
78 public function getSearchTypes() {
79 $alternatives = $this->config->get( 'SearchTypeAlternatives' ) ?: [];
80 array_unshift( $alternatives, $this->config->get( 'SearchType' ) );
81
82 return $alternatives;
83 }
84
85 /**
86 * Return the search engine configured in $wgSearchType, etc.
87 *
88 * @return string|null
89 */
90 public function getSearchType() {
91 return $this->config->get( 'SearchType' );
92 }
93
94 /**
95 * Get a list of namespace names useful for showing in tooltips
96 * and preferences.
97 *
98 * @param int[] $namespaces
99 * @return string[] List of names
100 */
101 public function namespacesAsText( $namespaces ) {
102 $formatted = array_map( [ $this->config->get( 'ContLang' ), 'getFormattedNsText' ], $namespaces );
103 foreach ( $formatted as $key => $ns ) {
104 if ( empty( $ns ) ) {
105 $formatted[$key] = wfMessage( 'blanknamespace' )->text();
106 }
107 }
108 return $formatted;
109 }
110 }