3 use MediaWiki\HookContainer\HookContainer
;
4 use Wikimedia\ObjectFactory\ObjectFactory
;
5 use Wikimedia\Rdbms\IConnectionProvider
;
8 * Factory class for SearchEngine.
9 * Allows to create engine of the specific type.
11 class SearchEngineFactory
{
13 * Configuration for SearchEngine classes.
14 * @var SearchEngineConfig
18 /** @var HookContainer */
19 private $hookContainer;
21 private IConnectionProvider
$dbProvider;
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
33 $this->config
= $config;
34 $this->hookContainer
= $hookContainer;
35 $this->dbProvider
= $dbProvider;
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 ) ) {
50 } elseif ( $configuredClass !== null ) {
51 $class = $configuredClass;
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 ];
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
);
76 * @param IConnectionProvider $dbProvider
77 * @return string SearchEngine subclass name
80 public static function getSearchEngineClass( IConnectionProvider
$dbProvider ) {
81 $type = $dbProvider->getReplicaDatabase()->getType();
85 return SearchSqlite
::class;
87 return SearchMySQL
::class;
89 return SearchPostgres
::class;
91 return SearchEngineDummy
::class;