From: Nicolas Boisselier Date: Sun, 4 Dec 2016 02:35:53 +0000 (+0000) Subject: Bed X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=869d07f9e39c597d9bdc2305258644f254564c61;p=nb.git Bed --- diff --git a/etc/dbq/zzz-all.php b/etc/dbq/zzz-all.php index fb25631c..37f1a702 100644 --- a/etc/dbq/zzz-all.php +++ b/etc/dbq/zzz-all.php @@ -79,3 +79,4 @@ foreach ($DBQ as $id => $db) { $DBQ['all']['_import'][] = $id; } +if (nb::p('db')!='nb') $DBQ['all']['_import'][] = 'nb'; diff --git a/lib/php/benchmark.php b/lib/php/benchmark.php index 94716afc..a1d7cd37 100755 --- a/lib/php/benchmark.php +++ b/lib/php/benchmark.php @@ -11,12 +11,6 @@ $hash = array(); for ($i=0;$i<100000;$i++) $hash[$i] = $i; $a = $b = ''; echo "Start benchmark\n"; -/* -$a = array('a' => 'A'); -if (isset($a['a'])) echo "OK\n"; -echo realpath('lib/php/default.php')."\n"; -*/ - /* print_r(parse_str('first=value&arr[]=foo+bar&arr[]=baz')); echo $first; diff --git a/lib/php/db.php b/lib/php/db.php index 9d995226..ce8ea218 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -99,18 +99,19 @@ class Db extends nb { ]; protected static $action_help = [ - ['(db.)?help','This help'], - ['(db.)?ls','List databases from conf'], - ['(db.)?tables','List tables (name=* type=* count=1 engine=*)'], + ['db.help','This help'], + ['db.ls','List databases from conf'], + ['db.database','List other databases on the same server'], + ['db.tables','List tables (name=* type=* count=1 engine=*)'], - ['(table.)?rows','Dump one table, use format='], - ['(table.)?fields','List fields'], - ['table.infos','Table infos (count(*). Type, ...)'], - ['table.sql','Get the sql source'], + ['table.rows','Dump one table, use format='], + ['table.fields','List fields'], + ['table.status','Table status (count= maxlen= max=)'], + ['table.sql','Sql source to create the table'], - ['(table.)?insert','Insert a record'], - ['(table.)?update','Update a record'], - ['(table.)?delete','Delete a record'], + ['table.insert','Insert a record'], + ['table.update','Update a record'], + ['table.delete','Delete a record'], ]; # Web @@ -378,9 +379,9 @@ class Db extends nb { * Return a table instance */ public function table($name='',$params=[]) { - #if (!array_key_exists($name,$this->tables)) { if (!$name and !empty($this->default_table)) $name = $this->default_table; if (!$name) bye('table name is empty'); + if (empty($this->tables[$name])) { $this->tables[$name] = new Table($name,['db'=>$this]+$params); } elseif ($params) { @@ -845,7 +846,7 @@ class Db extends nb { if (!self::p('table') and ($v = array_keys($Db->tables()))) self::pset('table',$Db->ar_first($v)); if (self::p('table')) $Table = $Db->table(self::p('table'), - empty($db['tables'][$Db->p('table')]) ? [] : $db['tables'][$Db->p('table')] + ( empty($db['tables'][$Db->p('table')]) ? [] : $db['tables'][$Db->p('table')] ) ); } @@ -1265,7 +1266,9 @@ class Db extends nb { return 'csv'; } } + public function page($opt=[]) { + require_once(realpath(dirname(__FILE__).'/page.php')); return new Page(array_merge([ 'title' => ($this->title ? $this->title : this::prettyText($this->name)), 'css' => [ diff --git a/lib/php/db/init.php b/lib/php/db/init.php index 990aba06..b3dd1650 100644 --- a/lib/php/db/init.php +++ b/lib/php/db/init.php @@ -1,6 +1,6 @@ $row) { + $i=0; + foreach ($row as $k=>$v) { + $ROWS[$key][$i] = $v; + $i++; + } + } #$opt['var']['rows']=[]; $_REQUEST_BAK = $_REQUEST; diff --git a/lib/php/default.php b/lib/php/default.php deleted file mode 100644 index 4f30e1c6..00000000 --- a/lib/php/default.php +++ /dev/null @@ -1,7 +0,0 @@ - diff --git a/lib/php/default.php.TODEL b/lib/php/default.php.TODEL new file mode 100644 index 00000000..4f30e1c6 --- /dev/null +++ b/lib/php/default.php.TODEL @@ -0,0 +1,7 @@ + diff --git a/lib/php/functions.php b/lib/php/functions.php index 2c5d8f4b..abc71867 100644 --- a/lib/php/functions.php +++ b/lib/php/functions.php @@ -9,7 +9,6 @@ s/array_key_exists(\([^,]\+\),\([^)]\+\))/isset(\2[\1])/g s/^define(\([^,]\+\)/if (!defined(\1)) define(\1/ */ -// NB 28.06.15 require_once((dirname(__FILE__).'/default.php')); function file_write($file,$data,$mode='w') { return nb::file_write($file,$data,$mode); @@ -78,7 +77,6 @@ function warn ($msg) { } function err($msg='__err__',$preff='err',$backtrace_deep=0) { -# NB 05.04.16 $msg = is_scalar($msg) ? $msg : print_r($msg,true); $msg = is_scalar($msg) ? $msg : print_r($msg,true); $preff_msg = $preff ? strtoupper($preff).': ' : ''; if ($msg !== '__err__' and $backtrace_deep !== false) { diff --git a/lib/php/nb.php b/lib/php/nb.php index 6a976675..ec349bf8 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -24,10 +24,11 @@ class NB { 'doc' => 'application/msword', 'xls' => 'application/vnd.ms-excel', 'xml' => 'application/xml', + 'txt' => 'text/plain', 'csv' => 'text/csv', 'html' => 'text/html', - 'txt' => 'text/plain', 'yaml' => 'text/yaml', + 'php' => 'text/x-php', ); #public static function zaza() { return (is_object($this) ? 'TRUE' : 'FALSE'); } diff --git a/lib/php/page.php b/lib/php/page.php index cadc3b25..884da78a 100644 --- a/lib/php/page.php +++ b/lib/php/page.php @@ -42,9 +42,25 @@ class Page extends nb { public $h1 = ''; public $sep = ' / '; - /* - New - */ + public function call($calls) { + foreach ((array)($calls) as $call) { + + if (is_scalar($call)) { + $this->$call(); + + } elseif ( is_array($call) ) { + $k = $call[0]; + $this->$k($call[1]); + + } elseif (is_callable($call)) { + $call($this); + + } + + } + + } + public function __construct($opt = []) { // Content-type @@ -74,25 +90,7 @@ class Page extends nb { if ( empty($this->h1) ) $this->h1 = !empty($nav) ? join($this->sep,$nav) : $this->title; // Call / Print - - // Nav - foreach ((array)($this->call) as $call) { - - if (is_scalar($call)) { - $this->$call(); - - } elseif ( is_array($call) ) { - $k = $call[0]; - $this->$k($call[1]); - - } elseif (is_callable($call)) { - $call($this); - - } else { - - } - - } + if (!empty($this->call)) $this->call($this->call); } @@ -348,6 +346,7 @@ class Page extends nb { public function headers() { #header('Content-type: ' . self::$content_type); + self::$content_type = 'text/plain'; header('Content-type: '.self::$content_type . (($c = strtoupper ( self::$charset )) ? "; charset=$c" : "")); if (isset($this->expires)) self::headers_cache($this->expires); @@ -520,7 +519,7 @@ class Page extends nb { return join("\n",$m[0]); } - public function infos() { + public function status() { $out = array( 'path' => self::path() ); @@ -579,8 +578,10 @@ class Page extends nb { if (!$format and $content_type) { self::p('format',self::mime2ext($content_type)); + } elseif (!$format and ($client_type = self::client_content_type())) { self::p('format',self::mime2ext($client_type)); + } if (!$content_type and $format) $content_type = self::ext2mime($format); diff --git a/share/templates/hosts.php b/share/templates/hosts.php index 00232ce4..eb55f477 100644 --- a/share/templates/hosts.php +++ b/share/templates/hosts.php @@ -7,6 +7,7 @@ if (isset($ROWS)) foreach ($ROWS as $r) { 'ip', 'addr', 'address', + 0, ] as $k) { if (!isset($r[$k])) continue; $line[] = $r[$k]; @@ -18,6 +19,7 @@ if (isset($ROWS)) foreach ($ROWS as $r) { 'host', 'name', 'id', + 1, ] as $k) { if (!isset($r[$k])) continue; $line[] = $r[$k]; diff --git a/share/templates/nginx-lb.php b/share/templates/nginx-lb.php index 3c49d933..f08e20ec 100644 --- a/share/templates/nginx-lb.php +++ b/share/templates/nginx-lb.php @@ -1,11 +1,14 @@ $servers) { echo "upstream $name {\n"; foreach ($servers as $server) { diff --git a/src/db/http_status.script b/src/db/http_status.script new file mode 100644 index 00000000..cd3afb3e --- /dev/null +++ b/src/db/http_status.script @@ -0,0 +1,6 @@ +DROP TABLE IF EXISTS http_status; +CREATE TABLE http_status(id int primary key,name varchar(100),description varchar(1000)); +DELETE FROM http_status; +.sep "\t" +.import /Users/nico/tmp/http_status.txt http_status +-- SELECT * FROM http_status limit 5; diff --git a/www/dbq/html/index.php b/www/dbq/html/index.php index 298c2d6d..f907dd8c 100644 --- a/www/dbq/html/index.php +++ b/www/dbq/html/index.php @@ -4,45 +4,49 @@ if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = ''; $params = [ #'db', - 'action', 'table', + 'action', 'format', ]; -# NB 03.12.16 list( -# NB 03.12.16 $action, -# NB 03.12.16 $db, -# NB 03.12.16 $table, -# NB 03.12.16 $format, -# NB 03.12.16 ) = -#if (strpos($_SERVER['REQUEST_URI'],'?')!==false) return header('HTTP/1.1 501 Not Implemented'); -if (strpos($_SERVER['REQUEST_URI'],'?')!==false) return http_response_code(501); -$values = explode('/',$_SERVER['REQUEST_URI']); -while (count($values)>0 and $values[0]==='') { - array_shift($values); +if (false and strpos($_SERVER['REQUEST_URI'],'?')!==false) { + #return http_response_code(501); + $msg = '501 Not Implemented'; + header($_SERVER['SERVER_PROTOCOL'].' '.$msg); + echo "$msg\n"; + return; } -#for ($i=count($values);$i>0 and $values[ +$_SERVER['REQUEST_URI'] = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']); +$values = explode('/',trim($_SERVER['REQUEST_URI'],'/')); require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/page.php'); -#debug($_SERVER['REQUEST_URI']); +#bye($_REQUEST); foreach ($values as $k=>$v) { + #if ($v!=='') debug($params[$k].'='.$v); if ($v!=='') Page::pdef($params[$k],$v); } Page::pdef('format','human'); -Page::pdef('action','ls'); +#Page::pdef('action','db.ls'); require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/db/init.php'); -$Db->page([ - 'call' => [ +header('Content-type: text/plain'); +$Page = $Db->page(); +$Page->headers_no_cache(); +$Page->call([ +# NB 04.12.16 $Db->page([ +# NB 04.12.16 'call' => [ 'begin', #['out', "Hello World !!!\n"], function($page) use($Db){ - if (!$page->action()) return $Db->action($Db->p('action'),$Db->table()); + #debug($Db->p()); + #global $values; debug($values); + }, + function($page) use($Db){ + if (!$page->action()) return $Db->action($Db->p('action'),$Db->table($Db->p('table'))); }, function($page) use($Db){ - #global $values; debug($values); if (!$page->is('html')) return; if (!empty($Db)) echo $page->tag('p class="db-infos"',join(' | ',[ $Db->type, @@ -52,7 +56,7 @@ $Db->page([ }, 'end', - ], +# NB 04.12.16 ], ]); #require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/db/page.php'); # /DB/ACTION/[TABLE]/[FORMAT]