Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
InTitleFeature
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
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
 getRegexHLFlavor
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
 getNonRegexFilterQuery
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 buildNonRegexHLFields
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\Parser\AST\KeywordFeatureNode;
6use CirrusSearch\Query\Builder\QueryBuildingContext;
7use CirrusSearch\Search\Escaper;
8use CirrusSearch\Search\Fetch\HighlightedField;
9use CirrusSearch\Search\Filters;
10use CirrusSearch\Search\SearchContext;
11use CirrusSearch\SearchConfig;
12use Elastica\Query\AbstractQuery;
13use MediaWiki\MainConfigNames;
14
15/**
16 * Applies a filter against the title field in elasticsearch. When not negated
17 * the term remains in the original query as a scoring signal. The term itself
18 * is used as a QueryString query, so some advanced syntax like * and phrase
19 * matches can be used. Note that quotes in the incoming query are maintained
20 * in the generated filter.
21 *
22 * Examples:
23 *   intitle:Foo
24 *   intitle:Foo*
25 *   intitle:"gold rush"
26 *
27 * Things that might seem like they would work, but don't. This is because the
28 * quotes are maintained in the filter and in the top level query.
29 *   intitle:"foo*"
30 *   intitle:"foo OR bar"
31 */
32class InTitleFeature extends BaseRegexFeature {
33
34    /**
35     * @var Escaper an escaper used to sanitize queries when not used as regular expression
36     *
37     * TODO: do not rely on escaper here, this should be consistent with what the Parser does.
38     * @see Filters::intitle()
39     */
40    private $escaper;
41
42    public function __construct( SearchConfig $config ) {
43        parent::__construct(
44            $config,
45            [
46                'title' => HighlightedField::TARGET_TITLE_SNIPPET,
47                'redirect.title' => HighlightedField::TARGET_REDIRECT_SNIPPET
48            ]
49        );
50        $this->escaper = new Escaper(
51            $config->get( MainConfigNames::LanguageCode ),
52            $config->get( 'CirrusSearchAllowLeadingWildcard' )
53        );
54    }
55
56    /**
57     * @return string[]
58     */
59    protected function getKeywords() {
60        return [ 'intitle' ];
61    }
62
63    protected function getRegexHLFlavor(): string {
64        return "lucene_anchored";
65    }
66
67    /**
68     * @param SearchContext $context
69     * @param string $key The keyword
70     * @param string $value The value attached to the keyword with quotes stripped
71     * @param string $quotedValue The original value in the search string, including quotes if used
72     * @param bool $negated Is the search negated? Not used to generate the returned AbstractQuery,
73     *  that will be negated as necessary. Used for any other building/context necessary.
74     * @return array Two element array, first an AbstractQuery or null to apply to the
75     *  query. Second a boolean indicating if the quotedValue should be kept in the search
76     *  string.
77     */
78    protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) {
79        $filter = Filters::intitle( $context->escaper(), $quotedValue, $value !== $quotedValue );
80
81        return [ $filter, !$negated ];
82    }
83
84    /**
85     * @param KeywordFeatureNode $node
86     * @param QueryBuildingContext $context
87     * @return AbstractQuery|null
88     */
89    protected function getNonRegexFilterQuery( KeywordFeatureNode $node, QueryBuildingContext $context ) {
90        return Filters::intitle( $this->escaper, $node->getQuotedValue(), $node->getValue() !== $node->getQuotedValue() );
91    }
92
93    /** @inheritDoc */
94    public function buildNonRegexHLFields( KeywordFeatureNode $node, QueryBuildingContext $context ) {
95        // we highlight this field a bit differently as it's part of the main query
96        return [];
97    }
98}