From 0030163d72c21342adca3ee51572fffbb53fcda1 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 21 Sep 2016 22:40:56 +0100 Subject: [PATCH] rename global and file as DBQ --- etc/{dbs.php => dbq.php} | 26 ++++++------ etc/{dbs.yaml => dbq.yaml} | 0 etc/{dbs => dbq}/nb.php | 4 +- etc/{dbs => dbq}/owncloud.php | 2 +- etc/{dbs => dbq}/postfix.php | 2 +- etc/{dbs => dbq}/puppetdb.php | 2 +- etc/{dbs => dbq}/ui.php | 8 ++-- etc/{dbs => dbq}/wp.php | 8 ++-- etc/vim/templates/bash.tt | 1 + lib/php/db.php | 75 +++++++++++++++++++++-------------- lib/php/db/config.php | 16 +++++++- 11 files changed, 87 insertions(+), 57 deletions(-) rename etc/{dbs.php => dbq.php} (77%) rename etc/{dbs.yaml => dbq.yaml} (100%) rename etc/{dbs => dbq}/nb.php (96%) rename etc/{dbs => dbq}/owncloud.php (98%) rename etc/{dbs => dbq}/postfix.php (80%) rename etc/{dbs => dbq}/puppetdb.php (95%) rename etc/{dbs => dbq}/ui.php (99%) rename etc/{dbs => dbq}/wp.php (75%) diff --git a/etc/dbs.php b/etc/dbq.php similarity index 77% rename from etc/dbs.php rename to etc/dbq.php index 799ff544..137d5895 100644 --- a/etc/dbs.php +++ b/etc/dbq.php @@ -1,6 +1,6 @@ $db) { +foreach ($DBQ as $id => $db) { if (1 and !empty($db['type']) and $db['type'] == 'sqlite' and !empty($db['host']) @@ -45,20 +45,20 @@ if ($DIR_SQLITE) { foreach (nb::ls_dir($DIR_SQLITE,'\.db$') as $file) { if (isset($host_already_exists["$DIR_SQLITE/$file"])) { - $DB_CONFS[$host_already_exists["$DIR_SQLITE/$file"]]['sql_pre'][] = 'PRAGMA journal_mode=OFF'; + $DBQ[$host_already_exists["$DIR_SQLITE/$file"]]['sql_pre'][] = 'PRAGMA journal_mode=OFF'; continue; } $name = basename($file,'.db'); $id = "$name-sqlite"; - $DB_CONFS[$id] = [ + $DBQ[$id] = [ 'host' => "$DIR_SQLITE/$file", 'type' => 'sqlite', - 'title' => 'created by dbs.php', + 'title' => 'created by dbq.php', '_import' => $name ]; - #if (!empty($DB_CONFS[$name])) $DB_CONFS[$id] = array_merge_recursive($DB_CONFS[$name],$DB_CONFS[$id]); + #if (!empty($DBQ[$name])) $DBQ[$id] = array_merge_recursive($DBQ[$name],$DBQ[$id]); } } @@ -66,7 +66,7 @@ if ($DIR_SQLITE) { # Sqlite all Attach db type=sqlite # #return; -$DB_CONFS['all'] = [ +$DBQ['all'] = [ 'host' => ':memory:', 'type' => 'sqlite', 'pdo' => '', @@ -82,7 +82,7 @@ $DB_CONFS['all'] = [ ], ]; -foreach ($DB_CONFS as $id => $db) { +foreach ($DBQ as $id => $db) { if (0 or empty($db['type'] ) or $db['type']!='sqlite' @@ -92,11 +92,11 @@ foreach ($DB_CONFS as $id => $db) { or !is_readable($db['host']) ) continue; $fname = basename($db['host'],'.db'); - $DB_CONFS['all']['types']['sql_pre'][] = "ATTACH DATABASE '".$db['host']."' as ".$fname.""; - conf_merge($DB_CONFS[$fname],$DB_CONFS['all']); + $DBQ['all']['types']['sql_pre'][] = "ATTACH DATABASE '".$db['host']."' as ".$fname.""; + conf_merge($DBQ[$fname],$DBQ['all']); } -conf_merge($DB_CONFS['nb'],$DB_CONFS['all']); +conf_merge($DBQ['nb'],$DBQ['all']); return 1; diff --git a/etc/dbs.yaml b/etc/dbq.yaml similarity index 100% rename from etc/dbs.yaml rename to etc/dbq.yaml diff --git a/etc/dbs/nb.php b/etc/dbq/nb.php similarity index 96% rename from etc/dbs/nb.php rename to etc/dbq/nb.php index c6a5a731..42c28662 100644 --- a/etc/dbs/nb.php +++ b/etc/dbq/nb.php @@ -1,6 +1,6 @@ 'admin.izideal.vpn', 'type' => 'mysql', 'name' => 'nb', @@ -64,7 +64,7 @@ $DB_CONFS['nb'] = array( ), ); -if (strpos(php_uname("n"),'ovh.net')!==false) $DB_CONFS['nb']['order'] = 1; +if (strpos(php_uname("n"),'ovh.net')!==false) $DBQ['nb']['order'] = 1; return 1; function tenant_doc(&$r) { return rent_doc('tenant',$r); } diff --git a/etc/dbs/owncloud.php b/etc/dbq/owncloud.php similarity index 98% rename from etc/dbs/owncloud.php rename to etc/dbq/owncloud.php index f1c78170..2435a118 100644 --- a/etc/dbs/owncloud.php +++ b/etc/dbq/owncloud.php @@ -1,5 +1,5 @@ '_nico', 'name' => 'owncloud', 'type' => 'mysql', diff --git a/etc/dbs/postfix.php b/etc/dbq/postfix.php similarity index 80% rename from etc/dbs/postfix.php rename to etc/dbq/postfix.php index 8c10855b..27c86d0c 100644 --- a/etc/dbs/postfix.php +++ b/etc/dbq/postfix.php @@ -1,5 +1,5 @@ 'Postfix on Izideal', 'type' => 'mysql', 'host' => 'admin.izideal.vpn', diff --git a/etc/dbs/puppetdb.php b/etc/dbq/puppetdb.php similarity index 95% rename from etc/dbs/puppetdb.php rename to etc/dbq/puppetdb.php index cfe407b1..90b49e44 100644 --- a/etc/dbs/puppetdb.php +++ b/etc/dbq/puppetdb.php @@ -1,5 +1,5 @@ 'Puppetdb', 'host' => 'big.cascais.loc', 'type' => 'pgsql', diff --git a/etc/dbs/ui.php b/etc/dbq/ui.php similarity index 99% rename from etc/dbs/ui.php rename to etc/dbq/ui.php index eaae442c..e2b1bd54 100644 --- a/etc/dbs/ui.php +++ b/etc/dbq/ui.php @@ -1,5 +1,5 @@ 'Semantico UI', 'type' => 'sqlite', 'host' => (file_exists('/opt/www/sem_ui/var/db/semantico.db') ? '/opt/www/sem_ui/var/db/semantico.db' : "$DIR_SQLITE/semantico.db"), @@ -153,19 +153,19 @@ $DB_CONFS['_ui'] = array( ), ); -$DB_CONFS['ui'] = [ +$DBQ['ui'] = [ '_import' => ['_ui'], 'order' => (preg_match('/^(dev|isp|ist)/',php_uname('n')) ? 1 : null), #'order' => 1, ]; -$DB_CONFS['ui-pgsql'] = [ +$DBQ['ui-pgsql'] = [ 'host' => 'ui.semantico.net', 'type' => 'pgsql', 'name' => 'ui', 'pdo' => '', '_import' => ['_ui','_semadm'], ]; -$DB_CONFS['ui-mysql'] = [ +$DBQ['ui-mysql'] = [ 'host' => 'ui.semantico.net', 'type' => 'mysql', 'name' => 'ui', diff --git a/etc/dbs/wp.php b/etc/dbq/wp.php similarity index 75% rename from etc/dbs/wp.php rename to etc/dbq/wp.php index af776f2c..d084af96 100644 --- a/etc/dbs/wp.php +++ b/etc/dbq/wp.php @@ -1,5 +1,5 @@ 'Wordpress', 'host' => 'admin.izideal.vpn', 'type' => 'mysql', @@ -13,11 +13,11 @@ $DB_CONFS['wp'] = [ ]; if (false and !empty($GLOBALS['wpdb'])) { - $DB_CONFS['wp']['type'] = 'sqlite'; - $DB_CONFS['wp']['host'] = DB_DIR.'/'.DB_FILE; + $DBQ['wp']['type'] = 'sqlite'; + $DBQ['wp']['host'] = DB_DIR.'/'.DB_FILE; } -$DB_CONFS['wp-sqlite'] = array ( +$DBQ['wp-sqlite'] = array ( 'host' => "$DIR_SQLITE/wp.db", 'type' => 'sqlite', '_import' => 'wp', diff --git a/etc/vim/templates/bash.tt b/etc/vim/templates/bash.tt index b9b094f3..dc801ff7 100644 --- a/etc/vim/templates/bash.tt +++ b/etc/vim/templates/bash.tt @@ -92,6 +92,7 @@ verbose() { # ################################################################################# #shopt -s extglob +#${1?"Usage: $0 ARGUMENT"} while [ $# -gt 0 ]; do case "$1" in diff --git a/lib/php/db.php b/lib/php/db.php index b5b6623f..f2fad75e 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -73,7 +73,7 @@ class Db extends nb { # Classes public $out; - public $_no_connect; +# NB 21.09.16 public $_no_connect; function __construct($opt = '') { @@ -149,16 +149,13 @@ class Db extends nb { # Extract args from pdo if (preg_match_all("/[:;](\w+)=([^;]+)/",$this->pdo,$m,PREG_SET_ORDER)) { - #array_shift($m); foreach ($m as $param) { $k = $param[1]; if ($k == 'dbname') $k = 'name'; if (!empty($this->$k)) continue; $this->$k = $param[2]; } - #$this->bye($m); } - #$this->bye($this->pdo); if ($p = $this->p('db.host')) $this->host = $p; @@ -200,6 +197,7 @@ class Db extends nb { if (!empty($this->conn)) return false; # Pdo + if (empty($this->pdo)) $this->bye("db: `".$this->id."`: Missing pdo: check host, name, user, password"); if (empty($this->pdo)) return false; # Type @@ -538,67 +536,85 @@ class Db extends nb { public static function conf_load($files=[],&$first=false) { if (empty($files)) return []; - # Load all files into a $h if #files is not a hash + # + # Load all files into a $dbs if #files is not a hash + # - $h = []; + $dbs = []; if (is_scalar($files)) $files = array($files); foreach ($files as $file) { + + $file = self::untilde($file); + if (!is_readable($file)) continue; if (preg_match('/\.(yaml|yml)$/i',$file) and ($yaml = yaml_parse_file($file))) { - $h = array_replace_recursive($h,$yaml); + $dbs = array_replace_recursive($dbs,$yaml); } elseif (preg_match('/\.php$/i',$file)) { - #global $DB_CONFS; require($file); - if (!empty($DB_CONFS)) $h = array_replace_recursive($h,$DB_CONFS); + if (!empty($DBQ)) $dbs = array_replace_recursive($dbs,$DBQ); } } - unset($yaml,$DB_CONFS); - if (!$h) return false; + unset($yaml,$DBQ); + if (!$dbs) return false; - # Emulate hash pointer _import - foreach ($h as $db=>$o) { + # + # Import database conf with key _import + # + foreach ($dbs as $id=>$params) { - foreach ((array)$o as $k => $v) { + $params = (array)$params; + + # If _default exists spush it to all dbs's _import + if ($id == '_default') { + continue; + } elseif(!empty($dbs['_default'])) { + if (empty($params['_import'])) $params['_import'] = []; + elseif (is_scalar($params['_import'])) $params['_import'] = (array)$params['_import']; + $params['_import'][] = '_default'; + } + + foreach ($params as $k => $v) { if ($k != '_import') continue; $import = is_array($v) ? $v : explode(',',$v); foreach ($import as $v) { - if (empty($h[$v])) continue; + if ($id == $v) this::bye("Infinite loop: _import $id = $v"); + if (empty($dbs[$v])) continue; - foreach ($h[$v] as $kk => $vv) { - if (!isset($o[$kk])) $h[$db][$kk] = $vv; + foreach ($dbs[$v] as $kk => $vv) { + if (!isset($params[$kk])) $dbs[$id][$kk] = $vv; } } - unset($h[$db][$k]); + unset($dbs[$id][$k]); } } # Remove db starting with _ - foreach ($h as $db=>$o) { if (preg_match('/^_/',$db)) unset($h[$db]); } + foreach ($dbs as $db=>$o) { if (preg_match('/^_/',$db)) unset($dbs[$db]); } # Add missing name - foreach ($h as $db=>$o) { - if (empty($o['name'])) $h[$db]['name'] = $db; - if (empty($o['id'])) $h[$db]['id'] = $db; + foreach ($dbs as $db=>$o) { + if (empty($o['name'])) $dbs[$db]['name'] = $db; + if (empty($o['id'])) $dbs[$db]['id'] = $db; } # Sort by `order`, min first - uasort($h,function($a,$b){ + uasort($dbs,function($a,$b){ if (empty($a['order']) and empty($b['order'])) return strcmp($a['id'],$b['id']); $a_ = !empty($a['order']) ? $a['order'] : 9999999; $b_ = !empty($b['order']) ? $b['order'] : 9999999; return($a_-$b_); }); - if (!$h) return false; - if ($first !== false) $first = self::ar_first($h); - return $h; + if (!$dbs) return false; + if ($first !== false) $first = self::ar_first($dbs); + return $dbs; } /** @@ -669,10 +685,9 @@ class Db extends nb { # Connection $Db->__construct($db); - if (empty($Db->_no_connect)) { - if (empty($Db->pdo)) $Db->bye("No pdo for db: `".$Db->p('db')."`"); +# NB 21.09.16 if (empty($Db->_no_connect)) { $Db->connect(); - } +# NB 21.09.16 } /* Table @@ -1006,7 +1021,7 @@ class Db extends nb { $has_order = false; foreach ($this->conf as $id => $attr) { - $attr['_no_connect'] = true; +# NB 21.09.16 $attr['_no_connect'] = true; $attr['id'] = $id; if (isset($attr['id'])) $has_order = true; $d = new Db($attr); diff --git a/lib/php/db/config.php b/lib/php/db/config.php index f2e0817b..a2dbe142 100644 --- a/lib/php/db/config.php +++ b/lib/php/db/config.php @@ -4,5 +4,19 @@ require_once(realpath(dirname(__FILE__).'/../db.php')); Db::pinit(); -$DB_CONFS = Db::conf_load([ Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml' ]); +$DB_CONFS = Db::conf_load([ + Db::ROOT_DIR.'/etc/dbs.yaml', + Db::ROOT_DIR.'/etc/dbs.php', + + Db::ROOT_DIR.'/etc/dbq.yaml', + Db::ROOT_DIR.'/etc/dbq.php', + + '/etc/dbq.yaml', + + '/etc/dbq.yaml', + '/etc/dbq.php', + + '~/.dbq.yaml', + '~/.dbq.php', +]); ?> -- 2.47.3