MediaWiki  master
SearchEngineFactory.php
Go to the documentation of this file.
1 <?php
2 
4 use Wikimedia\ObjectFactory;
7 
17  private $config;
18 
20  private $hookContainer;
21 
23  private $loadBalancer;
24 
30  public function __construct(
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  if ( isset( $mappings[$class] ) ) {
61  $spec = $mappings[$class];
62  } else {
63  // Convert non mapped classes to ObjectFactory spec
64  $spec = [ 'class' => $class ];
65  }
66 
67  $args = [];
68 
69  if ( isset( $spec['class'] ) && is_subclass_of( $spec['class'], SearchDatabase::class ) ) {
70  $args['extraArgs'][] = $this->loadBalancer;
71  }
72 
73  // ObjectFactory::getObjectFromSpec accepts an array, not just a callable (phan bug)
74  // @phan-suppress-next-line PhanTypeInvalidCallableArraySize
75  $engine = ObjectFactory::getObjectFromSpec( $spec, $args );
77  $engine->setHookContainer( $this->hookContainer );
78  return $engine;
79  }
80 
86  public static function getSearchEngineClass( $dbOrLb ) {
87  $type = ( $dbOrLb instanceof IDatabase )
88  ? $dbOrLb->getType()
89  : $dbOrLb->getServerType( $dbOrLb->getWriterIndex() );
90 
91  switch ( $type ) {
92  case 'sqlite':
93  return SearchSqlite::class;
94  case 'mysql':
95  return SearchMySQL::class;
96  case 'postgres':
97  return SearchPostgres::class;
98  default:
99  return SearchEngineDummy::class;
100  }
101  }
102 }
SearchEngineFactory\$hookContainer
HookContainer $hookContainer
Definition: SearchEngineFactory.php:20
SearchEngineFactory\$config
SearchEngineConfig $config
Configuration for SearchEngine classes.
Definition: SearchEngineFactory.php:17
SearchEngineFactory
Factory class for SearchEngine.
Definition: SearchEngineFactory.php:12
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
SearchEngineFactory\getSearchEngineClass
static getSearchEngineClass( $dbOrLb)
Definition: SearchEngineFactory.php:86
$args
if( $line===false) $args
Definition: mcc.php:124
SearchEngineFactory\__construct
__construct(SearchEngineConfig $config, HookContainer $hookContainer, ILoadBalancer $loadBalancer)
Definition: SearchEngineFactory.php:30
SearchEngineConfig
Configuration handling class for SearchEngine.
Definition: SearchEngineConfig.php:12
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
SearchEngineFactory\create
create( $type=null)
Create SearchEngine of the given type.
Definition: SearchEngineFactory.php:46
SearchEngineFactory\$loadBalancer
ILoadBalancer $loadBalancer
Definition: SearchEngineFactory.php:23
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
$type
$type
Definition: testCompression.php:52