Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.33% covered (success)
98.33%
118 / 120
92.31% covered (success)
92.31%
12 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchProfileServiceFactory
98.33% covered (success)
98.33%
118 / 120
92.31% covered (success)
92.31%
12 / 13
23
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 loadService
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
 loadCrossProjectBlockScorer
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 loadSimilarityProfiles
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 loadRescoreProfiles
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
1
 loadCompletionProfiles
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 loadPhraseSuggesterProfiles
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 loadIndexLookupFallbackProfiles
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 loadSaneitizerProfiles
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 loadDocumentSizeLimiterProfiles
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 loadFullTextQueryProfiles
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 loadInterwikiOverrides
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
9.16
 loadFallbackProfiles
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch\Profile;
4
5use BagOStuff;
6use CirrusSearch\CirrusSearchHookRunner;
7use CirrusSearch\InterwikiResolver;
8use CirrusSearch\SearchConfig;
9use MediaWiki\User\UserIdentity;
10use MediaWiki\User\UserOptionsLookup;
11use WebRequest;
12
13/**
14 * Default factory to build and prepare search profiles.
15 *
16 * The factory will load these defaults:
17 * <ul>
18 * <li>COMPLETION in CONTEXT_DEFAULT
19 *  <ul>
20 *   <li>default: <i>fuzzy</i></li>
21 *   <li>config override: <var>CirrusSearchCompletionSettings</var></li>
22 *   <li>user pref override: <var>cirrussearch-pref-completion-profile</var></li>
23 *  </ul>
24 * </li>
25 * <li>CROSS_PROJECT_BLOCK_SCORER in CONTEXT_DEFAULT
26 *  <ul>
27 *   <li>default: <i>static</i></li>
28 *   <li>config override: <var>CirrusSearchCrossProjectOrder</var></li>
29 *  </ul>
30 * </li>
31 * <li>FT_QUERY_BUILDER in CONTEXT_DEFAULT
32 *  <ul>
33 *   <li>default: <i>default</i></li>
34 *   <li>config override: <var>CirrusSearchFullTextQueryBuilderProfile</var></li>
35 *   <li>uri param override: <var>cirrusFTQBProfile</var></li>
36 *  </ul>
37 * </li>
38 * <li>PHRASE_SUGGESTER in CONTEXT_DEFAULT
39 *  <ul>
40 *   <li>default: <i>no defaults (selected by fallback methods profiles)</i></li>
41 *  </ul>
42 * </li>
43 * <li>RESCORE in CONTEXT_DEFAULT and CONTEXT_PREFIXSEARCH
44 *  <ul>
45 *   <li>default (CONTEXT_DEFAULT & CONTEXT_PREFIXSEARCH): <i>classic</i></li>
46 *   <li>config override (CONTEXT_DEFAULT): <var>CirrusSearchRescoreProfile</var></li>
47 *   <li>config override (CONTEXT_PREFIXSEARCH): <var>CirrusSearchPrefixSearchRescoreProfile</var></li>
48 *   <li>uri param override (CONTEXT_PREFIXSEARCH & CONTEXT_PREFIXSEARCH): <var>cirrusRescoreProfile</var></li>
49 *  </ul>
50 * </li>
51 * <li>SANEITIZER
52 *  <ul>
53 *   <li>default: <i>no defaults (automatically detected based on wiki size)</i></li>
54 *  </ul>
55 * </li>
56 * <li>SIMILARITY in CONTEXT_DEFAULT
57 *  <ul>
58 *   <li>default: <i>default</i></li>
59 *   <li>config override: <var>wgCirrusSearchSimilarityProfile</var></li>
60 *  </ul>
61 * </li>
62 * <li>FALLBACK in CONTEXT_DEFAULT
63 *  <ul>
64 *      <li>default: <i>none</i></li>
65 *   <li>config override: <var>wgCirrusSearchFallbackProfile</var></li>
66 *  </ul>
67 * </li>
68 * </ul>
69 *
70 * <b>NOTE:</b> extensions may load their own repositories and overriders.
71 */
72class SearchProfileServiceFactory {
73
74    /**
75     * Name of the service declared in MediaWikiServices
76     */
77    public const SERVICE_NAME = self::class;
78
79    /**
80     * Name of the repositories holding profiles
81     * provided by Cirrus.
82     */
83    private const CIRRUS_BASE = 'cirrus_base';
84
85    /**
86     * Name of the repositories holding profiles customized
87     * by wiki admin using CirrusSearch config vars.
88     */
89    private const CIRRUS_CONFIG = 'cirrus_config';
90
91    /**
92     * @var InterwikiResolver
93     */
94    private $interwikiResolver;
95
96    /**
97     * @var SearchConfig
98     */
99    private $hostWikiConfig;
100
101    /**
102     * @var BagOStuff
103     */
104    private $localServerCache;
105
106    /**
107     * @var CirrusSearchHookRunner
108     */
109    private $cirrusSearchHookRunner;
110
111    /**
112     * @var UserOptionsLookup
113     */
114    private $userOptionsLookup;
115
116    public function __construct(
117        InterwikiResolver $resolver,
118        SearchConfig $hostWikiConfig,
119        BagOStuff $localServerCache,
120        CirrusSearchHookRunner $cirrusSearchHookRunner,
121        UserOptionsLookup $userOptionsLookup
122    ) {
123        $this->interwikiResolver = $resolver;
124        $this->hostWikiConfig = $hostWikiConfig;
125        $this->localServerCache = $localServerCache;
126        $this->cirrusSearchHookRunner = $cirrusSearchHookRunner;
127        $this->userOptionsLookup = $userOptionsLookup;
128    }
129
130    /**
131     * @param SearchConfig $config
132     * @param WebRequest|null $request
133     * @param UserIdentity|null $user
134     * @param bool $forceHook force running the hook even if using HashSearchConfig
135     * @return SearchProfileService
136     * @throws \Exception
137     * @throws \FatalError
138     * @throws \MWException
139     */
140    public function loadService( SearchConfig $config, WebRequest $request = null, UserIdentity $user = null, $forceHook = false ) {
141        $service = new SearchProfileService( $this->userOptionsLookup, $request, $user );
142        $this->loadCrossProjectBlockScorer( $service, $config );
143        $this->loadSimilarityProfiles( $service, $config );
144        $this->loadRescoreProfiles( $service, $config );
145        $this->loadCompletionProfiles( $service, $config );
146        $this->loadPhraseSuggesterProfiles( $service, $config );
147        $this->loadIndexLookupFallbackProfiles( $service, $config );
148        $this->loadSaneitizerProfiles( $service );
149        $this->loadDocumentSizeLimiterProfiles( $service, $config );
150        $this->loadFullTextQueryProfiles( $service, $config );
151        $this->loadInterwikiOverrides( $service, $config );
152        $this->loadFallbackProfiles( $service, $config );
153        // Register extension profiles only if running on the host wiki.
154        // Only cirrus search is aware that we are running a crosswiki search
155        // Extensions have no way to know that the profiles they want to declare
156        // may be applied to other wikis. Since they may use host wiki config it seems
157        // safer not to allow extensions to add extra profiles here.
158        // E.g. extension could declare a profile that uses a field that is not available
159        // on the target wiki.
160        if ( $forceHook || $config->isLocalWiki() ) {
161            $this->cirrusSearchHookRunner->onCirrusSearchProfileService( $service );
162        }
163        $service->freeze();
164        return $service;
165    }
166
167    /**
168     * @param SearchProfileService $service
169     * @param SearchConfig $config
170     */
171    private function loadCrossProjectBlockScorer( SearchProfileService $service, SearchConfig $config ) {
172        $service->registerFileRepository( SearchProfileService::CROSS_PROJECT_BLOCK_SCORER,
173            self::CIRRUS_BASE, __DIR__ . '/../../profiles/CrossProjectBlockScorerProfiles.config.php' );
174        $service->registerRepository( new ConfigProfileRepository( SearchProfileService::CROSS_PROJECT_BLOCK_SCORER,
175            self::CIRRUS_CONFIG, 'CirrusSearchCrossProjectBlockScorerProfiles', $config ) );
176        $service->registerDefaultProfile( SearchProfileService::CROSS_PROJECT_BLOCK_SCORER,
177            SearchProfileService::CONTEXT_DEFAULT, 'static' );
178        $service->registerConfigOverride( SearchProfileService::CROSS_PROJECT_BLOCK_SCORER,
179            SearchProfileService::CONTEXT_DEFAULT, $config, 'CirrusSearchCrossProjectOrder' );
180    }
181
182    /**
183     * @param SearchProfileService $service
184     * @param SearchConfig $config
185     */
186    private function loadSimilarityProfiles( SearchProfileService $service, SearchConfig $config ) {
187        $service->registerFileRepository( SearchProfileService::SIMILARITY, self::CIRRUS_BASE,
188            __DIR__ . '/../../profiles/SimilarityProfiles.config.php' );
189        $service->registerRepository( new ConfigProfileRepository( SearchProfileService::SIMILARITY,
190            self::CIRRUS_CONFIG, 'CirrusSearchSimilarityProfiles', $config ) );
191
192        $service->registerDefaultProfile( SearchProfileService::SIMILARITY,
193            SearchProfileService::CONTEXT_DEFAULT, 'bm25_with_defaults' );
194        $service->registerConfigOverride( SearchProfileService::SIMILARITY,
195            SearchProfileService::CONTEXT_DEFAULT, $config, 'CirrusSearchSimilarityProfile' );
196    }
197
198    /**
199     * @param SearchProfileService $service
200     * @param SearchConfig $config
201     */
202    private function loadRescoreProfiles( SearchProfileService $service, SearchConfig $config ) {
203        $service->registerFileRepository( SearchProfileService::RESCORE,
204            self::CIRRUS_BASE, __DIR__ . '/../../profiles/RescoreProfiles.config.php' );
205        $service->registerRepository( new ConfigProfileRepository( SearchProfileService::RESCORE,
206            self::CIRRUS_CONFIG, 'CirrusSearchRescoreProfiles', $config ) );
207        $service->registerDefaultProfile( SearchProfileService::RESCORE,
208            SearchProfileService::CONTEXT_DEFAULT, 'classic' );
209        $service->registerDefaultProfile( SearchProfileService::RESCORE,
210            SearchProfileService::CONTEXT_PREFIXSEARCH, 'classic' );
211
212        $service->registerConfigOverride( SearchProfileService::RESCORE,
213            SearchProfileService::CONTEXT_DEFAULT, $config, 'CirrusSearchRescoreProfile' );
214        $service->registerConfigOverride( SearchProfileService::RESCORE,
215            SearchProfileService::CONTEXT_PREFIXSEARCH, $config, 'CirrusSearchPrefixSearchRescoreProfile' );
216
217        $service->registerUriParamOverride( SearchProfileService::RESCORE,
218            [ SearchProfileService::CONTEXT_DEFAULT, SearchProfileService::CONTEXT_PREFIXSEARCH ],
219            'cirrusRescoreProfile' );
220
221        // function chains
222        $service->registerFileRepository( SearchProfileService::RESCORE_FUNCTION_CHAINS,
223            self::CIRRUS_BASE, __DIR__ . '/../../profiles/RescoreFunctionChains.config.php' );
224        $service->registerRepository( new ConfigProfileRepository( SearchProfileService::RESCORE_FUNCTION_CHAINS,
225            self::CIRRUS_CONFIG, 'CirrusSearchRescoreFunctionScoreChains', $config ) );
226        // No default profiles for function chains, these profiles are always accessed explicitly
227    }
228
229    /**
230     * @param SearchProfileService $service
231     * @param SearchConfig $config
232     */
233    private function loadCompletionProfiles( SearchProfileService $service, SearchConfig $config ) {
234        $service->registerRepository( CompletionSearchProfileRepository::fromFile( SearchProfileService::COMPLETION,
235            self::CIRRUS_BASE, __DIR__ . '/../../profiles/SuggestProfiles.config.php', $config ) );
236        $service->registerRepository( CompletionSearchProfileRepository::fromConfig( SearchProfileService::COMPLETION,
237            self::CIRRUS_CONFIG, 'CirrusSearchCompletionProfiles', $config ) );
238        $service->registerDefaultProfile( SearchProfileService::COMPLETION,
239            SearchProfileService::CONTEXT_DEFAULT, 'fuzzy' );
240        // XXX: We don't really override the default here
241        // Due to the way User preference works we may always end up using
242        // the user pref overrides because we initialize default user pref
243        // in Hooks::onUserGetDefaultOptions
244        $service->registerConfigOverride( SearchProfileService::COMPLETION,
245            SearchProfileService::CONTEXT_DEFAULT, $config, 'CirrusSearchCompletionSettings' );
246        $service->registerUserPrefOverride( SearchProfileService::COMPLETION,
247            SearchProfileService::CONTEXT_DEFAULT, 'cirrussearch-pref-completion-profile' );
248    }
249
250    /**
251     * @param SearchProfileService $service
252     * @param SearchConfig $config
253     */
254    private function loadPhraseSuggesterProfiles( SearchProfileService $service, SearchConfig $config ) {
255        $service->registerRepository( PhraseSuggesterProfileRepoWrapper::fromFile( SearchProfileService::PHRASE_SUGGESTER,
256            self::CIRRUS_BASE, __DIR__ . '/../../profiles/PhraseSuggesterProfiles.config.php', $this->localServerCache ) );
257
258        $service->registerRepository( PhraseSuggesterProfileRepoWrapper::fromConfig( SearchProfileService::PHRASE_SUGGESTER,
259            self::CIRRUS_CONFIG, 'CirrusSearchPhraseSuggestProfiles', $config, $this->localServerCache ) );
260    }
261
262    private function loadIndexLookupFallbackProfiles( SearchProfileService $service, SearchConfig $config ) {
263        $service->registerFileRepository( SearchProfileService::INDEX_LOOKUP_FALLBACK,
264            self::CIRRUS_BASE, __DIR__ . '/../../profiles/IndexLookupFallbackProfiles.config.php' );
265
266        $service->registerRepository( new ConfigProfileRepository( SearchProfileService::INDEX_LOOKUP_FALLBACK,
267            self::CIRRUS_CONFIG, 'CirrusSearchIndexLookupFallbackProfiles', $config ) );
268    }
269
270    /**
271     * @param SearchProfileService $service
272     */
273    private function loadSaneitizerProfiles( SearchProfileService $service ) {
274        $service->registerFileRepository( SearchProfileService::SANEITIZER, self::CIRRUS_BASE,
275            __DIR__ . '/../../profiles/SaneitizeProfiles.config.php' );
276        // no name resolver, profile is automatically chosen based on wiki
277    }
278
279    /**
280     * @param SearchProfileService $service
281     * @param SearchConfig $config
282     */
283    private function loadDocumentSizeLimiterProfiles( SearchProfileService $service, SearchConfig $config ) {
284        $service->registerFileRepository( SearchProfileService::DOCUMENT_SIZE_LIMITER, self::CIRRUS_BASE,
285            __DIR__ . '/../../profiles/DocumentSizeLimiterProfiles.config.php' );
286        $service->registerRepository( new ConfigProfileRepository( SearchProfileService::DOCUMENT_SIZE_LIMITER,
287            self::CIRRUS_CONFIG, 'CirrusSearchDocumentSizeLimiterProfiles', $config ) );
288        $service->registerDefaultProfile( SearchProfileService::DOCUMENT_SIZE_LIMITER,
289            SearchProfileService::CONTEXT_DEFAULT, "default" );
290        $service->registerConfigOverride( SearchProfileService::DOCUMENT_SIZE_LIMITER,
291            SearchProfileService::CONTEXT_DEFAULT, $config, "CirrusSearchDocumentSizeLimiterProfile" );
292    }
293
294    /**
295     * @param SearchProfileService $service
296     * @param SearchConfig $config
297     */
298    private function loadFullTextQueryProfiles( SearchProfileService $service, SearchConfig $config ) {
299        $service->registerFileRepository( SearchProfileService::FT_QUERY_BUILDER, self::CIRRUS_BASE,
300            __DIR__ . '/../../profiles/FullTextQueryBuilderProfiles.config.php' );
301
302        $service->registerRepository( new ConfigProfileRepository( SearchProfileService::FT_QUERY_BUILDER, self::CIRRUS_CONFIG,
303            'CirrusSearchFullTextQueryBuilderProfiles', $config ) );
304
305        $service->registerDefaultProfile( SearchProfileService::FT_QUERY_BUILDER,
306            SearchProfileService::CONTEXT_DEFAULT, 'default' );
307        $service->registerConfigOverride( SearchProfileService::FT_QUERY_BUILDER,
308            SearchProfileService::CONTEXT_DEFAULT, $config, 'CirrusSearchFullTextQueryBuilderProfile' );
309        $service->registerUriParamOverride( SearchProfileService::FT_QUERY_BUILDER,
310            SearchProfileService::CONTEXT_DEFAULT, 'cirrusFTQBProfile' );
311    }
312
313    /**
314     * If the host wiki defines profiles in CirrusSearchCrossProjectProfiles
315     * we inject the defaults into the target wiki profile service using a static overrider
316     * with a prio that just supersedes the config default.
317     *
318     * Only rescore et ftbuilder are supported so far.
319     *
320     * @param SearchProfileService $service
321     * @param SearchConfig $config
322     */
323    private function loadInterwikiOverrides( SearchProfileService $service, SearchConfig $config ) {
324        if ( $config->isLocalWiki() || $config === $this->hostWikiConfig ) {
325            return;
326        }
327        $iwPrefix = $this->interwikiResolver->getInterwikiPrefix( $config->getWikiId() );
328        if ( $iwPrefix === null ) {
329            return;
330        }
331        $profiles = $this->hostWikiConfig->getElement( 'CirrusSearchCrossProjectProfiles',  $iwPrefix );
332        if ( $profiles === null || !is_array( $profiles ) || $profiles === [] ) {
333            return;
334        }
335        if ( isset( $profiles['rescore'] ) ) {
336            $service->registerProfileOverride( SearchProfileService::RESCORE,
337                SearchProfileService::CONTEXT_DEFAULT,
338                new StaticProfileOverride( $profiles['rescore'], SearchProfileOverride::CONFIG_PRIO - 1 ) );
339        }
340
341        if ( isset( $profiles['ftbuilder'] ) ) {
342            $service->registerProfileOverride( SearchProfileService::FT_QUERY_BUILDER,
343                SearchProfileService::CONTEXT_DEFAULT,
344                new StaticProfileOverride( $profiles['ftbuilder'], SearchProfileOverride::CONFIG_PRIO - 1 ) );
345        }
346    }
347
348    private function loadFallbackProfiles( SearchProfileService $service, SearchConfig $config ) {
349        $service->registerFileRepository( SearchProfileService::FALLBACKS, self::CIRRUS_BASE,
350            __DIR__ . '/../../profiles/FallbackProfiles.config.php' );
351        $service->registerRepository( new ConfigProfileRepository( SearchProfileService::FALLBACKS, self::CIRRUS_CONFIG,
352            'CirrusSearchFallbackProfiles', $config ) );
353
354        $service->registerDefaultProfile( SearchProfileService::FALLBACKS,
355            SearchProfileService::CONTEXT_DEFAULT, 'none' );
356        $service->registerConfigOverride( SearchProfileService::FALLBACKS,
357            SearchProfileService::CONTEXT_DEFAULT, $config, 'CirrusSearchFallbackProfile' );
358        $service->registerUriParamOverride( SearchProfileService::FALLBACKS,
359            SearchProfileService::CONTEXT_DEFAULT, 'cirrusFallbackProfile' );
360    }
361}