summaryrefslogtreecommitdiff
path: root/lib/lib_install.php
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2019-09-29 16:22:50 +0200
committerGravatar GitHub <noreply@github.com> 2019-09-29 16:22:50 +0200
commite3e5954394f4523850c78e80e496f1b916622677 (patch)
tree2e20d9091735e1da1de85e273e19635f58111e0f /lib/lib_install.php
parentec4307c1a64a0f60648fdd7d0a2eb819bbf12965 (diff)
PDO refactoring for code simplification (#2522)
* PDO refactor * Automatic prefix when using the syntax `_tableName` * Uniformity: MySQL is now PDO::ATTR_EMULATE_PREPARES = false just like SQLite and PostgreSQL, with consequences such as only one statement per query * Use PDO methods exec(), query(), prepare() + execute() in a more efficient way * Remove auto-update SQL code for versions older than FreshRSS 1.5 (3 years old) * The name of the default category is set in PHP instead of in the DB (simplies SQL and allows changing the name according to the FreshRSS language) * Rename `->bd` to `->pdo` (less of a frenshism, and more informative) * Fix some requests, which were not compatible with MySQL prepared statements * Whitespace * Fix syntax for PostgreSQL sequences + MySQL install * Minor formatting * Fix lastInsertId for PostgreSQL * Use PHP 5.6+ const Take advantage of https://github.com/FreshRSS/FreshRSS/pull/2527 https://www.php.net/manual/en/migration56.new-features.php * A bit of forgotten PHP 5.6 simplification for cURL * Forgotten $s * Mini fix custom user config https://github.com/FreshRSS/FreshRSS/pull/2490/files#r326290346 * More work on install.php but not finished * install.php working * More cleaning of PDO in install * Even more simplification Take advantage of PDO->exec() to run multiple statements * Disallow changing the name of the default category https://github.com/FreshRSS/FreshRSS/pull/2522#discussion_r326967724
Diffstat (limited to 'lib/lib_install.php')
-rw-r--r--lib/lib_install.php79
1 files changed, 17 insertions, 62 deletions
diff --git a/lib/lib_install.php b/lib/lib_install.php
index 17defccf6..ed361eb39 100644
--- a/lib/lib_install.php
+++ b/lib/lib_install.php
@@ -78,69 +78,24 @@ function generateSalt() {
return sha1(uniqid(mt_rand(), true).implode('', stat(__FILE__)));
}
-function checkDb(&$dbOptions) {
- $dsn = '';
- $driver_options = null;
- prepareSyslog();
- try {
- switch ($dbOptions['type']) {
- case 'mysql':
- include_once(APP_PATH . '/SQL/install.sql.mysql.php');
- $driver_options = array(
- PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4'
- );
- try { // on ouvre une connexion juste pour créer la base si elle n'existe pas
- $dsn = 'mysql:host=' . $dbOptions['host'] . ';';
- $c = new PDO($dsn, $dbOptions['user'], $dbOptions['password'], $driver_options);
- $sql = sprintf(SQL_CREATE_DB, $dbOptions['base']);
- $res = $c->query($sql);
- } catch (PDOException $e) {
- syslog(LOG_DEBUG, 'FreshRSS MySQL warning: ' . $e->getMessage());
- }
- // on écrase la précédente connexion en sélectionnant la nouvelle BDD
- $dsn = 'mysql:host=' . $dbOptions['host'] . ';dbname=' . $dbOptions['base'];
- break;
- case 'sqlite':
- include_once(APP_PATH . '/SQL/install.sql.sqlite.php');
- $path = join_path(USERS_PATH, $dbOptions['default_user']);
- if (!is_dir($path)) {
- mkdir($path);
- }
- $dsn = 'sqlite:' . join_path($path, 'db.sqlite');
- $driver_options = array(
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- );
- break;
- case 'pgsql':
- include_once(APP_PATH . '/SQL/install.sql.pgsql.php');
- $driver_options = array(
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- );
- try { // on ouvre une connexion juste pour créer la base si elle n'existe pas
- $dsn = 'pgsql:host=' . $dbOptions['host'] . ';dbname=postgres';
- $c = new PDO($dsn, $dbOptions['user'], $dbOptions['password'], $driver_options);
- $sql = sprintf(SQL_CREATE_DB, $dbOptions['base']);
- $res = $c->query($sql);
- } catch (PDOException $e) {
- syslog(LOG_DEBUG, 'FreshRSS PostgreSQL warning: ' . $e->getMessage());
- }
- // on écrase la précédente connexion en sélectionnant la nouvelle BDD
- $dsn = 'pgsql:host=' . $dbOptions['host'] . ';dbname=' . $dbOptions['base'];
- break;
- default:
- return false;
- }
-
- $c = new PDO($dsn, $dbOptions['user'], $dbOptions['password'], $driver_options);
- $res = $c->query('SELECT 1');
- } catch (PDOException $e) {
- $dsn = '';
- syslog(LOG_DEBUG, 'FreshRSS SQL warning: ' . $e->getMessage());
- $dbOptions['error'] = $e->getMessage();
+function checkDb() {
+ $conf = FreshRSS_Context::$system_conf;
+ $db = $conf->db;
+ if (empty($db['pdo_options'])) {
+ $db['pdo_options'] = [];
}
- $dbOptions['dsn'] = $dsn;
- $dbOptions['options'] = $driver_options;
- return $dsn != '';
+ $db['pdo_options'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
+ $dbBase = isset($db['base']) ? $db['base'] : '';
+
+ $db['base'] = ''; //First connection without database name to create the database
+ Minz_ModelPdo::$usesSharedPdo = false;
+ $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
+ $databaseDAO->create();
+
+ $db['base'] = $dbBase; //New connection with the database name
+ $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
+ Minz_ModelPdo::$usesSharedPdo = true;
+ return $databaseDAO->testConnection();
}
function deleteInstall() {