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 string $basePath The basepath of the application
16 * @param Cache\IConfigCache $configCache The configuration cache
17 * @param Profiler $profiler The profiler
18 * @param array $server The $_SERVER variables
20 * @throws \Exception if connection went bad
22 * @todo refactor basedir during https://github.com/friendica/friendica/issues/6720
24 public static function init($basePath, Cache\IConfigCache $configCache, Profiler $profiler, array $server)
26 if (Database\DBA::connected()) {
30 $db_host = $configCache->get('database', 'hostname');
31 $db_user = $configCache->get('database', 'username');
32 $db_pass = $configCache->get('database', 'password');
33 $db_data = $configCache->get('database', 'database');
34 $charset = $configCache->get('database', 'charset');
36 // Use environment variables for mysql if they are set beforehand
37 if (!empty($server['MYSQL_HOST'])
38 && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
39 && $server['MYSQL_PASSWORD'] !== false
40 && !empty($server['MYSQL_DATABASE']))
42 $db_host = $server['MYSQL_HOST'];
43 if (!empty($server['MYSQL_PORT'])) {
44 $db_host .= ':' . $server['MYSQL_PORT'];
46 if (!empty($server['MYSQL_USERNAME'])) {
47 $db_user = $server['MYSQL_USERNAME'];
49 $db_user = $server['MYSQL_USER'];
51 $db_pass = (string) $server['MYSQL_PASSWORD'];
52 $db_data = $server['MYSQL_DATABASE'];
55 if (Database\DBA::connect($basePath, $configCache, $profiler, new VoidLogger(), $db_host, $db_user, $db_pass, $db_data, $charset)) {
56 // Loads DB_UPDATE_VERSION constant
57 Database\DBStructure::definition($basePath, false);
60 unset($db_host, $db_user, $db_pass, $db_data, $charset);