Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
20 / 22
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
CrossSearchStrategy
90.91% covered (success)
90.91%
20 / 22
71.43% covered (warning)
71.43%
5 / 7
17.22
0.00% covered (danger)
0.00%
0 / 1
 hostWikiOnlyStrategy
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 allWikisStrategy
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 isCrossProjectSearchSupported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isCrossLanguageSearchSupported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isExtraIndicesSearchSupported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 intersect
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2
3namespace CirrusSearch;
4
5/**
6 * Defines support strategies regarding cross wiki searches.
7 *
8 * Cross wiki search techniques include:
9 *  - extra indices searches (i.e. search media files on commons)
10 *  - cross project searches
11 *  - cross language searches (i.e. second try attempt)
12 */
13class CrossSearchStrategy {
14    /**
15     * @see InterwikiSearcher
16     * @var bool
17     */
18    private $crossProjectSearchSupported;
19
20    /**
21     * @see \CirrusSearch::searchTextSecondTry()
22     * @var bool
23     */
24    private $crossLanguageSearchSupported;
25
26    /**
27     * Controlled by $wgCirrusSearchExtraIndexes
28     * (Usually used to search media files on commons from any host wikis)
29     * @var bool
30     */
31    private $extraIndicesSearchSupported;
32
33    /**
34     * @var CrossSearchStrategy
35     */
36    private static $hostWikiOnly;
37
38    /**
39     * @var CrossSearchStrategy
40     */
41    private static $allWikisStrategy;
42
43    /**
44     * Only host wiki is supported
45     * Applies to features that are probably not available on any
46     * other target wiki cirrus may access.
47     * @return CrossSearchStrategy
48     */
49    public static function hostWikiOnlyStrategy() {
50        if ( self::$hostWikiOnly === null ) {
51            self::$hostWikiOnly = new CrossSearchStrategy( false, false, false );
52        }
53        return self::$hostWikiOnly;
54    }
55
56    /**
57     * Applies to features that must be available on any target wiki.
58     *
59     * @return CrossSearchStrategy
60     */
61    public static function allWikisStrategy() {
62        if ( self::$allWikisStrategy === null ) {
63            self::$allWikisStrategy = new CrossSearchStrategy( true, true, true );
64        }
65        return self::$allWikisStrategy;
66    }
67
68    /**
69     * @param bool $crossProjectSearchSupported
70     * @param bool $crossLanguageSearchSupported
71     * @param bool $extraIndicesSupported
72     */
73    public function __construct( $crossProjectSearchSupported, $crossLanguageSearchSupported, $extraIndicesSupported ) {
74        $this->crossProjectSearchSupported = $crossProjectSearchSupported;
75        $this->crossLanguageSearchSupported = $crossLanguageSearchSupported;
76        $this->extraIndicesSearchSupported = $extraIndicesSupported;
77    }
78
79    /**
80     * Is cross project search supported (aka interwiki search)
81     * @return bool
82     */
83    public function isCrossProjectSearchSupported() {
84        return $this->crossProjectSearchSupported;
85    }
86
87    /**
88     * Is cross language search supported (i.e. second try language search)
89     * @return bool
90     */
91    public function isCrossLanguageSearchSupported() {
92        return $this->crossLanguageSearchSupported;
93    }
94
95    /**
96     * (in WMF context it's most commonly used to search media files
97     * on commons from any wikis.)
98     *
99     * See wgCirrusSearchExtraIndexes
100     * @return bool
101     */
102    public function isExtraIndicesSearchSupported() {
103        return $this->extraIndicesSearchSupported;
104    }
105
106    /**
107     * Intersect this strategy with other.
108     * @param CrossSearchStrategy $other
109     * @return CrossSearchStrategy
110     */
111    public function intersect( CrossSearchStrategy $other ) {
112        if ( $other === self::hostWikiOnlyStrategy() || $this === self::hostWikiOnlyStrategy() ) {
113            return self::hostWikiOnlyStrategy();
114        }
115        $crossL = $other->crossLanguageSearchSupported && $this->crossLanguageSearchSupported;
116        $crossP = $other->crossProjectSearchSupported && $this->crossProjectSearchSupported;
117        $otherI = $other->extraIndicesSearchSupported && $this->extraIndicesSearchSupported;
118        if ( $crossL === $crossP && $crossP === $otherI ) {
119            if ( $crossL ) {
120                return self::allWikisStrategy();
121            } else {
122                return self::hostWikiOnlyStrategy();
123            }
124        }
125        return new CrossSearchStrategy( $crossP, $crossL, $otherI );
126    }
127}