3 namespace Friendica\Factory;
5 use Friendica\Core\Config\Cache;
6 use Friendica\Database;
10 public static function init(Cache\ConfigCache $configCache, array $server)
12 if (Database\DBA::connected()) {
16 $db_host = $configCache->get('database', 'hostname');
17 $db_user = $configCache->get('database', 'username');
18 $db_pass = $configCache->get('database', 'password');
19 $db_data = $configCache->get('database', 'database');
20 $charset = $configCache->get('database', 'charset');
22 // Use environment variables for mysql if they are set beforehand
23 if (!empty($server['MYSQL_HOST'])
24 && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
25 && $server['MYSQL_PASSWORD'] !== false
26 && !empty($server['MYSQL_DATABASE']))
28 $db_host = $server['MYSQL_HOST'];
29 if (!empty($server['MYSQL_PORT'])) {
30 $db_host .= ':' . $server['MYSQL_PORT'];
32 if (!empty($server['MYSQL_USERNAME'])) {
33 $db_user = $server['MYSQL_USERNAME'];
35 $db_user = $server['MYSQL_USER'];
37 $db_pass = (string) $server['MYSQL_PASSWORD'];
38 $db_data = $server['MYSQL_DATABASE'];
41 if (Database\DBA::connect($configCache, $db_host, $db_user, $db_pass, $db_data, $charset)) {
42 // Loads DB_UPDATE_VERSION constant
43 Database\DBStructure::definition($configCache->get('system', 'basepath'), false);
46 unset($db_host, $db_user, $db_pass, $db_data, $charset);