MediaWiki master
SearchEngineFactory.php
Go to the documentation of this file.
1<?php
2
4use Wikimedia\ObjectFactory\ObjectFactory;
6
12
13 private SearchEngineConfig $config;
14 private HookContainer $hookContainer;
15 private IConnectionProvider $dbProvider;
16
17 public function __construct(
18 SearchEngineConfig $config,
19 HookContainer $hookContainer,
20 IConnectionProvider $dbProvider
21 ) {
22 $this->config = $config;
23 $this->hookContainer = $hookContainer;
24 $this->dbProvider = $dbProvider;
25 }
26
33 public function create( $type = null ) {
34 $configuredClass = $this->config->getSearchType();
35 $alternativesClasses = $this->config->getSearchTypes();
36
37 if ( $type !== null && in_array( $type, $alternativesClasses ) ) {
38 $class = $type;
39 } elseif ( $configuredClass !== null ) {
40 $class = $configuredClass;
41 } else {
42 $class = self::getSearchEngineClass( $this->dbProvider );
43 }
44
45 $mappings = $this->config->getSearchMappings();
46
47 // Convert non mapped classes to ObjectFactory spec
48 $spec = $mappings[$class] ?? [ 'class' => $class ];
49
50 $args = [];
51
52 if ( isset( $spec['class'] ) && is_subclass_of( $spec['class'], SearchDatabase::class ) ) {
53 $args['extraArgs'][] = $this->dbProvider;
54 }
55
56 // ObjectFactory::getObjectFromSpec accepts an array, not just a callable (phan bug)
57 // @phan-suppress-next-line PhanTypeInvalidCallableArraySize
58 $engine = ObjectFactory::getObjectFromSpec( $spec, $args );
60 $engine->setHookContainer( $this->hookContainer );
61 return $engine;
62 }
63
69 public static function getSearchEngineClass( IConnectionProvider $dbProvider ) {
70 $type = $dbProvider->getReplicaDatabase()->getType();
71
72 switch ( $type ) {
73 case 'sqlite':
74 return SearchSqlite::class;
75 case 'mysql':
76 return SearchMySQL::class;
77 case 'postgres':
78 return SearchPostgres::class;
79 default:
80 return SearchEngineDummy::class;
81 }
82 }
83}
Configuration handling class for SearchEngine.
Factory class for SearchEngine.
create( $type=null)
Create SearchEngine of the given type.
__construct(SearchEngineConfig $config, HookContainer $hookContainer, IConnectionProvider $dbProvider)
static getSearchEngineClass(IConnectionProvider $dbProvider)
Provide primary and replica IDatabase connections.
getReplicaDatabase( $domain=false, $group=null)
Get connection to a replica database.