Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / search / SearchEngineFactory.php
blob06b7f2e13f80d6f448254b7f8b344de94a7e26da
1 <?php
3 use MediaWiki\HookContainer\HookContainer;
4 use Wikimedia\ObjectFactory\ObjectFactory;
5 use Wikimedia\Rdbms\IConnectionProvider;
7 /**
8 * Factory class for SearchEngine.
9 * Allows to create engine of the specific type.
11 class SearchEngineFactory {
12 /**
13 * Configuration for SearchEngine classes.
14 * @var SearchEngineConfig
16 private $config;
18 /** @var HookContainer */
19 private $hookContainer;
21 private IConnectionProvider $dbProvider;
23 /**
24 * @param SearchEngineConfig $config
25 * @param HookContainer $hookContainer
26 * @param IConnectionProvider $dbProvider
28 public function __construct(
29 SearchEngineConfig $config,
30 HookContainer $hookContainer,
31 IConnectionProvider $dbProvider
32 ) {
33 $this->config = $config;
34 $this->hookContainer = $hookContainer;
35 $this->dbProvider = $dbProvider;
38 /**
39 * Create SearchEngine of the given type.
41 * @param string|null $type
42 * @return SearchEngine
44 public function create( $type = null ) {
45 $configuredClass = $this->config->getSearchType();
46 $alternativesClasses = $this->config->getSearchTypes();
48 if ( $type !== null && in_array( $type, $alternativesClasses ) ) {
49 $class = $type;
50 } elseif ( $configuredClass !== null ) {
51 $class = $configuredClass;
52 } else {
53 $class = self::getSearchEngineClass( $this->dbProvider );
56 $mappings = $this->config->getSearchMappings();
58 // Convert non mapped classes to ObjectFactory spec
59 $spec = $mappings[$class] ?? [ 'class' => $class ];
61 $args = [];
63 if ( isset( $spec['class'] ) && is_subclass_of( $spec['class'], SearchDatabase::class ) ) {
64 $args['extraArgs'][] = $this->dbProvider;
67 // ObjectFactory::getObjectFromSpec accepts an array, not just a callable (phan bug)
68 // @phan-suppress-next-line PhanTypeInvalidCallableArraySize
69 $engine = ObjectFactory::getObjectFromSpec( $spec, $args );
70 /** @var SearchEngine $engine */
71 $engine->setHookContainer( $this->hookContainer );
72 return $engine;
75 /**
76 * @param IConnectionProvider $dbProvider
77 * @return string SearchEngine subclass name
78 * @since 1.28
80 public static function getSearchEngineClass( IConnectionProvider $dbProvider ) {
81 $type = $dbProvider->getReplicaDatabase()->getType();
83 switch ( $type ) {
84 case 'sqlite':
85 return SearchSqlite::class;
86 case 'mysql':
87 return SearchMySQL::class;
88 case 'postgres':
89 return SearchPostgres::class;
90 default:
91 return SearchEngineDummy::class;