Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.47% covered (warning)
89.47%
17 / 19
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExternalIndex
89.47% covered (warning)
89.47%
17 / 19
66.67% covered (warning)
66.67%
4 / 6
11.14
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getGroupAndIndexName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getIndexName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCrossClusterName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSearchIndex
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 getBoosts
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace CirrusSearch;
4
5/**
6 * Represents an external index referenced by the OtherIndex functionality.
7 * Typically sourced from $wgCirrusSearchExtraIndex.
8 */
9class ExternalIndex {
10    /**
11     * @var SearchConfig
12     */
13    private $config;
14
15    /**
16     * @var string replica group to write to, null if the index requested is hosted on the same
17     * cluster group declared in as the default group in this SearchConfig $config.
18     */
19    private $crossClusterName;
20
21    /**
22     * @var string Name of index on external clusters
23     */
24    private $indexName;
25
26    /**
27     * @var string Name of index on external clusters. Can include a group prefix
28     *  when required.
29     */
30    private $groupAndIndexName;
31
32    /**
33     * @param Searchconfig $config
34     * @param string $indexName Name of index on external clusters. Can include a group prefix
35     * (e.g. "cluster_group:index_name")
36     */
37    public function __construct( SearchConfig $config, $indexName ) {
38        $this->config = $config;
39        $this->groupAndIndexName = $indexName;
40        $groupAndIndex = explode( ':', $indexName, 2 );
41        if ( count( $groupAndIndex ) === 2 ) {
42            $currentGroup = $config->getClusterAssignment()->getCrossClusterName();
43            $this->crossClusterName = $currentGroup !== $groupAndIndex[0] ? $groupAndIndex[0] : null;
44            $this->indexName = $groupAndIndex[1];
45        } else {
46            $this->indexName = $indexName;
47        }
48    }
49
50    /**
51     * @return string Name of index on external clusters. Can include a group prefix
52     *  when required.
53     */
54    public function getGroupAndIndexName() {
55        return $this->groupAndIndexName;
56    }
57
58    /**
59     * @return string The name of the external index.
60     */
61    public function getIndexName() {
62        return $this->indexName;
63    }
64
65    /**
66     * @return string|null The group external index writes must be sent to, null to send to current group.
67     */
68    public function getCrossClusterName() {
69        return $this->crossClusterName;
70    }
71
72    /**
73     * @param string|null $sourceCrossClusterName Name of the cluster as configured in the cross-cluster
74     * search settings, null for simple&deprecated configs.
75     * @return string The name of the index to search. Includes
76     *   cross-cluster identifier if necessary.
77     */
78    public function getSearchIndex( $sourceCrossClusterName ) {
79        $currentGroup = $this->crossClusterName ?? $this->config->getClusterAssignment()->getCrossClusterName();
80
81        return $sourceCrossClusterName === $currentGroup || $currentGroup === null
82            ? $this->indexName
83            : "{$currentGroup}:{$this->indexName}";
84    }
85
86    /**
87     * @return array Two item array first containing a wiki name and second a map
88     *  from template name to weight for that template.
89     */
90    public function getBoosts() {
91        $boosts = $this->config->getElement( 'CirrusSearchExtraIndexBoostTemplates', $this->indexName );
92        if ( isset( $boosts['wiki'] ) ) {
93            return [ $boosts['wiki'], $boosts['boosts'] ?? [] ];
94        } else {
95            return [ '', [] ];
96        }
97    }
98}