Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.67% covered (warning)
86.67%
26 / 30
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FullTextKeywordRegistry
86.67% covered (warning)
86.67%
26 / 30
50.00% covered (danger)
50.00%
1 / 2
5.06
0.00% covered (danger)
0.00%
0 / 1
 __construct
86.21% covered (warning)
86.21%
25 / 29
0.00% covered (danger)
0.00%
0 / 1
4.04
 getKeywords
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch\Parser;
4
5use CirrusSearch\CirrusSearchHookRunner;
6use CirrusSearch\Query\BoostTemplatesFeature;
7use CirrusSearch\Query\ContentModelFeature;
8use CirrusSearch\Query\DeepcatFeature;
9use CirrusSearch\Query\FileNumericFeature;
10use CirrusSearch\Query\FileTypeFeature;
11use CirrusSearch\Query\HasTemplateFeature;
12use CirrusSearch\Query\InCategoryFeature;
13use CirrusSearch\Query\InSourceFeature;
14use CirrusSearch\Query\InTitleFeature;
15use CirrusSearch\Query\KeywordFeature;
16use CirrusSearch\Query\LanguageFeature;
17use CirrusSearch\Query\LinksToFeature;
18use CirrusSearch\Query\LocalFeature;
19use CirrusSearch\Query\MoreLikeFeature;
20use CirrusSearch\Query\MoreLikeThisFeature;
21use CirrusSearch\Query\PageIdFeature;
22use CirrusSearch\Query\PreferRecentFeature;
23use CirrusSearch\Query\PrefixFeature;
24use CirrusSearch\Query\SimpleKeywordFeature;
25use CirrusSearch\Query\SubPageOfFeature;
26use CirrusSearch\Query\TextFieldFilterFeature;
27use CirrusSearch\SearchConfig;
28use MediaWiki\Logger\LoggerFactory;
29use MediaWiki\MediaWikiServices;
30use MediaWiki\Sparql\SparqlClient;
31
32/**
33 * Registry of keywords suited for fulltext searches
34 */
35class FullTextKeywordRegistry implements KeywordRegistry {
36    /**
37     * @var KeywordFeature[]
38     */
39    private $features;
40
41    /**
42     * @param SearchConfig $config
43     * @param CirrusSearchHookRunner|null $cirrusSearchHookRunner
44     * @param NamespacePrefixParser|null $namespacePrefixParser
45     * @param SparqlClient|null $client
46     */
47    public function __construct(
48        SearchConfig $config,
49        CirrusSearchHookRunner $cirrusSearchHookRunner = null,
50        NamespacePrefixParser $namespacePrefixParser = null,
51        SparqlClient $client = null
52    ) {
53        $this->features = [
54            // Handle morelike keyword (greedy). Kept for BC reasons with existing clients.
55            // The morelikethis keyword should be preferred.
56            new MoreLikeFeature( $config ),
57            // Handle title prefix notation (greedy). Kept for BC reasons with existing clients.
58            // The subpageof keyword should be preferred.
59            new PrefixFeature( $namespacePrefixParser ),
60            // Handle prefer-recent keyword
61            new PreferRecentFeature( $config ),
62            // Handle local keyword
63            new LocalFeature(),
64            // Handle boost-templates keyword
65            new BoostTemplatesFeature(),
66            // Handle hastemplate keyword
67            new HasTemplateFeature(),
68            // Handle linksto keyword
69            new LinksToFeature(),
70            // Handle incategory keyword
71            new InCategoryFeature( $config ),
72            // Handle non-regex insource keyword
73            new InSourceFeature( $config ),
74            // Handle intitle keyword
75            new InTitleFeature( $config ),
76            // inlanguage keyword
77            new LanguageFeature(),
78            // File types
79            new FileTypeFeature( $config ),
80            // File mime types
81            new TextFieldFilterFeature( 'filemime', 'file_mime' ),
82            // File numeric characteristics - size, resolution, etc.
83            new FileNumericFeature(),
84            // Content model feature
85            new ContentModelFeature(),
86            // subpageof keyword
87            new SubPageOfFeature(),
88            // deepcat feature
89            new DeepcatFeature( $config, $client ),
90            // morelikethis feature: a non-greedy version of the morelike keyword.
91            new MoreLikeThisFeature( $config ),
92            // ids query
93            new PageIdFeature()
94        ];
95
96        $extraFeatures = [];
97        $cirrusSearchHookRunner = $cirrusSearchHookRunner ?: new CirrusSearchHookRunner(
98            MediaWikiServices::getInstance()->getHookContainer() );
99        $cirrusSearchHookRunner->onCirrusSearchAddQueryFeatures( $config, $extraFeatures );
100        foreach ( $extraFeatures as $extra ) {
101            if ( $extra instanceof SimpleKeywordFeature ) {
102                $this->features[] = $extra;
103            } else {
104                LoggerFactory::getInstance( 'CirrusSearch' )
105                    ->warning( 'Skipped invalid feature of class ' . get_class( $extra ) .
106                               ' - should be instanceof SimpleKeywordFeature' );
107            }
108        }
109    }
110
111    /**
112     * @return KeywordFeature[]
113     */
114    public function getKeywords() {
115        return $this->features;
116    }
117}