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