Merge "Add tablesUsed to RevisionStoreDbTest"
[lhc/web/wiklou.git] / includes / search / SearchEngineFactory.php
1 <?php
2
3 use Wikimedia\Rdbms\IDatabase;
4
5 /**
6 * Factory class for SearchEngine.
7 * Allows to create engine of the specific type.
8 */
9 class SearchEngineFactory {
10 /**
11 * Configuration for SearchEngine classes.
12 * @var SearchEngineConfig
13 */
14 private $config;
15
16 public function __construct( SearchEngineConfig $config ) {
17 $this->config = $config;
18 }
19
20 /**
21 * Create SearchEngine of the given type.
22 * @param string $type
23 * @return SearchEngine
24 */
25 public function create( $type = null ) {
26 $dbr = null;
27
28 $configType = $this->config->getSearchType();
29 $alternatives = $this->config->getSearchTypes();
30
31 if ( $type && in_array( $type, $alternatives ) ) {
32 $class = $type;
33 } elseif ( $configType !== null ) {
34 $class = $configType;
35 } else {
36 $dbr = wfGetDB( DB_REPLICA );
37 $class = self::getSearchEngineClass( $dbr );
38 }
39
40 $search = new $class( $dbr );
41 return $search;
42 }
43
44 /**
45 * @param IDatabase $db
46 * @return string SearchEngine subclass name
47 * @since 1.28
48 */
49 public static function getSearchEngineClass( IDatabase $db ) {
50 switch ( $db->getType() ) {
51 case 'sqlite':
52 return SearchSqlite::class;
53 case 'mysql':
54 return SearchMySQL::class;
55 case 'postgres':
56 return SearchPostgres::class;
57 case 'mssql':
58 return SearchMssql::class;
59 case 'oracle':
60 return SearchOracle::class;
61 default:
62 return SearchEngineDummy::class;
63 }
64 }
65 }