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