Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
MoreLikeFeature
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
9 / 9
10
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
 greedy
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 queryHeader
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
 getFeatureName
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%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 expand
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getConfig
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\CrossSearchStrategy;
6use CirrusSearch\Parser\AST\KeywordFeatureNode;
7use CirrusSearch\Search\SearchContext;
8use CirrusSearch\SearchConfig;
9use CirrusSearch\WarningCollector;
10use MediaWiki\Title\Title;
11
12/**
13 * Finds pages similar to another one.
14 * Greedy keyword kept for BC purposes, MoreLikeThisFeature should be preferred.
15 */
16class MoreLikeFeature extends SimpleKeywordFeature implements LegacyKeywordFeature {
17    use MoreLikeTrait;
18
19    private const MORE_LIKE_THIS = 'morelike';
20
21    /**
22     * @var SearchConfig
23     */
24    private $config;
25
26    /**
27     * @param SearchConfig $config
28     */
29    public function __construct( SearchConfig $config ) {
30        $this->config = $config;
31    }
32
33    /**
34     * @return bool
35     */
36    public function greedy() {
37        return true;
38    }
39
40    /**
41     * morelike is only allowed at the beginning of the query
42     * @return bool
43     */
44    public function queryHeader() {
45        return true;
46    }
47
48    /** @inheritDoc */
49    protected function getKeywords() {
50        return [ self::MORE_LIKE_THIS ];
51    }
52
53    /**
54     * @param string $key
55     * @param string $valueDelimiter
56     * @return string
57     */
58    public function getFeatureName( $key, $valueDelimiter ) {
59        return "more_like";
60    }
61
62    /**
63     * @param KeywordFeatureNode $node
64     * @return CrossSearchStrategy
65     */
66    public function getCrossSearchStrategy( KeywordFeatureNode $node ) {
67        // We depend on the db to fetch the title
68        return CrossSearchStrategy::hostWikiOnlyStrategy();
69    }
70
71    /**
72     * @param SearchContext $context
73     * @param string $key
74     * @param string $value
75     * @param string $quotedValue
76     * @param bool $negated
77     * @return array
78     */
79    protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) {
80        $context->setCacheTtl( $this->config->get( 'CirrusSearchMoreLikeThisTTL' ) );
81        $titles = $this->doExpand( $key, $value, $context );
82        if ( $titles === [] ) {
83            $context->setResultsPossible( false );
84            return [ null, false ];
85        }
86        $query = $this->buildMoreLikeQuery( $titles );
87
88        // this erases the main query making it impossible to combine with
89        // other keywords/search query. MoreLikeThisFeature addresses this problem.
90        $context->setMainQuery( $query );
91
92        // highlight snippets are not great so it's worth running a match all query
93        // to save cpu cycles
94        $context->setHighlightQuery( new \Elastica\Query\MatchAll() );
95
96        return [ null, false ];
97    }
98
99    /**
100     * @param KeywordFeatureNode $node
101     * @param SearchConfig $config
102     * @param WarningCollector $warningCollector
103     * @return array|Title[]
104     */
105    public function expand( KeywordFeatureNode $node, SearchConfig $config, WarningCollector $warningCollector ) {
106        return $this->doExpand( $node->getKey(), $node->getValue(), $warningCollector );
107    }
108
109    /**
110     * @return SearchConfig
111     */
112    public function getConfig(): SearchConfig {
113        return $this->config;
114    }
115}