3 namespace Friendica\Factory;
5 use Friendica\Core\Config\Cache;
6 use Friendica\Database;
7 use Friendica\Util\Logger\VoidLogger;
8 use Friendica\Util\Profiler;
13 * Initialize the DBA connection
15 * @param Cache\IConfigCache $configCache The configuration cache
16 * @param Profiler $profiler The profiler
17 * @param array $server The $_SERVER variables
19 * @throws \Exception if connection went bad
21 public static function init(Cache\IConfigCache $configCache, Profiler $profiler, array $server)
23 if (Database\DBA::connected()) {
27 $db_host = $configCache->get('database', 'hostname');
28 $db_user = $configCache->get('database', 'username');
29 $db_pass = $configCache->get('database', 'password');
30 $db_data = $configCache->get('database', 'database');
31 $charset = $configCache->get('database', 'charset');
33 // Use environment variables for mysql if they are set beforehand
34 if (!empty($server['MYSQL_HOST'])
35 && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
36 && $server['MYSQL_PASSWORD'] !== false
37 && !empty($server['MYSQL_DATABASE']))
39 $db_host = $server['MYSQL_HOST'];
40 if (!empty($server['MYSQL_PORT'])) {
41 $db_host .= ':' . $server['MYSQL_PORT'];
43 if (!empty($server['MYSQL_USERNAME'])) {
44 $db_user = $server['MYSQL_USERNAME'];
46 $db_user = $server['MYSQL_USER'];
48 $db_pass = (string) $server['MYSQL_PASSWORD'];
49 $db_data = $server['MYSQL_DATABASE'];
52 if (Database\DBA::connect($configCache, $profiler, new VoidLogger(), $db_host, $db_user, $db_pass, $db_data, $charset)) {
53 // Loads DB_UPDATE_VERSION constant
54 Database\DBStructure::definition($configCache->get('system', 'basepath'), false);
57 unset($db_host, $db_user, $db_pass, $db_data, $charset);