From b30636c313f852844034fbfd8963d7ab6c6deb9d Mon Sep 17 00:00:00 2001 From: Peter Kokot Date: Wed, 14 Nov 2018 16:25:23 +0100 Subject: [PATCH] Partially refactor database configuration This patch refactors database handling in certain development scripts --- .env.dist | 14 +++++++++++++- bin/cleanup-user.php | 9 +++++++-- bin/drop-unused-tables.php | 3 ++- bin/update-karma.php | 3 ++- bin/update-vcs-link.php | 3 ++- config/app.php | 15 +++++++++++++++ config/app_prod.php | 12 ++++++++++++ include/pear-config.php | 1 - 8 files changed, 53 insertions(+), 7 deletions(-) diff --git a/.env.dist b/.env.dist index a1ef776a..b385ed75 100644 --- a/.env.dist +++ b/.env.dist @@ -2,7 +2,7 @@ # the settings in the config directory files. # Application environment -PECL_ENV=dev +PECL_ENV="dev" # http or https PECL_SCHEME="http" @@ -10,5 +10,17 @@ PECL_SCHEME="http" # PECL channel host (domain name) PECL_HOST="pecl.localhost" +# Database username +PECL_DB_USERNAME="nobody" + +# Database password +PECL_DB_PASSWORD="password" + +# Database host +PECL_DB_HOST="localhost" + +# Database name +PECL_DB_NAME="pecl" + # Path where new PECL account requests are sent when requesting a SVN account PECL_MASTER_API_URL="http://pecl.localhost" diff --git a/bin/cleanup-user.php b/bin/cleanup-user.php index 6a2c9bfc..b3f43f95 100755 --- a/bin/cleanup-user.php +++ b/bin/cleanup-user.php @@ -27,12 +27,17 @@ require_once __DIR__.'/../include/bootstrap.php'; $svnusers = '/home/pierre/project/pecl/migration/svnusers'; $svn_accounts = file($svnusers); -function nonl(&$var) {$var = str_replace(["\n","\r", "\r\n"], '', $var);} + +function nonl(&$var) { + $var = str_replace(["\n","\r", "\r\n"], '', $var); +} + array_walk($svn_accounts, 'nonl'); $sql = 'select handle from users where handle NOT IN (select handle from maintains)'; -$dh = new \PDO(PECL_DB_DSN, PECL_DB_USER, PECL_DB_PASSWORD); +$dsn = 'mysql:host='.$config->get('db_host').';dbname='.$config->get('db_name'); +$dh = new \PDO($dsn, $config->get('db_username'), $config->get('db_password')); $res = $dh->query($sql); $sql_del = 'DELETE FROM users WHERE handle='; diff --git a/bin/drop-unused-tables.php b/bin/drop-unused-tables.php index 697221c1..862ef7e3 100755 --- a/bin/drop-unused-tables.php +++ b/bin/drop-unused-tables.php @@ -31,5 +31,6 @@ DROP TABLE IF EXISTS elections, election_votes_single, election_votes_multiple, election_votes_abstain, election_results, election_handle_votes, election_choices, election_account_request, zendinfo, trackbacks, apidoc_queue, tagnames, tag_package_link, `comments`, manual_notes '; -$dh = new \PDO(PECL_DB_DSN, PECL_DB_USER, PECL_DB_PASSWORD); +$dsn = 'mysql:host='.$config->get('db_host').';dbname='.$config->get('db_name'); +$dh = new \PDO($dsn, $config->get('db_username'), $config->get('db_password')); $res = $dh->query($sql); diff --git a/bin/update-karma.php b/bin/update-karma.php index 23328203..9f8112d9 100755 --- a/bin/update-karma.php +++ b/bin/update-karma.php @@ -25,7 +25,8 @@ require_once __DIR__.'/../include/bootstrap.php'; -$dh = new \PDO(PECL_DB_DSN, PECL_DB_USER, PECL_DB_PASSWORD); +$dsn = 'mysql:host='.$config->get('db_host').';dbname='.$config->get('db_name'); +$dh = new \PDO($dsn, $config->get('db_username'), $config->get('db_password')); $sql = "update karma set level='developer' where level='pear.dev' or level='pecl.dev';"; $res = $dh->query($sql); diff --git a/bin/update-vcs-link.php b/bin/update-vcs-link.php index 6bd7b031..4f064a00 100755 --- a/bin/update-vcs-link.php +++ b/bin/update-vcs-link.php @@ -44,7 +44,8 @@ IF (cvs_link REGEXP('cvs.php.net\/cvs.php(.*)'), where package_type='pecl' and cvs_link like '%cvs.php.net%'; "; -$dh = new \PDO(PECL_DB_DSN, PECL_DB_USER, PECL_DB_PASSWORD); +$dsn = 'mysql:host='.$config->get('db_host').';dbname='.$config->get('db_name'); +$dh = new \PDO($dsn, $config->get('db_username'), $config->get('db_password')); $res = $dh->query($sql_movetosvn); if (!$res) { diff --git a/config/app.php b/config/app.php index e9da1aaa..f973d5b3 100644 --- a/config/app.php +++ b/config/app.php @@ -34,6 +34,21 @@ return [ // PECL channel URL host (domain name) 'host' => isset($_SERVER['PECL_HOST']) ? $_SERVER['PECL_HOST'] : 'pecl.php.net', + // Database username + 'db_username' => isset($_SERVER['PECL_DB_USERNAME']) ? $_SERVER['PECL_DB_USERNAME'] : 'nobody', + + // Database password + 'db_password' => isset($_SERVER['PECL_DB_PASSWORD']) ? $_SERVER['PECL_DB_PASSWORD'] : 'password', + + // Database name + 'db_name' => isset($_SERVER['PECL_DB_NAME']) ? $_SERVER['PECL_DB_NAME'] : 'pecl', + + // Database host + 'db_host' => isset($_SERVER['PECL_DB_HOST']) ? $_SERVER['PECL_DB_HOST'] : 'localhost', + + // Database driver + 'db_driver' => (function_exists('mysql_connect') ? 'mysql' : (function_exists('mysqli_connect') ? 'mysqli' : '')), + // REST static files directory 'rest_dir' => isset($_SERVER['PECL_REST_DIR']) ? $_SERVER['PECL_REST_DIR'] : __DIR__.'/../public_html/rest', diff --git a/config/app_prod.php b/config/app_prod.php index f4e2312d..b7531cf5 100644 --- a/config/app_prod.php +++ b/config/app_prod.php @@ -25,6 +25,18 @@ */ return [ + // Database username + 'db_username' => isset($_SERVER['PECL_DB_USERNAME']) ? $_SERVER['PECL_DB_USERNAME'] : 'pear', + + // Database password + 'db_password' => isset($_SERVER['PECL_DB_PASSWORD']) ? $_SERVER['PECL_DB_PASSWORD'] : 'pear', + + // Database name + 'db_name' => isset($_SERVER['PECL_DB_NAME']) ? $_SERVER['PECL_DB_NAME'] : 'pear', + + // Database host + 'db_host' => isset($_SERVER['PECL_DB_HOST']) ? $_SERVER['PECL_DB_HOST'] : 'localhost', + // REST static files directory 'rest_dir' => isset($_SERVER['PECL_REST_DIR']) ? $_SERVER['PECL_REST_DIR'] : '/var/lib/peclweb/rest', diff --git a/include/pear-config.php b/include/pear-config.php index bdb8b699..d7cf4977 100644 --- a/include/pear-config.php +++ b/include/pear-config.php @@ -38,5 +38,4 @@ if (isset($_SERVER['PEAR_DATABASE_DSN'])) { $driver = 'mysqli'; } define('PEAR_DATABASE_DSN', $driver . '://' . PECL_DB_USER . ':' . PECL_DB_PASSWORD. '@' . PECL_DB_HOST. '/' . PECL_DB_NAME); - define('PECL_DB_DSN', 'mysql:host=' . PECL_DB_HOST . ';dbname=' . PECL_DB_NAME); }