Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
FileTypeFeature
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
7 / 7
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 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%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 parseValue
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
4
 matchFileType
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getFilterQuery
100.00% covered (success)
100.00%
2 / 2
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\WarningCollector;
12use Elastica\Query;
13use Elastica\Query\AbstractQuery;
14use MediaWiki\Config\Config;
15
16/**
17 * File type features:
18 *  filetype:bitmap
19 * Types can be OR'd together:
20 *  filetype:bitmap|png
21 * Selects only files of these specified features.
22 */
23class FileTypeFeature extends SimpleKeywordFeature implements FilterQueryFeature {
24    public const MAX_CONDITIONS = 20;
25
26    /** @var string[] Map of aliases from user provided term to term to search for */
27    private $aliases;
28
29    public function __construct( Config $config ) {
30        $this->aliases = $config->get( CirrusConfigNames::FiletypeAliases );
31    }
32
33    /**
34     * @return string[]
35     */
36    protected function getKeywords() {
37        return [ 'filetype' ];
38    }
39
40    /**
41     * @param KeywordFeatureNode $node
42     * @return CrossSearchStrategy
43     */
44    public function getCrossSearchStrategy( KeywordFeatureNode $node ) {
45        return CrossSearchStrategy::allWikisStrategy();
46    }
47
48    /**
49     * @param SearchContext $context
50     * @param string $key The keyword
51     * @param string $value The value attached to the keyword with quotes stripped
52     * @param string $quotedValue The original value in the search string, including quotes
53     *     if used
54     * @param bool $negated Is the search negated? Not used to generate the returned
55     *     AbstractQuery, that will be negated as necessary. Used for any other building/context
56     *     necessary.
57     * @return array Two element array, first an AbstractQuery or null to apply to the
58     *  query. Second a boolean indicating if the quotedValue should be kept in the search
59     *  string.
60     */
61    protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) {
62        $parsed = $this->parseValue( $key, $value, $quotedValue, '', '', $context );
63        '@phan-var array $parsed';
64        $query = $this->matchFileType( array_merge( $parsed['aliased'], $parsed['user_types'] ) );
65
66        return [ $query, false ];
67    }
68
69    /**
70     * @param string $key
71     * @param string $value
72     * @param string $quotedValue
73     * @param string $valueDelimiter
74     * @param string $suffix
75     * @param WarningCollector $warningCollector
76     * @return array|false|null
77     */
78    public function parseValue( $key, $value, $quotedValue, $valueDelimiter, $suffix, WarningCollector $warningCollector ) {
79        // TODO: The explode/count/warn is repeated elsewhere and should be generalized?
80        $types = explode( '|', $value );
81        if ( count( $types ) > self::MAX_CONDITIONS ) {
82            $warningCollector->addWarning(
83                'cirrussearch-feature-too-many-conditions',
84                $key,
85                self::MAX_CONDITIONS
86            );
87            $types = array_slice(
88                $types,
89                0,
90                self::MAX_CONDITIONS
91            );
92        }
93
94        $aliased = [];
95        foreach ( $types as $type ) {
96            $lcType = mb_strtolower( $type );
97            if ( isset( $this->aliases[$lcType] ) ) {
98                $aliased[] = $this->aliases[$lcType];
99            }
100        }
101
102        return [
103            'user_types' => $types,
104            'aliased' => $aliased,
105        ];
106    }
107
108    /**
109     * @param string[] $types
110     * @return Query\BoolQuery|Query\MatchQuery|null
111     */
112    protected function matchFileType( $types ) {
113        $queries = [];
114        foreach ( $types as $type ) {
115            $queries[] = new Query\MatchQuery( 'file_media_type', [ 'query' => $type ] );
116        }
117
118        return Filters::booleanOr( $queries, false );
119    }
120
121    /**
122     * @param KeywordFeatureNode $node
123     * @param QueryBuildingContext $context
124     * @return AbstractQuery|null
125     */
126    public function getFilterQuery( KeywordFeatureNode $node, QueryBuildingContext $context ) {
127        $parsed = $node->getParsedValue();
128        '@phan-var array $parsed';
129        return $this->matchFileType( array_merge( $parsed['aliased'], $parsed['user_types'] ) );
130    }
131}