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