MediaWiki master
SearchEngineFactory.php
Go to the documentation of this file.
1<?php
2
4use Wikimedia\ObjectFactory\ObjectFactory;
6
16 private $config;
17
19 private $hookContainer;
20
21 private IConnectionProvider $dbProvider;
22
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;
36 }
37
44 public function create( $type = null ) {
45 $configuredClass = $this->config->getSearchType();
46 $alternativesClasses = $this->config->getSearchTypes();
47
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 );
54 }
55
56 $mappings = $this->config->getSearchMappings();
57
58 // Convert non mapped classes to ObjectFactory spec
59 $spec = $mappings[$class] ?? [ 'class' => $class ];
60
61 $args = [];
62
63 if ( isset( $spec['class'] ) && is_subclass_of( $spec['class'], SearchDatabase::class ) ) {
64 $args['extraArgs'][] = $this->dbProvider;
65 }
66
67 // ObjectFactory::getObjectFromSpec accepts an array, not just a callable (phan bug)
68 // @phan-suppress-next-line PhanTypeInvalidCallableArraySize
69 $engine = ObjectFactory::getObjectFromSpec( $spec, $args );
71 $engine->setHookContainer( $this->hookContainer );
72 return $engine;
73 }
74
80 public static function getSearchEngineClass( IConnectionProvider $dbProvider ) {
81 $type = $dbProvider->getReplicaDatabase()->getType();
82
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;
92 }
93 }
94}
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.