2013-04-02 22:01:23 +02:00
|
|
|
<?php
|
2024-05-23 09:26:56 +02:00
|
|
|
|
2015-03-26 11:44:34 +01:00
|
|
|
/**
|
2024-05-23 09:26:56 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-03-26 11:44:34 +01:00
|
|
|
*/
|
2013-04-02 22:01:23 +02:00
|
|
|
namespace OC\Setup;
|
|
|
|
|
2021-01-03 15:28:31 +01:00
|
|
|
use OC\DB\Connection;
|
2016-07-12 13:11:44 +02:00
|
|
|
use OC\DB\ConnectionFactory;
|
2017-06-01 16:56:34 +02:00
|
|
|
use OC\DB\MigrationService;
|
2017-03-17 16:37:48 -06:00
|
|
|
use OC\SystemConfig;
|
2016-10-28 21:46:28 +02:00
|
|
|
use OCP\IL10N;
|
2023-10-31 12:06:09 +01:00
|
|
|
use OCP\Migration\IOutput;
|
2015-07-30 00:04:30 +02:00
|
|
|
use OCP\Security\ISecureRandom;
|
2021-04-16 14:26:43 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-07-29 18:09:23 +02:00
|
|
|
|
2013-04-02 22:01:23 +02:00
|
|
|
abstract class AbstractDatabase {
|
2016-10-28 21:46:28 +02:00
|
|
|
/** @var IL10N */
|
2013-04-02 22:01:23 +02:00
|
|
|
protected $trans;
|
2015-07-29 18:09:23 +02:00
|
|
|
/** @var string */
|
|
|
|
protected $dbUser;
|
|
|
|
/** @var string */
|
|
|
|
protected $dbPassword;
|
|
|
|
/** @var string */
|
|
|
|
protected $dbName;
|
|
|
|
/** @var string */
|
|
|
|
protected $dbHost;
|
|
|
|
/** @var string */
|
2016-07-06 09:58:38 +02:00
|
|
|
protected $dbPort;
|
|
|
|
/** @var string */
|
2015-07-29 18:09:23 +02:00
|
|
|
protected $tablePrefix;
|
2017-03-17 16:37:48 -06:00
|
|
|
/** @var SystemConfig */
|
2015-07-29 18:09:23 +02:00
|
|
|
protected $config;
|
2021-04-16 14:26:43 +02:00
|
|
|
/** @var LoggerInterface */
|
2015-07-30 00:04:30 +02:00
|
|
|
protected $logger;
|
|
|
|
/** @var ISecureRandom */
|
|
|
|
protected $random;
|
2023-01-29 15:54:39 +01:00
|
|
|
/** @var bool */
|
|
|
|
protected $tryCreateDbUser;
|
2013-04-02 22:01:23 +02:00
|
|
|
|
2021-04-16 14:26:43 +02:00
|
|
|
public function __construct(IL10N $trans, SystemConfig $config, LoggerInterface $logger, ISecureRandom $random) {
|
2013-04-02 22:01:23 +02:00
|
|
|
$this->trans = $trans;
|
2015-07-29 18:09:23 +02:00
|
|
|
$this->config = $config;
|
2015-07-30 00:04:30 +02:00
|
|
|
$this->logger = $logger;
|
|
|
|
$this->random = $random;
|
2013-04-03 17:52:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function validate($config) {
|
2020-03-26 09:30:18 +01:00
|
|
|
$errors = [];
|
2016-03-14 21:30:59 +05:30
|
|
|
if (empty($config['dbuser']) && empty($config['dbname'])) {
|
2024-02-13 14:37:09 +01:00
|
|
|
$errors[] = $this->trans->t('Enter the database Login and name for %s', [$this->dbprettyname]);
|
2020-04-10 10:35:09 +02:00
|
|
|
} elseif (empty($config['dbuser'])) {
|
2024-02-13 14:37:09 +01:00
|
|
|
$errors[] = $this->trans->t('Enter the database Login for %s', [$this->dbprettyname]);
|
2020-04-10 10:35:09 +02:00
|
|
|
} elseif (empty($config['dbname'])) {
|
2022-09-16 09:18:24 +02:00
|
|
|
$errors[] = $this->trans->t('Enter the database name for %s', [$this->dbprettyname]);
|
2013-04-03 17:52:18 +02:00
|
|
|
}
|
|
|
|
if (substr_count($config['dbname'], '.') >= 1) {
|
2022-09-16 09:18:24 +02:00
|
|
|
$errors[] = $this->trans->t('You cannot use dots in the database name %s', [$this->dbprettyname]);
|
2013-04-03 17:52:18 +02:00
|
|
|
}
|
|
|
|
return $errors;
|
2013-04-02 22:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function initialize($config) {
|
2015-01-26 12:59:25 +01:00
|
|
|
$dbUser = $config['dbuser'];
|
|
|
|
$dbPass = $config['dbpass'];
|
|
|
|
$dbName = $config['dbname'];
|
|
|
|
$dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost';
|
2016-07-06 09:58:38 +02:00
|
|
|
$dbPort = !empty($config['dbport']) ? $config['dbport'] : '';
|
2023-07-07 13:43:21 +03:30
|
|
|
$dbTablePrefix = $config['dbtableprefix'] ?? 'oc_';
|
2013-04-02 22:01:23 +02:00
|
|
|
|
2023-01-29 15:54:39 +01:00
|
|
|
$createUserConfig = $this->config->getValue('setup_create_db_user', true);
|
|
|
|
// accept `false` both as bool and string, since setting config values from env will result in a string
|
|
|
|
$this->tryCreateDbUser = $createUserConfig !== false && $createUserConfig !== 'false';
|
|
|
|
|
2017-03-17 16:37:48 -06:00
|
|
|
$this->config->setValues([
|
2020-10-05 15:12:57 +02:00
|
|
|
'dbname' => $dbName,
|
|
|
|
'dbhost' => $dbHost,
|
2016-07-06 09:58:38 +02:00
|
|
|
'dbport' => $dbPort,
|
2020-10-05 15:12:57 +02:00
|
|
|
'dbtableprefix' => $dbTablePrefix,
|
2015-01-23 11:13:47 +01:00
|
|
|
]);
|
2013-04-02 22:01:23 +02:00
|
|
|
|
2015-07-29 18:09:23 +02:00
|
|
|
$this->dbUser = $dbUser;
|
|
|
|
$this->dbPassword = $dbPass;
|
|
|
|
$this->dbName = $dbName;
|
|
|
|
$this->dbHost = $dbHost;
|
2016-07-06 09:58:38 +02:00
|
|
|
$this->dbPort = $dbPort;
|
2015-07-29 18:09:23 +02:00
|
|
|
$this->tablePrefix = $dbTablePrefix;
|
2013-04-02 22:01:23 +02:00
|
|
|
}
|
2015-01-26 12:59:25 +01:00
|
|
|
|
2016-07-12 13:11:44 +02:00
|
|
|
/**
|
2016-07-12 13:50:54 +02:00
|
|
|
* @param array $configOverwrite
|
2016-07-12 13:11:44 +02:00
|
|
|
* @return \OC\DB\Connection
|
|
|
|
*/
|
2021-01-03 15:28:31 +01:00
|
|
|
protected function connect(array $configOverwrite = []): Connection {
|
2020-03-26 09:30:18 +01:00
|
|
|
$connectionParams = [
|
2016-07-26 11:44:15 +02:00
|
|
|
'host' => $this->dbHost,
|
|
|
|
'user' => $this->dbUser,
|
|
|
|
'password' => $this->dbPassword,
|
|
|
|
'tablePrefix' => $this->tablePrefix,
|
2016-07-21 12:44:02 +02:00
|
|
|
'dbname' => $this->dbName
|
2020-03-26 09:30:18 +01:00
|
|
|
];
|
2016-07-26 11:44:15 +02:00
|
|
|
|
|
|
|
// adding port support through installer
|
|
|
|
if (!empty($this->dbPort)) {
|
|
|
|
if (ctype_digit($this->dbPort)) {
|
|
|
|
$connectionParams['port'] = $this->dbPort;
|
|
|
|
} else {
|
|
|
|
$connectionParams['unix_socket'] = $this->dbPort;
|
|
|
|
}
|
2020-04-10 10:35:09 +02:00
|
|
|
} elseif (strpos($this->dbHost, ':')) {
|
2016-07-26 11:44:15 +02:00
|
|
|
// Host variable may carry a port or socket.
|
2021-01-12 09:15:48 +00:00
|
|
|
[$host, $portOrSocket] = explode(':', $this->dbHost, 2);
|
2016-07-26 11:44:15 +02:00
|
|
|
if (ctype_digit($portOrSocket)) {
|
|
|
|
$connectionParams['port'] = $portOrSocket;
|
|
|
|
} else {
|
|
|
|
$connectionParams['unix_socket'] = $portOrSocket;
|
|
|
|
}
|
|
|
|
$connectionParams['host'] = $host;
|
2016-07-12 13:11:44 +02:00
|
|
|
}
|
2023-12-28 11:45:58 +01:00
|
|
|
$connectionParams = array_merge($connectionParams, $configOverwrite);
|
|
|
|
$connectionParams = array_merge($connectionParams, ['primary' => $connectionParams, 'replica' => [$connectionParams]]);
|
2016-02-09 17:25:12 +01:00
|
|
|
$cf = new ConnectionFactory($this->config);
|
2023-12-28 11:45:58 +01:00
|
|
|
$connection = $cf->getConnection($this->config->getValue('dbtype', 'sqlite'), $connectionParams);
|
|
|
|
$connection->ensureConnectedToPrimary();
|
|
|
|
return $connection;
|
2016-07-12 13:11:44 +02:00
|
|
|
}
|
|
|
|
|
2016-02-08 15:43:39 +00:00
|
|
|
/**
|
2022-05-18 21:48:58 +02:00
|
|
|
* @param string $username
|
2016-02-08 15:43:39 +00:00
|
|
|
*/
|
2022-05-18 21:48:58 +02:00
|
|
|
abstract public function setupDatabase($username);
|
2017-06-01 16:56:34 +02:00
|
|
|
|
2023-10-31 12:06:09 +01:00
|
|
|
public function runMigrations(?IOutput $output = null) {
|
2017-06-01 16:56:34 +02:00
|
|
|
if (!is_dir(\OC::$SERVERROOT . '/core/Migrations')) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-31 12:06:09 +01:00
|
|
|
$ms = new MigrationService('core', \OC::$server->get(Connection::class), $output);
|
2020-11-11 20:12:13 +01:00
|
|
|
$ms->migrate('latest', true);
|
2017-06-01 16:56:34 +02:00
|
|
|
}
|
2013-04-02 22:01:23 +02:00
|
|
|
}
|