Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.65% |
22 / 23 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
FTQueryClassifiersRepository | |
95.65% |
22 / 23 |
|
83.33% |
5 / 6 |
10 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
registerClassifier | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
registerClassifierAsCallable | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
1.00 | |||
getConfig | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getClassifier | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getKnownClassifiers | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Parser; |
4 | |
5 | use CirrusSearch\CirrusSearchHookRunner; |
6 | use CirrusSearch\Parser\AST\ParsedQuery; |
7 | use CirrusSearch\SearchConfig; |
8 | use Wikimedia\Assert\Assert; |
9 | |
10 | /** |
11 | * Repository of query classifiers |
12 | */ |
13 | class FTQueryClassifiersRepository implements ParsedQueryClassifiersRepository { |
14 | /** |
15 | * @var ParsedQueryClassifier[] |
16 | */ |
17 | private $classifiers = []; |
18 | |
19 | /** |
20 | * @var SearchConfig |
21 | */ |
22 | private $config; |
23 | |
24 | /** |
25 | * @var bool |
26 | */ |
27 | private $frozen = false; |
28 | |
29 | /** |
30 | * @param SearchConfig $config |
31 | * @param CirrusSearchHookRunner $cirrusSearchHookRunner |
32 | * @throws ParsedQueryClassifierException |
33 | */ |
34 | public function __construct( SearchConfig $config, CirrusSearchHookRunner $cirrusSearchHookRunner ) { |
35 | $this->config = $config; |
36 | $this->registerClassifier( new BasicQueryClassifier() ); |
37 | $cirrusSearchHookRunner->onCirrusSearchRegisterFullTextQueryClassifiers( $this ); |
38 | $this->frozen = true; |
39 | } |
40 | |
41 | /** |
42 | * @param ParsedQueryClassifier $classifier |
43 | * @throws ParsedQueryClassifierException |
44 | */ |
45 | public function registerClassifier( ParsedQueryClassifier $classifier ) { |
46 | if ( $this->frozen ) { |
47 | throw new ParsedQueryClassifierException( 'Repository is frozen' ); |
48 | } |
49 | foreach ( $classifier->classes() as $class ) { |
50 | if ( array_key_exists( $class, $this->classifiers ) ) { |
51 | throw new ParsedQueryClassifierException( "Classifier with $class already registered" ); |
52 | } |
53 | $this->classifiers[$class] = $classifier; |
54 | } |
55 | } |
56 | |
57 | /** |
58 | * @param string[] $classes list of $classes that this classifier can produce |
59 | * @param callable $callable called as ParsedQueryClassifier::classify( ParsedQuery $query ) |
60 | * @throws ParsedQueryClassifierException |
61 | * @see ParsedQueryClassifier::classify() |
62 | */ |
63 | public function registerClassifierAsCallable( array $classes, callable $callable ) { |
64 | Assert::parameter( $classes !== [], '$classes', 'A classifier must support at least one class' ); |
65 | $factory = new class( $classes, $callable ) implements ParsedQueryClassifier { |
66 | /** |
67 | * @var string[] |
68 | */ |
69 | private $classes; |
70 | |
71 | /** |
72 | * @var callable |
73 | */ |
74 | private $callable; |
75 | |
76 | /** |
77 | * @param string[] $classes |
78 | * @param callable $callable |
79 | */ |
80 | public function __construct( $classes, callable $callable ) { |
81 | $this->classes = $classes; |
82 | $this->callable = $callable; |
83 | } |
84 | |
85 | /** |
86 | * @param ParsedQuery $query |
87 | * @return string[] |
88 | */ |
89 | public function classify( ParsedQuery $query ) { |
90 | return ( $this->callable )( $query ); |
91 | } |
92 | |
93 | /** |
94 | * @return string[] |
95 | */ |
96 | public function classes() { |
97 | return $this->classes; |
98 | } |
99 | }; |
100 | $this->registerClassifier( $factory ); |
101 | } |
102 | |
103 | /** |
104 | * The host wiki SearchConfig |
105 | * @return SearchConfig |
106 | */ |
107 | public function getConfig() { |
108 | return $this->config; |
109 | } |
110 | |
111 | /** |
112 | * @param string $name |
113 | * @return ParsedQueryClassifier |
114 | * @throws ParsedQueryClassifierException |
115 | */ |
116 | public function getClassifier( $name ) { |
117 | if ( array_key_exists( $name, $this->classifiers ) ) { |
118 | return $this->classifiers[$name]; |
119 | } |
120 | throw new ParsedQueryClassifierException( "Classifier $name not found" ); |
121 | } |
122 | |
123 | /** |
124 | * List known classifiers |
125 | * @return string[] |
126 | */ |
127 | public function getKnownClassifiers() { |
128 | return array_keys( $this->classifiers ); |
129 | } |
130 | } |