Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
16 / 16 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
HashSearchConfig | |
100.00% |
16 / 16 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
getWikiId | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getHostWikiConfig | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
isLocalWiki | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace CirrusSearch; |
4 | |
5 | use CirrusSearch\Profile\SearchProfileServiceFactoryFactory; |
6 | use InvalidArgumentException; |
7 | use MediaWiki\Config\Config; |
8 | use MediaWiki\Config\GlobalVarConfig; |
9 | use MediaWiki\Config\HashConfig; |
10 | use MediaWiki\Config\MultiConfig; |
11 | |
12 | /** |
13 | * SearchConfig implemenation backed by a simple HashConfig |
14 | */ |
15 | class HashSearchConfig extends SearchConfig { |
16 | public const FLAG_INHERIT = 'inherit'; |
17 | |
18 | /** @var bool */ |
19 | private $localWiki = false; |
20 | |
21 | /** |
22 | * @param array $settings config vars |
23 | * @param string[] $flags customization flags: |
24 | * - inherit: config vars not part the settings provided are fetched from GlobalVarConfig |
25 | * @param Config|null $inherited (only useful when the inherit flag is set) |
26 | * @param SearchProfileServiceFactoryFactory|null $searchProfileServiceFactoryFactory |
27 | */ |
28 | public function __construct( |
29 | array $settings, |
30 | array $flags = [], |
31 | ?Config $inherited = null, |
32 | ?SearchProfileServiceFactoryFactory $searchProfileServiceFactoryFactory = null |
33 | ) { |
34 | parent::__construct( $searchProfileServiceFactoryFactory ); |
35 | $config = new HashConfig( $settings ); |
36 | $extra = array_diff( $flags, [ self::FLAG_INHERIT ] ); |
37 | if ( $extra ) { |
38 | throw new InvalidArgumentException( "Unknown config flags: " . implode( ',', $extra ) ); |
39 | } |
40 | |
41 | if ( in_array( self::FLAG_INHERIT, $flags ) ) { |
42 | $config = new MultiConfig( [ $config, $inherited ?? new GlobalVarConfig ] ); |
43 | $this->localWiki = !isset( $settings['_wikiID' ] ); |
44 | } |
45 | $this->setSource( $config ); |
46 | } |
47 | |
48 | /** |
49 | * Allow overriding Wiki ID |
50 | * @return mixed|string |
51 | */ |
52 | public function getWikiId() { |
53 | if ( $this->has( '_wikiID' ) ) { |
54 | return $this->get( '_wikiID' ); |
55 | } |
56 | return parent::getWikiId(); |
57 | } |
58 | |
59 | public function getHostWikiConfig(): SearchConfig { |
60 | if ( $this->localWiki ) { |
61 | return $this; |
62 | } |
63 | return parent::getHostWikiConfig(); |
64 | } |
65 | |
66 | public function isLocalWiki() { |
67 | return $this->localWiki; |
68 | } |
69 | } |