Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / search / SearchEngineConfig.php
blob90f85c3d28d09f3f9f575908738089557db70b80
1 <?php
3 /**
4 * Configuration handling class for SearchEngine.
5 * Provides added service over plain configuration.
7 * @since 1.27
8 */
9 class SearchEngineConfig {
11 /**
12 * Config object from which the settings will be derived.
13 * @var Config
15 private $config;
17 /**
18 * Current language
19 * @var Language
21 private $language;
23 public function __construct( Config $config, Language $lang ) {
24 $this->config = $config;
25 $this->language = $lang;
28 /**
29 * Retrieve original config.
30 * @return Config
32 public function getConfig() {
33 return $this->config;
36 /**
37 * Make a list of searchable namespaces and their canonical names.
38 * @return array Namespace ID => name
40 public function searchableNamespaces() {
41 $arr = [];
42 foreach ( $this->language->getNamespaces() as $ns => $name ) {
43 if ( $ns >= NS_MAIN ) {
44 $arr[$ns] = $name;
48 Hooks::run( 'SearchableNamespaces', [ &$arr ] );
49 return $arr;
52 /**
53 * Extract default namespaces to search from the given user's
54 * settings, returning a list of index numbers.
56 * @param user $user
57 * @return int[]
59 public function userNamespaces( $user ) {
60 $arr = [];
61 foreach ( $this->searchableNamespaces() as $ns => $name ) {
62 if ( $user->getOption( 'searchNs' . $ns ) ) {
63 $arr[] = $ns;
67 return $arr;
70 /**
71 * An array of namespaces indexes to be searched by default
73 * @return int[] Namespace IDs
75 public function defaultNamespaces() {
76 return array_keys( $this->config->get( 'NamespacesToBeSearchedDefault' ), true );
79 /**
80 * Return the search engines we support. If only $wgSearchType
81 * is set, it'll be an array of just that one item.
83 * @return array
85 public function getSearchTypes() {
86 $alternatives = $this->config->get( 'SearchTypeAlternatives' ) ?: [];
87 array_unshift( $alternatives, $this->config->get( 'SearchType' ) );
89 return $alternatives;
92 /**
93 * Return the search engine configured in $wgSearchType, etc.
95 * @return string|null
97 public function getSearchType() {
98 return $this->config->get( 'SearchType' );
102 * Get a list of namespace names useful for showing in tooltips
103 * and preferences.
105 * @param int[] $namespaces
106 * @return string[] List of names
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();
115 return $formatted;