Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.89% covered (success)
95.89%
70 / 73
88.89% covered (warning)
88.89%
24 / 27
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchQuery
95.89% covered (success)
95.89%
70 / 73
88.89% covered (warning)
88.89%
24 / 27
41
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 getDebugOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParsedQuery
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getInitialNamespaces
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getInitialCrossSearchStrategy
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCrossSearchStrategy
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 getContextualFilters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSearchEngineEntryPoint
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSort
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRandomSeed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getForcedProfiles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOffset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLimit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMaximumResultPosition
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getNamespaces
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
8.02
 getSearchConfig
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getForcedProfile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasForcedProfile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isWithDYMSuggestion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAllowRewrite
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getProfileContextParameters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getExtraFieldsToExtract
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 shouldProvideAllSnippets
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 mustTrackTotalHits
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 isTargetingDefaultSearchedNamespaces
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isUsingDefaultSearchedNamespaces
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 matchDefaultNamespaces
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace CirrusSearch\Search;
4
5use CirrusSearch\CirrusConfigNames;
6use CirrusSearch\CirrusDebugOptions;
7use CirrusSearch\CrossSearchStrategy;
8use CirrusSearch\Parser\AST\ParsedQuery;
9use CirrusSearch\SearchConfig;
10use MediaWiki\MainConfigNames;
11
12/**
13 * A search query, it contains all the necessary information to build and send a query to the backend.
14 * NOTE: Immutable value class.
15 */
16class SearchQuery {
17    /**
18     * Identifier for the fulltext SearchEngine entry point
19     * @see \SearchEngine::searchText()
20     */
21    public const SEARCH_TEXT = 'searchText';
22
23    /**
24     * @var ParsedQuery
25     */
26    private $parsedQuery;
27
28    /**
29     * @var int[]
30     */
31    private $initialNamespaces;
32
33    /**
34     * @var CrossSearchStrategy
35     */
36    private $initialCrossSearchStrategy;
37
38    /**
39     * @var CrossSearchStrategy|null (lazy loaded)
40     */
41    private $crossSearchStrategy;
42
43    /**
44     * @var \CirrusSearch\Query\Builder\ContextualFilter[]
45     */
46    private $contextualFilters;
47
48    /**
49     * Entry point from the SearchEngine
50     * TODO: clarify its usage and see whether or not another
51     * entry point var is needed to carry some provenance information
52     * from the UI.
53     * @var string
54     */
55    private $searchEngineEntryPoint;
56
57    /**
58     * @var string
59     */
60    private $sort;
61
62    /**
63     * @var int|null
64     */
65    private $randomSeed;
66
67    /**
68     * @var string[]
69     */
70    private $forcedProfiles;
71
72    /**
73     * @var int
74     */
75    private $offset;
76
77    /**
78     * @var int
79     */
80    private $limit;
81
82    /**
83     * @var CirrusDebugOptions
84     */
85    private $debugOptions;
86
87    /**
88     * @var SearchConfig
89     */
90    private $searchConfig;
91
92    /**
93     * @var bool
94     */
95    private $withDYMSuggestion;
96
97    /**
98     * @var bool
99     */
100    private $allowRewrite;
101
102    /**
103     * @var string[] parameters for SearchProfileService
104     * @see \CirrusSearch\Profile\ContextualProfileOverride
105     */
106    private $profileContextParameters;
107
108    /**
109     * @var string[] list of extra fields to extract
110     */
111    private $extraFieldsToExtract;
112
113    /**
114     * @var bool
115     */
116    private $provideAllSnippets;
117
118    /**
119     * @param ParsedQuery $parsedQuery
120     * @param int[] $initialNamespaces
121     * @param CrossSearchStrategy $initialCrosswikiStrategy
122     * @param \CirrusSearch\Query\Builder\ContextualFilter[] $contextualFilters
123     * @param string $searchEngineEntryPoint
124     * @param string $sort
125     * @param int|null $randomSeed
126     * @param string[] $forcedProfiles
127     * @param int $offset
128     * @param int $limit
129     * @param CirrusDebugOptions $debugOptions
130     * @param SearchConfig $searchConfig
131     * @param bool $withDYMSuggestion
132     * @param bool $allowRewrite
133     * @param string[] $profileContextParameters
134     * @param string[] $extraFieldsToExtract
135     * @param bool $provideAllSnippets
136     * @see SearchQueryBuilder
137     */
138    public function __construct(
139        ParsedQuery $parsedQuery,
140        array $initialNamespaces,
141        CrossSearchStrategy $initialCrosswikiStrategy,
142        array $contextualFilters,
143        $searchEngineEntryPoint,
144        $sort,
145        $randomSeed,
146        array $forcedProfiles,
147        $offset,
148        $limit,
149        CirrusDebugOptions $debugOptions,
150        SearchConfig $searchConfig,
151        $withDYMSuggestion,
152        $allowRewrite,
153        array $profileContextParameters,
154        array $extraFieldsToExtract,
155        bool $provideAllSnippets
156    ) {
157        $this->parsedQuery = $parsedQuery;
158        $this->initialNamespaces = $initialNamespaces;
159        $this->initialCrossSearchStrategy = $initialCrosswikiStrategy;
160        $this->contextualFilters = $contextualFilters;
161        $this->searchEngineEntryPoint = $searchEngineEntryPoint;
162        $this->sort = $sort;
163        $this->randomSeed = $randomSeed;
164        $this->forcedProfiles = $forcedProfiles;
165        $this->offset = $offset;
166        $this->limit = $limit;
167        $this->debugOptions = $debugOptions;
168        $this->searchConfig = $searchConfig;
169        $this->withDYMSuggestion = $withDYMSuggestion;
170        $this->allowRewrite = $allowRewrite;
171        $this->profileContextParameters = $profileContextParameters;
172        $this->extraFieldsToExtract = $extraFieldsToExtract;
173        $this->provideAllSnippets = $provideAllSnippets;
174    }
175
176    public function getDebugOptions(): CirrusDebugOptions {
177        return $this->debugOptions;
178    }
179
180    public function getParsedQuery(): ParsedQuery {
181        return $this->parsedQuery;
182    }
183
184    /**
185     * @return int[]
186     */
187    public function getInitialNamespaces() {
188        return $this->initialNamespaces;
189    }
190
191    public function getInitialCrossSearchStrategy(): CrossSearchStrategy {
192        return $this->initialCrossSearchStrategy;
193    }
194
195    public function getCrossSearchStrategy(): CrossSearchStrategy {
196        if ( $this->crossSearchStrategy === null ) {
197            if ( $this->contextualFilters !== [] ) {
198                $this->crossSearchStrategy = CrossSearchStrategy::hostWikiOnlyStrategy();
199            } else {
200                $this->crossSearchStrategy = $this->parsedQuery
201                    ->getCrossSearchStrategy()
202                    ->intersect( $this->initialCrossSearchStrategy );
203            }
204        }
205        return $this->crossSearchStrategy;
206    }
207
208    /**
209     * @return \CirrusSearch\Query\Builder\ContextualFilter[]
210     */
211    public function getContextualFilters(): array {
212        return $this->contextualFilters;
213    }
214
215    /**
216     * From which SearchEngine method this query entered CirrusSearch
217     * @return string
218     */
219    public function getSearchEngineEntryPoint() {
220        return $this->searchEngineEntryPoint;
221    }
222
223    /**
224     * @return string
225     */
226    public function getSort() {
227        return $this->sort;
228    }
229
230    public function getRandomSeed(): ?int {
231        return $this->randomSeed;
232    }
233
234    /**
235     * @return string[]
236     */
237    public function getForcedProfiles(): array {
238        return $this->forcedProfiles;
239    }
240
241    /**
242     * @return int
243     */
244    public function getOffset() {
245        return $this->offset;
246    }
247
248    /**
249     * @return int
250     */
251    public function getLimit() {
252        return $this->limit;
253    }
254
255    public function getMaximumResultPosition(): int {
256        return $this->offset + $this->limit;
257    }
258
259    /**
260     * List of namespaces required to run this query.
261     *
262     * @return int[] list of namespaces, empty array means that all namespaces
263     * are required.
264     */
265    public function getNamespaces(): array {
266        $additionalRequired = null;
267        if ( $this->initialNamespaces != [] && $this->contextualFilters != [] ) {
268            foreach ( $this->contextualFilters as $filter ) {
269                $additional = $filter->requiredNamespaces();
270                if ( $additional === null ) {
271                    continue;
272                }
273                if ( $additional === [] ) {
274                    $additionalRequired = [];
275                    break;
276                }
277                if ( $additionalRequired === null ) {
278                    $additionalRequired = $additional;
279                } else {
280                    $additionalRequired = array_merge( $additionalRequired, $additional );
281                }
282            }
283            if ( $additionalRequired !== null ) {
284                $additionalRequired = array_unique( $additionalRequired );
285            }
286        }
287        return $this->parsedQuery->getActualNamespaces( $this->initialNamespaces, $additionalRequired );
288    }
289
290    public function getSearchConfig(): SearchConfig {
291        return $this->searchConfig;
292    }
293
294    /**
295     * @param string $profileType
296     * @see \CirrusSearch\Profile\SearchProfileService
297     * @return string|null name of the profile or null if nothing forced for this type
298     */
299    public function getForcedProfile( $profileType ) {
300        return $this->forcedProfiles[$profileType] ?? null;
301    }
302
303    /**
304     * @return bool
305     */
306    public function hasForcedProfile() {
307        return $this->forcedProfiles !== [];
308    }
309
310    /**
311     * @return bool
312     */
313    public function isWithDYMSuggestion() {
314        return $this->withDYMSuggestion;
315    }
316
317    /**
318     * @return bool
319     */
320    public function isAllowRewrite() {
321        return $this->allowRewrite;
322    }
323
324    /**
325     * @return string[]
326     * @see \CirrusSearch\Profile\ContextualProfileOverride
327     */
328    public function getProfileContextParameters() {
329        return $this->profileContextParameters;
330    }
331
332    /**
333     * @return string[]
334     * @see \CirrusSearch\Search\FullTextResultsType
335     */
336    public function getExtraFieldsToExtract(): array {
337        return $this->extraFieldsToExtract;
338    }
339
340    public function shouldProvideAllSnippets(): bool {
341        return $this->provideAllSnippets;
342    }
343
344    public function mustTrackTotalHits(): bool {
345        $queryClasses = $this->getSearchConfig()->get( CirrusConfigNames::MustTrackTotalHits ) ?: [];
346        foreach ( $queryClasses as $queryClass => $track ) {
347            if ( $queryClass === "default" ) {
348                continue;
349            }
350            if ( $this->parsedQuery->isQueryOfClass( $queryClass ) ) {
351                return $track;
352            }
353        }
354        return $queryClasses['default'] ?? true;
355    }
356
357    /**
358     * Identify if this query targets the default set of namespaces
359     * @return bool true if the target namespaces are equals to the default searched namespaces
360     */
361    public function isTargetingDefaultSearchedNamespaces(): bool {
362        return $this->matchDefaultNamespaces( $this->getNamespaces() );
363    }
364
365    /**
366     * Identify if this query initially targets the default set of namespaces
367     * @return bool true if the initial namespaces are equals to the default searched namespaces
368     */
369    public function isUsingDefaultSearchedNamespaces(): bool {
370        return $this->matchDefaultNamespaces( $this->initialNamespaces );
371    }
372
373    private function matchDefaultNamespaces( array $namespaces ): bool {
374        $defaultSearchedNamespaces = $this->getSearchConfig()->get( MainConfigNames::NamespacesToBeSearchedDefault );
375        if ( is_array( $defaultSearchedNamespaces ) ) {
376            $defaultSearchedNamespaces = array_map( intval( ... ), array_keys( $defaultSearchedNamespaces, true ) );
377            return $namespaces == $defaultSearchedNamespaces;
378        }
379        return false;
380    }
381}