3 namespace Friendica\Factory;
5 use Friendica\Core\Config\Cache;
6 use Friendica\Database;
7 use Friendica\Util\Profiler;
12 * Initialize the DBA connection
14 * @param string $basePath The basepath of the application
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 * @todo refactor basedir during https://github.com/friendica/friendica/issues/6720
23 public static function init($basePath, Cache\IConfigCache $configCache, Profiler $profiler, array $server)
25 if (Database\DBA::connected()) {
29 $db_host = $configCache->get('database', 'hostname');
30 $db_user = $configCache->get('database', 'username');
31 $db_pass = $configCache->get('database', 'password');
32 $db_data = $configCache->get('database', 'database');
33 $charset = $configCache->get('database', 'charset');
35 // Use environment variables for mysql if they are set beforehand
36 if (!empty($server['MYSQL_HOST'])
37 && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
38 && $server['MYSQL_PASSWORD'] !== false
39 && !empty($server['MYSQL_DATABASE']))
41 $db_host = $server['MYSQL_HOST'];
42 if (!empty($server['MYSQL_PORT'])) {
43 $db_host .= ':' . $server['MYSQL_PORT'];
45 if (!empty($server['MYSQL_USERNAME'])) {
46 $db_user = $server['MYSQL_USERNAME'];
48 $db_user = $server['MYSQL_USER'];
50 $db_pass = (string) $server['MYSQL_PASSWORD'];
51 $db_data = $server['MYSQL_DATABASE'];
54 if (Database\DBA::connect($basePath, $configCache, $profiler, $db_host, $db_user, $db_pass, $db_data, $charset)) {
55 // Loads DB_UPDATE_VERSION constant
56 Database\DBStructure::definition($basePath, false);
59 unset($db_host, $db_user, $db_pass, $db_data, $charset);