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