Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CirrusSearchInProjectFeature
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 getKeywords
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 parseValue
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 doApply
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace MediaWiki\Extension\PageAssessments;
4
5use CirrusSearch\Query\SimpleKeywordFeature;
6use CirrusSearch\Search\SearchContext;
7use CirrusSearch\WarningCollector;
8use CirrusSearch\Wikimedia\WeightedTagsHooks;
9use Elastica\Query\DisMax;
10use Elastica\Query\Term;
11
12class CirrusSearchInProjectFeature extends SimpleKeywordFeature {
13    public const MAX_CONDITIONS = 32;
14    public const TAG_PREFIX = 'ext.pageassessments.project';
15
16    /** @inheritDoc */
17    protected function getKeywords() {
18        return [ 'inproject' ];
19    }
20
21    /** @inheritDoc */
22    public function parseValue( $key, $value, $quotedValue, $valueDelimiter, $suffix,
23        WarningCollector $warningCollector
24    ) {
25        $values = explode( '|', $value, self::MAX_CONDITIONS + 1 );
26        if ( count( $values ) > self::MAX_CONDITIONS ) {
27            $warningCollector->addWarning(
28                'cirrussearch-feature-too-many-conditions',
29                $key,
30                self::MAX_CONDITIONS
31            );
32            $values = array_slice(
33                $values,
34                0,
35                self::MAX_CONDITIONS
36            );
37        }
38        return [ 'projects' => $values ];
39    }
40
41    /** @inheritDoc */
42    protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) {
43        $parsed = $this->parseValue( $key, $value, $quotedValue, '', '', $context );
44        '@phan-var array $parsed';
45        $projects = $parsed['projects'];
46        if ( $projects === [] ) {
47            $context->setResultsPossible( false );
48            return [ null, true ];
49        }
50        $query = new DisMax();
51        foreach ( $projects as $project ) {
52            $projectQuery = new Term();
53            $projectQuery->setTerm( WeightedTagsHooks::FIELD_NAME, self::TAG_PREFIX . '/' . $project );
54            $query->addQuery( $projectQuery );
55        }
56
57        if ( !$negated ) {
58            $context->addNonTextQuery( $query );
59            return [ null, false ];
60        } else {
61            return [ $query, false ];
62        }
63    }
64}