Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 32 |
|
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 | namespace MediaWiki\Search; |
| 4 | |
| 5 | use MediaWiki\Config\Config; |
| 6 | use MediaWiki\Config\ServiceOptions; |
| 7 | use MediaWiki\HookContainer\HookContainer; |
| 8 | use MediaWiki\HookContainer\HookRunner; |
| 9 | use MediaWiki\Language\Language; |
| 10 | use MediaWiki\MainConfigNames; |
| 11 | use MediaWiki\User\Options\UserOptionsLookup; |
| 12 | use MediaWiki\User\UserIdentity; |
| 13 | |
| 14 | /** |
| 15 | * Configuration handling class for SearchEngine. |
| 16 | * Provides added service over plain configuration. |
| 17 | * |
| 18 | * @since 1.27 |
| 19 | */ |
| 20 | class SearchEngineConfig { |
| 21 | |
| 22 | /** @internal For use by ServiceWiring.php ONLY */ |
| 23 | public const CONSTRUCTOR_OPTIONS = [ |
| 24 | MainConfigNames::NamespacesToBeSearchedDefault, |
| 25 | MainConfigNames::SearchTypeAlternatives, |
| 26 | MainConfigNames::SearchType, |
| 27 | ]; |
| 28 | |
| 29 | /** |
| 30 | * Config object from which the settings will be derived. |
| 31 | * @var Config |
| 32 | */ |
| 33 | private $config; |
| 34 | |
| 35 | /** |
| 36 | * Search Engine Mappings |
| 37 | * |
| 38 | * Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives). |
| 39 | * Value is a specification for ObjectFactory. |
| 40 | * |
| 41 | * @var array |
| 42 | */ |
| 43 | private $engineMappings; |
| 44 | |
| 45 | private ServiceOptions $options; |
| 46 | private Language $language; |
| 47 | private HookRunner $hookRunner; |
| 48 | private UserOptionsLookup $userOptionsLookup; |
| 49 | |
| 50 | public function __construct( |
| 51 | ServiceOptions $options, |
| 52 | Language $language, |
| 53 | HookContainer $hookContainer, |
| 54 | array $engineMappings, |
| 55 | UserOptionsLookup $userOptionsLookup |
| 56 | ) { |
| 57 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 58 | $this->options = $options; |
| 59 | $this->language = $language; |
| 60 | $this->engineMappings = $engineMappings; |
| 61 | $this->hookRunner = new HookRunner( $hookContainer ); |
| 62 | $this->userOptionsLookup = $userOptionsLookup; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Retrieve original config. |
| 67 | * @return Config |
| 68 | * @deprecated since 1.43, use ServiceOptions instead with DI. |
| 69 | */ |
| 70 | public function getConfig() { |
| 71 | wfDeprecated( __METHOD__, '1.43' ); |
| 72 | return $this->config; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * List searchable namespaces and their localized names (with underscores, without considering |
| 77 | * language variants). |
| 78 | * |
| 79 | * NOTE: This is not suitable for UI text, as language variants of namespace names defined via |
| 80 | * system messages are ignored. Use {@see LanguageConverter::convertNamespace} instead. |
| 81 | * |
| 82 | * @return array<int,string> Numeric namespace id => localized name (without language variants) |
| 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 => $_ ) { |
| 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->options->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->options->get( MainConfigNames::SearchTypeAlternatives ) ?: []; |
| 132 | array_unshift( $alternatives, $this->options->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->options->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 | * @return array |
| 164 | * @since 1.35 |
| 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 | } |
| 187 | |
| 188 | /** @deprecated class alias since 1.46 */ |
| 189 | class_alias( SearchEngineConfig::class, 'SearchEngineConfig' ); |