| 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 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class SearchEngineConfig { |
| 19 |
|
| 20 |
|
| 21 |
public const CONSTRUCTOR_OPTIONS = [ |
| 22 |
MainConfigNames::NamespacesToBeSearchedDefault, |
| 23 |
MainConfigNames::SearchTypeAlternatives, |
| 24 |
MainConfigNames::SearchType, |
| 25 |
]; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private $config; |
| 32 |
private ServiceOptions $options; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private $language; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private $engineMappings; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private $hookRunner; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private $userOptionsLookup; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function __construct( |
| 68 |
ServiceOptions $options, |
| 69 |
Language $lang, |
| 70 |
HookContainer $hookContainer, |
| 71 |
array $mappings, |
| 72 |
UserOptionsLookup $userOptionsLookup |
| 73 |
) { |
| 74 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 75 |
$this->options = $options; |
| 76 |
$this->language = $lang; |
| 77 |
$this->engineMappings = $mappings; |
| 78 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 79 |
$this->userOptionsLookup = $userOptionsLookup; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function getConfig() { |
| 88 |
wfDeprecated( __METHOD__, '1.43' ); |
| 89 |
return $this->config; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function searchableNamespaces() { |
| 98 |
$arr = []; |
| 99 |
foreach ( $this->language->getNamespaces() as $ns => $name ) { |
| 100 |
if ( $ns >= NS_MAIN ) { |
| 101 |
$arr[$ns] = $name; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
$this->hookRunner->onSearchableNamespaces( $arr ); |
| 106 |
return $arr; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function userNamespaces( $user ) { |
| 117 |
$arr = []; |
| 118 |
foreach ( $this->searchableNamespaces() as $ns => $name ) { |
| 119 |
if ( $this->userOptionsLookup->getOption( $user, 'searchNs' . $ns ) ) { |
| 120 |
$arr[] = $ns; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return $arr; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function defaultNamespaces() { |
| 133 |
return array_keys( $this->options->get( MainConfigNames::NamespacesToBeSearchedDefault ), |
| 134 |
true ); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function getSearchTypes() { |
| 144 |
$alternatives = $this->options->get( MainConfigNames::SearchTypeAlternatives ) ?: []; |
| 145 |
array_unshift( $alternatives, $this->options->get( MainConfigNames::SearchType ) ); |
| 146 |
|
| 147 |
return $alternatives; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function getSearchType() { |
| 156 |
return $this->options->get( MainConfigNames::SearchType ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function getSearchMappings() { |
| 180 |
return $this->engineMappings; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function namespacesAsText( $namespaces ) { |
| 191 |
$formatted = array_map( [ $this->language, 'getFormattedNsText' ], $namespaces ); |
| 192 |
foreach ( $formatted as $key => $ns ) { |
| 193 |
if ( !$ns ) { |
| 194 |
$formatted[$key] = wfMessage( 'blanknamespace' )->text(); |
| 195 |
} |
| 196 |
} |
| 197 |
return $formatted; |
| 198 |
} |
| 199 |
} |