From: Nicolas Boisselier Date: Wed, 7 Sep 2016 23:57:06 +0000 (+0100) Subject: Bed X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=76cd5eb43feb9ca08d5c71e1dc89f196f8c4227e;p=nb.git Bed --- diff --git a/etc/profile.d/git.sh b/etc/profile.d/git.sh index cc933de8..08120015 100644 --- a/etc/profile.d/git.sh +++ b/etc/profile.d/git.sh @@ -134,7 +134,6 @@ git_conn_check() { fi #[ "$1" = "TEST" ] && dest=$(url2nc "$dest") - #dest="$dest 22" local ok case "$_git_conn_check" in diff --git a/lib/php/db.php b/lib/php/db.php index 1599f757..21512d3b 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -283,6 +283,7 @@ class Db extends nb { */ public function table($name,$params=[]) { #if (!array_key_exists($name,$this->tables)) { + if (!$name) bye('empty name'); if (empty($this->tables[$name])) { $this->tables[$name] = new Table($name,['db'=>$this]+$params); } elseif ($params) { @@ -394,7 +395,7 @@ class Db extends nb { foreach ($this->conn->query($sql,PDO::FETCH_ASSOC) as $row) { #debug($row); $name = current($row); - $t = $this->table($name,$row+['status'=>$row]); + $this->table($name,$row+['status'=>$row]); # add to this->tables too ! } } @@ -493,7 +494,7 @@ class Db extends nb { * @param [FILES] $files Files to load * @return ARRAY the new/existing value of $this->db */ - public static function conf_dbs($files=[],&$first=false) { + public static function conf_load($files=[],&$first=false) { if (empty($files)) return []; # Load all files into a $h if #files is not a hash @@ -515,6 +516,7 @@ class Db extends nb { } unset($yaml,$CONF); + if (!$h) return false; # Emulate hash pointer _import foreach ($h as $db=>$o) { @@ -566,7 +568,7 @@ class Db extends nb { $id = self::p('db'); # Load databases - $conf = self::is_hash($conf) ? $conf : self::conf_dbs($conf); + $conf = self::is_hash($conf) ? $conf : self::conf_load($conf); # Check db=pdo if ($id and preg_match('/^(\w+):(.*)/',$id,$m)) { diff --git a/lib/php/db/index.php b/lib/php/db/index.php index b6656f48..f2bb0562 100755 --- a/lib/php/db/index.php +++ b/lib/php/db/index.php @@ -15,8 +15,8 @@ Db::pinit(); Db::pdef('format',out::client_type()); if(defined('DB_NO_INIT') and DB_NO_INIT) return true; -#bye(Db::conf_dbs(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' ))); -#$conf = Db::conf_dbs(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' )); Db::init($conf); +#bye(Db::conf_load(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' ))); +#$conf = Db::conf_load(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' )); Db::init($conf); Db::init([Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml']); if(defined('DB_NO_ACTION') and DB_NO_ACTION) return true; diff --git a/lib/php/db/page.php b/lib/php/db/page.php index 9421f5f0..027e452f 100644 --- a/lib/php/db/page.php +++ b/lib/php/db/page.php @@ -1,6 +1,9 @@ type); + $Page = new Page([ #'title' => 'Test', 'content_type' => ((Page::php_cli() and !Page::p('web') )? 'text/plain' : 'text/html'), diff --git a/lib/php/db/shell.php b/lib/php/db/shell.php index 3a7a2c6e..32339c93 100755 --- a/lib/php/db/shell.php +++ b/lib/php/db/shell.php @@ -8,7 +8,7 @@ require_once(dirname(__FILE__).'/../db.php'); # # Default params # -Db::pinit(); +argv2request(); # Set format from client Accept if != html #bye(out::client_type()); @@ -17,71 +17,84 @@ Db::pdef('format',out::client_type()); # # Confs # -$Confs = Db::conf_dbs(array( Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml' )); +$Confs = Db::conf_load(array( Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml' )); # # Parse stdin # $Db = null; $Table = null; -#bye(Db::conf_search_db($Conf['ui'])); +$no_reset_params = [ + 'db', + 'table', + 'format', +]; #bye($Confs['ui']); while($line = fgets(STDIN)){ + $line = trim($line); + + # Ignore comment + if (preg_match('/^\s*#/',$line)) continue; + + # Exit command + if ($line == 'quit' or $line == 'q') break; # Set params - #if (preg_match('/^\s*([^=]+)\s*=\s*(.*?)\s*$/',$line,$args)) { - #echo "IGNORE: $line"; - while ($line and preg_match('/^\s*([^=]+)\s*=\s*(.*?)\s*$/',$line,$args)) { + while ($line and (0 + #or preg_match('/^([\w\.]+)=("[^"]+"|\S+)/',$line,$args) + or preg_match("/^([\w\.]+)='([^']+)'/",$line,$args) + or preg_match('/^([\w\.]+)="([^"]+)"/',$line,$args) + or preg_match('/^([\w\.]+)=(\S+)/',$line,$args) + )) { + # (?:(?:[^()]++|"(?R)")*) + $line = mb_substr($line,mb_strlen($args[0])+1); + #debug($args); array_shift($args); - #var_dump($args); - Db::pset($args[0],$args[1]); - $line = mb_substr($line,mb_strlen($line)-1); + list ($k,$v) = $args; - } + Db::pset($k,$v); + Db::pinit(); - Db::pinit(); - if (!Db::p('action')) continue; - if (true and !db_shell_init($Confs)) { - echo "ERR: $line\n"; - continue; - } + switch ($k) { - #Db::bye(Db::p()); - #var_dump(Db::p()); - #echo ">>".$Db->p('action')."\n"; + case 'action': - $r = $Db->action($Db->p('action'),$Table); - #foreach ($args + if ($v == 'reset') { + $keep = []; + foreach (Db::p() as $k => $v) { if (in_array($k,$no_reset_params)) $keep[$k] = $v; } + $_REQUEST = []; + foreach ($keep as $k => $v) { Db::pset($k,$v); } + $keep = []; + break; + } + $Db->action($Db->p('action'),$Table); + break; -} + case 'db': +#bye($Confs[$v]); + $Db = new Db($Confs[$v]+['conf'=>$Confs]); + $Db->connect(); + break; -function db_shell_init($confs) { - global $Db, $Table; + case 'table': + $Table = new Table($v); + break; - static $p_db = null; - if (Db::p('db') and ( - $p_db === null or $p_db !== Db::p('db') - )) { - #if (!empty( - unset($Table); - unset($DB); - return Db::init($confs); - if (!($conf = Db::conf_search_db($confs))) return; -#unset($conf['dbs']); unset($conf['conf']); bye($conf); - #bye($conf); - $Db = new Db($conf); - $p_db = Db::p('db'); - } - #$r = $Db->action($Db->p('action'),$Table); - #bye(var_dump(Db::p(),true)); + case 'format': + break; - static $p_table = null; - if ($p_table === null) $p_table = Db::p('table'); + default: + echo "ERR: $k=$v\n"; + Db::pset($k,null); + break; - bye([$p_db,$p_table]); + } + + } } + ?> diff --git a/lib/php/nb.php b/lib/php/nb.php index 4d8c83c2..ca5abe8e 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -8,6 +8,7 @@ if (!function_exists('yaml_emit')) { } require_once(realpath(dirname(__FILE__).'/functions.php')); #$nb = new NB(); debug($nb->test()); +#if (!defined('NB_PARAMS')) define('NB_PARAMS',$_REQUEST); if (class_exists('NB')) return; class NB { @@ -237,9 +238,14 @@ class NB { * Function: argv2request * Set $_REQUEST from argv */ - public static function argv2request() { + public static function argv2request($args=null) { - global $argv; + if (isset($args)) { + $argv = $args; + $args = []; + } else { + global $argv; + } $new_argv = array(); diff --git a/lib/php/out.php b/lib/php/out.php index 37c63ce8..fb56deb7 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -512,10 +512,14 @@ function out_human(&$row,&$o) { if (empty($GLOBALS['_human_len'])) { $GLOBALS['_human_len'] = []; if (empty($o['_human'])) $o['_human'] = []; + $o['tmpfile'] = tmpfile(); + + # switch for later use of out_csv() $o['sep.human'] = $o['sep']; $o['sep'] = "\t"; + + # no echo with out_csv() $o['return'] = true; - $o['tmpfile'] = tmpfile(); } $line = out_csv($row,$o); @@ -546,8 +550,9 @@ function out_human_end(&$o) { $sep_line = ''; if (out::p('sep_line') !== '0') { + foreach ($len as $i) { - $sep_line .= '+'.str_repeat('-',$i+2); + $sep_line .= '+'.str_repeat('-',$i+strlen($o['sep.human'])-1); } $sep_line .= '+'."\n"; }