MediaWiki  master
SearchEngineFactory.php
Go to the documentation of this file.
1 <?php
2 
4 use Wikimedia\ObjectFactory\ObjectFactory;
7 
17  private $config;
18 
20  private $hookContainer;
21 
23  private $loadBalancer;
24 
30  public function __construct(
31  SearchEngineConfig $config,
32  HookContainer $hookContainer,
33  ILoadBalancer $loadBalancer
34  ) {
35  $this->config = $config;
36  $this->hookContainer = $hookContainer;
37  $this->loadBalancer = $loadBalancer;
38  }
39 
46  public function create( $type = null ) {
47  $configuredClass = $this->config->getSearchType();
48  $alternativesClasses = $this->config->getSearchTypes();
49 
50  if ( $type !== null && in_array( $type, $alternativesClasses ) ) {
51  $class = $type;
52  } elseif ( $configuredClass !== null ) {
53  $class = $configuredClass;
54  } else {
55  $class = self::getSearchEngineClass( $this->loadBalancer );
56  }
57 
58  $mappings = $this->config->getSearchMappings();
59 
60  // Convert non mapped classes to ObjectFactory spec
61  $spec = $mappings[$class] ?? [ 'class' => $class ];
62 
63  $args = [];
64 
65  if ( isset( $spec['class'] ) && is_subclass_of( $spec['class'], SearchDatabase::class ) ) {
66  $args['extraArgs'][] = $this->loadBalancer;
67  }
68 
69  // ObjectFactory::getObjectFromSpec accepts an array, not just a callable (phan bug)
70  // @phan-suppress-next-line PhanTypeInvalidCallableArraySize
71  $engine = ObjectFactory::getObjectFromSpec( $spec, $args );
73  $engine->setHookContainer( $this->hookContainer );
74  return $engine;
75  }
76 
82  public static function getSearchEngineClass( $dbOrLb ) {
83  $type = ( $dbOrLb instanceof IDatabase )
84  ? $dbOrLb->getType()
85  : $dbOrLb->getServerType( $dbOrLb->getWriterIndex() );
86 
87  switch ( $type ) {
88  case 'sqlite':
89  return SearchSqlite::class;
90  case 'mysql':
91  return SearchMySQL::class;
92  case 'postgres':
93  return SearchPostgres::class;
94  default:
95  return SearchEngineDummy::class;
96  }
97  }
98 }
Configuration handling class for SearchEngine.
Factory class for SearchEngine.
__construct(SearchEngineConfig $config, HookContainer $hookContainer, ILoadBalancer $loadBalancer)
create( $type=null)
Create SearchEngine of the given type.
static getSearchEngineClass( $dbOrLb)
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
Create and track the database connections and transactions for a given database cluster.
if( $line===false) $args
Definition: mcc.php:124