Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
25 / 25 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
LanguageFeature | |
100.00% |
25 / 25 |
|
100.00% |
6 / 6 |
10 | |
100.00% |
1 / 1 |
getKeywords | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCrossSearchStrategy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
doApply | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
parseValue | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
doGetFilterQuery | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
getFilterQuery | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Query; |
4 | |
5 | use CirrusSearch\CrossSearchStrategy; |
6 | use CirrusSearch\Parser\AST\KeywordFeatureNode; |
7 | use CirrusSearch\Query\Builder\QueryBuildingContext; |
8 | use CirrusSearch\Search\Filters; |
9 | use CirrusSearch\Search\SearchContext; |
10 | use CirrusSearch\WarningCollector; |
11 | use Elastica\Query\AbstractQuery; |
12 | |
13 | /** |
14 | * Filters the result set based on pages labeled with the provided language. |
15 | * More than one language can be specified with commas and they will be |
16 | * generated as an OR query. |
17 | * |
18 | * Examples: |
19 | * inlanguage:en |
20 | * inlanguage:fr,en |
21 | */ |
22 | class LanguageFeature extends SimpleKeywordFeature implements FilterQueryFeature { |
23 | /** |
24 | * Limit search to 20 languages. Arbitrarily chosen, but should be more |
25 | * than enough and some sort of limit has to be enforced. |
26 | */ |
27 | public const QUERY_LIMIT = 20; |
28 | |
29 | /** |
30 | * @return string[] |
31 | */ |
32 | protected function getKeywords() { |
33 | return [ 'inlanguage' ]; |
34 | } |
35 | |
36 | /** |
37 | * @param KeywordFeatureNode $node |
38 | * @return CrossSearchStrategy |
39 | */ |
40 | public function getCrossSearchStrategy( KeywordFeatureNode $node ) { |
41 | return CrossSearchStrategy::allWikisStrategy(); |
42 | } |
43 | |
44 | /** |
45 | * @param SearchContext $context |
46 | * @param string $key The keyword |
47 | * @param string $value The value attached to the keyword with quotes stripped |
48 | * @param string $quotedValue The original value in the search string, including quotes if used |
49 | * @param bool $negated Is the search negated? Not used to generate the returned AbstractQuery, |
50 | * that will be negated as necessary. Used for any other building/context necessary. |
51 | * @return array Two element array, first an AbstractQuery or null to apply to the |
52 | * query. Second a boolean indicating if the quotedValue should be kept in the search |
53 | * string. |
54 | */ |
55 | protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) { |
56 | $parsedValue = $this->parseValue( $key, $value, $quotedValue, '', '', $context ); |
57 | return [ $this->doGetFilterQuery( $parsedValue ), false ]; |
58 | } |
59 | |
60 | /** |
61 | * @param string $key |
62 | * @param string $value |
63 | * @param string $quotedValue |
64 | * @param string $valueDelimiter |
65 | * @param string $suffix |
66 | * @param WarningCollector $warningCollector |
67 | * @return array|false|null |
68 | */ |
69 | public function parseValue( $key, $value, $quotedValue, $valueDelimiter, $suffix, WarningCollector $warningCollector ) { |
70 | if ( strpos( $value, ',' ) !== false ) { |
71 | $langs = explode( ',', $value ); |
72 | $warningCollector->addWarning( 'cirrussearch-inlanguage-deprecate-comma' ); |
73 | } else { |
74 | $langs = explode( '|', $value ); |
75 | } |
76 | if ( count( $langs ) > self::QUERY_LIMIT ) { |
77 | $warningCollector->addWarning( |
78 | 'cirrussearch-feature-too-many-conditions', |
79 | $key, |
80 | self::QUERY_LIMIT |
81 | ); |
82 | $langs = array_slice( $langs, 0, self::QUERY_LIMIT ); |
83 | } |
84 | return [ 'langs' => $langs ]; |
85 | } |
86 | |
87 | /** |
88 | * @param array[] $parsedValue |
89 | * @return \Elastica\Query\AbstractQuery|\Elastica\Query\MatchQuery|null |
90 | */ |
91 | private function doGetFilterQuery( $parsedValue ) { |
92 | $queries = []; |
93 | foreach ( $parsedValue['langs'] as $lang ) { |
94 | if ( strlen( trim( $lang ) ) > 0 ) { |
95 | $query = new \Elastica\Query\MatchQuery(); |
96 | $query->setFieldQuery( 'language', $lang ); |
97 | $queries[] = $query; |
98 | } |
99 | } |
100 | $query = Filters::booleanOr( $queries, false ); |
101 | |
102 | return $query; |
103 | } |
104 | |
105 | /** |
106 | * @param KeywordFeatureNode $node |
107 | * @param QueryBuildingContext $context |
108 | * @return AbstractQuery|null |
109 | */ |
110 | public function getFilterQuery( KeywordFeatureNode $node, QueryBuildingContext $context ) { |
111 | return $this->doGetFilterQuery( $node->getParsedValue() ); |
112 | } |
113 | } |