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    public function __construct( SearchConfig $config ) {
27        $this->config = $config;
28    }
29
30    /**
31     * @return bool
32     */
33    public function greedy() {
34        return true;
35    }
36
37    /**
38     * morelike is only allowed at the beginning of the query
39     * @return bool
40     */
41    public function queryHeader() {
42        return true;
43    }
44
45    /** @inheritDoc */
46    protected function getKeywords() {
47        return [ self::MORE_LIKE_THIS ];
48    }
49
50    /**
51     * @param string $key
52     * @param string $valueDelimiter
53     * @return string
54     */
55    public function getFeatureName( $key, $valueDelimiter ) {
56        return "more_like";
57    }
58
59    /**
60     * @param KeywordFeatureNode $node
61     * @return CrossSearchStrategy
62     */
63    public function getCrossSearchStrategy( KeywordFeatureNode $node ) {
64        // We depend on the db to fetch the title
65        return CrossSearchStrategy::hostWikiOnlyStrategy();
66    }
67
68    /**
69     * @param SearchContext $context
70     * @param string $key
71     * @param string $value
72     * @param string $quotedValue
73     * @param bool $negated
74     * @return array
75     */
76    protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) {
77        $context->setCacheTtl( $this->config->get( 'CirrusSearchMoreLikeThisTTL' ) );
78        $titles = $this->doExpand( $key, $value, $context );
79        if ( $titles === [] ) {
80            $context->setResultsPossible( false );
81            return [ null, false ];
82        }
83        $query = $this->buildMoreLikeQuery( $titles );
84
85        // this erases the main query making it impossible to combine with
86        // other keywords/search query. MoreLikeThisFeature addresses this problem.
87        $context->setMainQuery( $query );
88
89        // highlight snippets are not great so it's worth running a match all query
90        // to save cpu cycles
91        $context->setHighlightQuery( new \Elastica\Query\MatchAll() );
92
93        return [ null, false ];
94    }
95
96    /**
97     * @param KeywordFeatureNode $node
98     * @param SearchConfig $config
99     * @param WarningCollector $warningCollector
100     * @return array|Title[]
101     */
102    public function expand( KeywordFeatureNode $node, SearchConfig $config, WarningCollector $warningCollector ) {
103        return $this->doExpand( $node->getKey(), $node->getValue(), $warningCollector );
104    }
105
106    public function getConfig(): SearchConfig {
107        return $this->config;
108    }
109}