Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
24 / 24 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| HasLemmaKeyword | |
100.00% |
24 / 24 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
1 / 1 |
| getKeywords | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseValue | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
| getCrossSearchStrategy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| doApply | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getFilterQuery | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| buildFilterQuery | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Wikibase\Lexeme\Search\Elastic; |
| 4 | |
| 5 | use CirrusSearch\CrossSearchStrategy; |
| 6 | use CirrusSearch\Parser\AST\KeywordFeatureNode; |
| 7 | use CirrusSearch\Query\Builder\QueryBuildingContext; |
| 8 | use CirrusSearch\Query\FilterQueryFeature; |
| 9 | use CirrusSearch\Query\SimpleKeywordFeature; |
| 10 | use CirrusSearch\Search\Filters; |
| 11 | use CirrusSearch\Search\SearchContext; |
| 12 | use CirrusSearch\WarningCollector; |
| 13 | use Elastica\Query\AbstractQuery; |
| 14 | use Elastica\Query\MatchQuery; |
| 15 | |
| 16 | class HasLemmaKeyword extends SimpleKeywordFeature implements FilterQueryFeature { |
| 17 | public const MAX_CONDITIONS = 30; |
| 18 | |
| 19 | /** |
| 20 | * @inheritDoc |
| 21 | */ |
| 22 | protected function getKeywords() { |
| 23 | return [ 'haslemma' ]; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @inheritDoc |
| 28 | */ |
| 29 | public function parseValue( |
| 30 | $key, |
| 31 | $value, |
| 32 | $quotedValue, |
| 33 | $valueDelimiter, |
| 34 | $suffix, |
| 35 | WarningCollector $warningCollector |
| 36 | ) { |
| 37 | $variants = explode( ',', $value ); |
| 38 | if ( count( $variants ) > self::MAX_CONDITIONS ) { |
| 39 | $warningCollector->addWarning( |
| 40 | 'cirrussearch-feature-too-many-conditions', |
| 41 | $key, |
| 42 | self::MAX_CONDITIONS |
| 43 | ); |
| 44 | $variants = array_slice( |
| 45 | $variants, |
| 46 | 0, |
| 47 | self::MAX_CONDITIONS |
| 48 | ); |
| 49 | } |
| 50 | return [ 'variants' => $variants ]; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @inheritDoc |
| 55 | */ |
| 56 | public function getCrossSearchStrategy( KeywordFeatureNode $node ): CrossSearchStrategy { |
| 57 | return CrossSearchStrategy::hostWikiOnlyStrategy(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @inheritDoc |
| 62 | */ |
| 63 | protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) { |
| 64 | $parsedValue = $this->parseValue( $key, $value, $quotedValue, '', '', $context ); |
| 65 | return [ $this->buildFilterQuery( $parsedValue ), false ]; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @inheritDoc |
| 70 | */ |
| 71 | public function getFilterQuery( KeywordFeatureNode $node, QueryBuildingContext $context ) { |
| 72 | return $this->buildFilterQuery( $node->getParsedValue() ); |
| 73 | } |
| 74 | |
| 75 | private function buildFilterQuery( array $parseValue ): ?AbstractQuery { |
| 76 | $queries = []; |
| 77 | foreach ( $parseValue[ 'variants' ] as $variant ) { |
| 78 | $trimmed = trim( $variant ); |
| 79 | if ( $trimmed !== '' ) { |
| 80 | $queries[] = ( new MatchQuery() )->setFieldQuery( 'lemma_spelling_variants', $variant ); |
| 81 | } |
| 82 | } |
| 83 | return Filters::booleanOr( $queries, false ); |
| 84 | } |
| 85 | } |