Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.77% |
21 / 26 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| BasicSearchQueryRoute | |
80.77% |
21 / 26 |
|
75.00% |
3 / 4 |
11.86 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| score | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
8 | |||
| getSearchEngineEntryPoint | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getProfileContext | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Dispatch; |
| 4 | |
| 5 | use Wikimedia\Assert\Assert; |
| 6 | |
| 7 | /** |
| 8 | * Basic SearchQuery routing functionality which produces a constant |
| 9 | * score when successful, 0.0 otherwise. |
| 10 | * Inspect the requested namespaces and the classes of the query. |
| 11 | */ |
| 12 | class BasicSearchQueryRoute implements SearchQueryRoute { |
| 13 | /** @var string */ |
| 14 | private $searchEngineEntryPoint; |
| 15 | |
| 16 | /** @var int[] */ |
| 17 | private $namespaces; |
| 18 | |
| 19 | /** @var string[] */ |
| 20 | private $acceptableQueryClasses; |
| 21 | |
| 22 | /** @var string */ |
| 23 | private $profileContext; |
| 24 | |
| 25 | /** @var float */ |
| 26 | private $score; |
| 27 | |
| 28 | /** |
| 29 | * @param string $searchEngineEntryPoint |
| 30 | * @param int[] $namespaces |
| 31 | * @param string[] $acceptableQueryClasses |
| 32 | * @param string $profileContext |
| 33 | * @param float $score |
| 34 | */ |
| 35 | public function __construct( |
| 36 | $searchEngineEntryPoint, |
| 37 | array $namespaces, |
| 38 | array $acceptableQueryClasses, |
| 39 | $profileContext, |
| 40 | $score |
| 41 | ) { |
| 42 | $this->searchEngineEntryPoint = $searchEngineEntryPoint; |
| 43 | $this->namespaces = $namespaces; |
| 44 | $this->acceptableQueryClasses = $acceptableQueryClasses; |
| 45 | $this->profileContext = $profileContext; |
| 46 | $this->score = $score; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param \CirrusSearch\Search\SearchQuery $query |
| 51 | * @return float |
| 52 | */ |
| 53 | public function score( \CirrusSearch\Search\SearchQuery $query ) { |
| 54 | Assert::parameter( $query->getSearchEngineEntryPoint() === $this->searchEngineEntryPoint, |
| 55 | 'query', |
| 56 | "must be {$this->searchEngineEntryPoint} but {$query->getSearchEngineEntryPoint()} given." ); |
| 57 | |
| 58 | if ( $this->namespaces !== [] ) { |
| 59 | $qNs = $query->getNamespaces(); |
| 60 | if ( $qNs === [] ) { |
| 61 | return 0.0; |
| 62 | } |
| 63 | if ( count( array_intersect( $this->namespaces, $qNs ) ) !== count( $qNs ) ) { |
| 64 | return 0.0; |
| 65 | } |
| 66 | } |
| 67 | if ( $this->acceptableQueryClasses !== [] ) { |
| 68 | $parsedQuery = $query->getParsedQuery(); |
| 69 | $match = false; |
| 70 | foreach ( $this->acceptableQueryClasses as $qClass ) { |
| 71 | if ( $parsedQuery->isQueryOfClass( $qClass ) ) { |
| 72 | $match = true; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | if ( !$match ) { |
| 77 | return 0.0; |
| 78 | } |
| 79 | } |
| 80 | return $this->score; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * The entry point used in the search engine: |
| 85 | * - searchText |
| 86 | * - nearMatch |
| 87 | * - completionSearch |
| 88 | * |
| 89 | * @return string |
| 90 | */ |
| 91 | public function getSearchEngineEntryPoint() { |
| 92 | return $this->searchEngineEntryPoint; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * The SearchProfile context to use when this route is chosen. |
| 97 | * |
| 98 | * @return string |
| 99 | */ |
| 100 | public function getProfileContext() { |
| 101 | return $this->profileContext; |
| 102 | } |
| 103 | } |