]> git.nbdom.net Git - nb.git/commitdiff
lib/php/out.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 6 Dec 2017 00:39:01 +0000 (00:39 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 6 Dec 2017 00:39:01 +0000 (00:39 +0000)
16 files changed:
etc/dbq/000-local.php
etc/profile.d/nb.sh
etc/vim/source/vars.vim
lib/php/http.php
lib/php/mime.php
lib/php/nb.php
lib/php/out.php
share/db/0-clean.sql [new file with mode: 0644]
share/db/http_status.sql [new file with mode: 0644]
share/db/mime.sql [new file with mode: 0644]
share/db/mime_type.sql [new file with mode: 0644]
share/db/nb.db [new file with mode: 0644]
share/db/port.sql [new file with mode: 0644]
share/db/shell_function.sql [new file with mode: 0644]
share/db/sys.sql [deleted file]
share/db/update.sh

index a0c97f6532ee8c368678b495bb721056f98522af..1a86220aba90d59184e1a34b2e39b31592240c67 100644 (file)
@@ -3,11 +3,11 @@
 # !!! MANDATORY !!!
 #
 $DBQ = [
-  'nb-sys' => [
+  'nb' => [
     'type' => 'sqlite',
-    'name' => 'Sys',
+    'name' => 'Nb',
     'default_table' => 'mime',
-    'host' => nb::ROOT_DIR.'/share/db/sys.db',
+    'host' => nb::ROOT_DIR.'/share/db/nb.db',
   ],
 ];
 ?>
index 43d68cc57c330ce85183c6905ef285606d9273aa..d1038e5567561717bba372b28b8ea4e7e914dcab 100644 (file)
@@ -1,3 +1,5 @@
+export NB_DB=nb
+
 nb_repos_commit_push() {
   shell_help "Commit each repos whith a comment" "$@" && return
   local status
index ecfb0c12bd34e6175786f86889d5efe472e5438c..41dee9cd48d18c620da8bf58812b37efe15cdc5a 100644 (file)
@@ -3,7 +3,7 @@
 "
 " Command to check files per file types
 "
-">SHELL_REPLACE echo "let mimeCheck = {"; dbq db=nb-sys t=mime -cut 1,3 | sort -k 1 | sed -E -e "s/'/''/g" -e "s/^(.*?)\t(.*)$/  \\\,'\1' : '\2'/" -e "0,/,/s/,//" ; echo "\}"
+">SHELL_REPLACE echo "let mimeCheck = {"; dbq db=$NB_DB t=mime -cut 1,3 | sort -k 1 | sed -E -e "s/'/''/g" -e "s/^(.*?)\t(.*)$/  \\\,'\1' : '\2'/" -e "0,/,/s/,//" ; echo "\}"
 let mimeCheck = {
   \'bash' : 'bash -c'
   \,'erb' : 'sh -c \"cat | erb -x - | ruby -c\" <'
index 03aa1c41c8cdb9329c27e0daa26f32f7d8d6d604..9b5d5cdf56e583225b4c770a2a45d2e344e82b4f 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 class Http {
   const Status = [
-#>SHELL_REPLACE dbq db=nb-sys t=http_status -cut 1,2 | perl -pe "s/'/\\\\'/; s/^(.*?)\t(.*?)$/    '\$1' => '\$2',/"
+#>SHELL_REPLACE dbq db=$NB_DB t=http_status -cut 1,2 | perl -pe "s/'/\\\\'/; s/^(.*?)\t(.*?)$/    '\$1' => '\$2',/"
     '100' => 'Continue',
     '101' => 'Switching Protocols',
     '102' => 'Processing',
index 0cc80ac9edf627979d258edc7f89b9405a6b89c5..16a48c775c413b2ff142b92e06d298f7dc889ff1 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 class Mime {
   const Types = [
-#>SHELL_REPLACE dbq db=nb-sys t=mime_type -cut 2,3 | perl -pe "s/'/\\\\'/; s/^(.*?)\t(.*?)$/    '\$1' => '\$2',/"
+#>SHELL_REPLACE dbq db=$NB_DB t=mime_type -cut 2,3 | perl -pe "s/'/\\\\'/; s/^(.*?)\t(.*?)$/    '\$1' => '\$2',/"
     'application/applixware' => 'aw',
     'application/atom+xml' => 'atom xml',
     'application/atomcat+xml' => 'atomcat',
index 0868fbced32f797a939746e0525a1624ddcf2e8d..33c43c406aa5eadf0e1d069a843d7c522bb77fa8 100644 (file)
@@ -14,12 +14,13 @@ class NB {
   const ROOT_DIR = NB_ROOT;
   const NB_ROOT = NB_ROOT;
 
-  # Sql: elect type,ext from mime_type where ext in ('json','csv','txt','yaml','xml','html','doc','xls');
   protected static $content_types_aliases = array(
+       #>_SHELL_REPLACE dbq db=$NB_DB table="SELECT type,ext FROM mime_type WHERE ext IN ('json','csv','txt','yaml','xml','html','doc','xls')"
     'text' => 'txt',
     'human' => 'txt',
     'table' => 'html',
     'div' => 'html',
+       #<_SHELL_REPLACE
   );
 
   protected static $content_types = array(
@@ -101,6 +102,7 @@ class NB {
     if ($name === null) return isset($_GET) ? $_GET : [];
     return isset($_GET[$name]) ? $_GET[$name] : $default;
   }
+
   public static function p($name=null,$default=null) {
     if(NB_P_GET) return self::_p_get($name,$default);
     if ($name === null) return isset($_REQUEST) ? $_REQUEST : [];
index 24bc5a4fc0f57435e0bee89e965427f469e414a7..a9150c3219cd8f954cd5428738d226f40a63442b 100644 (file)
@@ -27,6 +27,12 @@ Class Out extends Nb {
         'end' => 'out_human_end',
       ],
 
+      'php_hash' => [
+        'align' => ' => ',
+        'eol' => ",\n",
+        'quote' => "'",
+      ],
+
       'cust' => [
         '_web_' => false,
         'cust' => self::p('cust',"CHANGE ME"),
@@ -271,7 +277,7 @@ Class Out extends Nb {
       echo $call($o,$row);
 
     # Internal method
-    } elseif(is_scalar($o[$k]) and preg_match('/^out_tag|out_human/',$o[$k])) {
+    } elseif(is_scalar($o[$k]) and preg_match('/^(out_tag|out_human|out_size)/',$o[$k])) {
       $f = $o[$k];
       echo self::$f($o,$row);
 
@@ -288,7 +294,7 @@ Class Out extends Nb {
   }
 
   public static function head(&$o,$head,$data=[]) {
-    $o['out_count'] = 0;
+    $o['row_count'] = 0;
 
     # Format $head
     if ($head !== false) {
@@ -327,7 +333,7 @@ Class Out extends Nb {
     ) return;
 
     if (self::type_call('head',$o,$head)) {
-      $o['out_count']++;
+      $o['row_count']++;
       if (isset($o['eol'])) echo $o['eol'];
     }
     return $head;
@@ -336,8 +342,28 @@ Class Out extends Nb {
 
   public static function row(&$o,&$row) {
 
-    if ($o['out_count'] and isset($o['rec'])) echo $o['rec'];
-    if (self::type_call('row',$o,$row)) {
+    if ($o['row_count'] and isset($o['rec'])) echo $o['rec'];
+    $o['row_count']++;
+
+    # buffering
+    if (!empty($o['align'])) {
+       if (!isset($o['sep'])) $o['sep'] = $o['align'];
+       if (!isset($o['rows'])) $o['rows'] = [];
+       if (!isset($o['length'])) $o['length'] = [];
+
+                       $tmp_row = $row;
+                       foreach ($tmp_row as $k => $v) {
+                               if (!isset($o['length'][$k])) $o['length'][$k] = 0;
+        if (!empty($o['quote'])) $tmp_row[$k] = $v = self::quote($v,$o['quote'],isset($o['quote_escape']) ? $o['quote_escape'] : null);
+       $l = mb_strlen($v,self::$charset);
+       if ($l > $o['length'][$k]) $o['length'][$k] = $l;
+                       }
+
+                       $o['rows'][] = $tmp_row;
+                       return true;
+
+    # row() or defined
+    } elseif (self::type_call('row',$o,$row)) {
       $ret = true;
 
     # Assume html/xml tag style
@@ -351,7 +377,6 @@ Class Out extends Nb {
     }
 
     if (isset($o['eol'])) echo $o['eol'];
-    $o['out_count']++;
     return $ret;
   }
 
@@ -363,9 +388,23 @@ Class Out extends Nb {
 
   public static function end(&$o) {
     #if (isset($o['end'])) echo $o['end']($o);
+    if (!empty($o['align'])) {
+                       foreach ($o['rows'] as $rec) {
+                               $values = [];
+
+                               foreach ($rec as $k => $v) {
+               $v = self::mb_str_pad($v, $o['length'][$k], ' ', STR_PAD_RIGHT);
+               $values[] = $v;
+                               }
+
+               echo join($o['sep'],$values) . $o['eol'];
+                       }
+       return;
+    }
+
     self::type_call('end',$o);
     if (isset($o['enclose'])) echo $o['enclose'][1];
-    unset($o['out_count']);
+    unset($o['row_count']);
   }
 
   public static function rows_get($type,$data,$head=array()) {
@@ -420,6 +459,7 @@ Class Out extends Nb {
 
 # NB 27.09.16       if ($count>0) self::row_end($conf);
       $count++;
+
       self::row($conf,$row);
 
     }
@@ -512,6 +552,14 @@ Class Out extends Nb {
     return $v;
   }
 
+  public static function quote($str,$quote='"',$quote_escape='\\') {
+       if ($quote === null or $quote == true) $quote = '"';
+       if ($quote_escape === null) $quote_escape = '\\';
+    $str = str_replace($quote,$quote_escape.$quote,$str);
+    $str = $quote . $str . $quote;
+    return $str;
+  }
+
   public static function row_txt(&$o,&$row) {
     if (!isset($o['col'])) $o['col'] = '';
     self::row_parse($row,$o);
@@ -711,7 +759,6 @@ Class Out extends Nb {
       # header
       if (!$count) echo $sep_line;
 
-# NB 23.11.17       if ($header or $count) echo ''
       echo ''
         . ltrim($o['_human']['sep'])
         . join($o['_human']['sep'],$values)
@@ -733,6 +780,34 @@ Class Out extends Nb {
 
   }
 
+       private static function out_sized(&$o,&$recs) {
+               $len = [];
+
+               foreach ($recs as $rec) {
+
+                       foreach ($rec as $k => $v) {
+                               if (!isset($len[$k])) $len[$k] = 0;
+
+       $l = mb_strlen($v,self::$charset);
+
+       if ($l > $len[$k]) $len[$k] = $l;
+                       }
+
+               }
+
+               foreach ($recs as $rec) {
+                       $values = [];
+
+                       foreach ($rec as $k => $v) {
+        $v = self::mb_str_pad($v, $len[$k], ' ', STR_PAD_RIGHT);
+        $values[] = $v;
+                       }
+
+       echo join($o['sep'],$values) . $o['eol'];
+               }
+
+       }
+
 } Out::init() ; # < Class
 /****************************************************************************/
 
diff --git a/share/db/0-clean.sql b/share/db/0-clean.sql
new file mode 100644 (file)
index 0000000..2c08ca5
--- /dev/null
@@ -0,0 +1,19 @@
+-- NB 05.12.17 DROP TABLE IF EXISTS service;
+-- NB 25.10.17 CREATE TABLE IF NOT EXISTS service (
+-- NB 25.10.17   name VARCHAR(100),
+-- NB 25.10.17   description VARCHAR(200),
+-- NB 25.10.17   PRIMARY KEY (port,name)
+-- NB 25.10.17 );
+
+-- NB 25.10.17 DROP TABLE IF EXISTS service_port;
+
+-- NB 25.10.17 DROP TABLE IF EXISTS ovh_dedicated;
+-- NB 25.10.17 CREATE TABLE ovh_dedicated (
+-- NB 25.10.17   offer VARCHAR(80),
+-- NB 25.10.17   proc VARCHAR(150),
+-- NB 25.10.17   cpu VARCHAR(150),
+-- NB 25.10.17   ram VARCHAR(150),
+-- NB 25.10.17   disk VARCHAR(150),
+-- NB 25.10.17   option VARCHAR(100),
+-- NB 25.10.17   price float(5,2)
+-- NB 25.10.17 );
diff --git a/share/db/http_status.sql b/share/db/http_status.sql
new file mode 100644 (file)
index 0000000..fa699ae
--- /dev/null
@@ -0,0 +1,6 @@
+-- DROP TABLE IF EXISTS http_status;
+CREATE TABLE IF NOT EXISTS http_status (
+  id int PRIMARY KEY,
+  name varchar(100),
+  description varchar(1000)
+);
diff --git a/share/db/mime.sql b/share/db/mime.sql
new file mode 100644 (file)
index 0000000..8d3b086
--- /dev/null
@@ -0,0 +1,5 @@
+CREATE TABLE IF NOT EXISTS mime (
+       id varchar(32) NOT NULL PRIMARY KEY,
+       name varchar(128),
+       cmd_check varchar(300)
+);
diff --git a/share/db/mime_type.sql b/share/db/mime_type.sql
new file mode 100644 (file)
index 0000000..b960fa0
--- /dev/null
@@ -0,0 +1,6 @@
+CREATE TABLE IF NOT EXISTS mime_type (
+       name varchar(128),
+       type varchar(128) NOT NULL DEFAULT '' PRIMARY KEY,
+       ext varchar(128)
+);
+CREATE INDEX IF NOT EXISTS mime_type_ext_idx ON mime_type (ext);
diff --git a/share/db/nb.db b/share/db/nb.db
new file mode 100644 (file)
index 0000000..30236da
Binary files /dev/null and b/share/db/nb.db differ
diff --git a/share/db/port.sql b/share/db/port.sql
new file mode 100644 (file)
index 0000000..eae612e
--- /dev/null
@@ -0,0 +1,8 @@
+DROP TABLE IF EXISTS port;
+CREATE TABLE IF NOT EXISTS port (
+  port INT,
+  proto VARCHAR(3),
+  name VARCHAR(100),
+  PRIMARY KEY (port,proto,name),
+  FOREIGN KEY(name) REFERENCES service(name)
+);
diff --git a/share/db/shell_function.sql b/share/db/shell_function.sql
new file mode 100644 (file)
index 0000000..29e5782
--- /dev/null
@@ -0,0 +1,5 @@
+DROP TABLE IF EXISTS shell_function;
+CREATE TABLE shell_function (
+  name VARCHAR(255) PRIMARY KEY,
+  code BLOB
+);
diff --git a/share/db/sys.sql b/share/db/sys.sql
deleted file mode 100644 (file)
index d46cb97..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
--- NB 05.12.17 DROP TABLE IF EXISTS service;
--- NB 25.10.17 CREATE TABLE IF NOT EXISTS service (
--- NB 25.10.17   name VARCHAR(100),
--- NB 25.10.17   description VARCHAR(200),
--- NB 25.10.17   PRIMARY KEY (port,name)
--- NB 25.10.17 );
-
--- NB 25.10.17 DROP TABLE IF EXISTS service_port;
-DROP TABLE IF EXISTS port;
-CREATE TABLE IF NOT EXISTS port (
-  port INT,
-  proto VARCHAR(3),
-  name VARCHAR(100),
-  PRIMARY KEY (port,proto,name),
-  FOREIGN KEY(name) REFERENCES service(name)
-);
-
--- DROP TABLE IF EXISTS http_status;
-CREATE TABLE IF NOT EXISTS http_status (
-  id int PRIMARY KEY,
-  name varchar(100),
-  description varchar(1000)
-);
-
-DROP TABLE IF EXISTS shell_function;
-CREATE TABLE shell_function (
-  name VARCHAR(255) PRIMARY KEY,
-  code BLOB
-);
-
--- NB 25.10.17 DROP TABLE IF EXISTS ovh_dedicated;
--- NB 25.10.17 CREATE TABLE ovh_dedicated (
--- NB 25.10.17   offer VARCHAR(80),
--- NB 25.10.17   proc VARCHAR(150),
--- NB 25.10.17   cpu VARCHAR(150),
--- NB 25.10.17   ram VARCHAR(150),
--- NB 25.10.17   disk VARCHAR(150),
--- NB 25.10.17   option VARCHAR(100),
--- NB 25.10.17   price float(5,2)
--- NB 25.10.17 );
-CREATE TABLE IF NOT EXISTS mime (
-       id varchar(32) NOT NULL PRIMARY KEY,
-       name varchar(128),
-       cmd_check varchar(300)
-);
-CREATE TABLE IF NOT EXISTS mime_type (
-       name varchar(128),
-       type varchar(128) NOT NULL DEFAULT '' PRIMARY KEY,
-       ext varchar(128)
-);
-CREATE INDEX IF NOT EXISTS mime_type_ext_idx ON mime_type (ext);
index a3d521ac8a3df995bd9dfb4bb0243d2dd3d3f702..a169d5e77d536f9b5df3a93c701e66f29077c849 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/bash --login
 set +e
-dir=$(dirname "$0")
+DIR=$(dirname "$0")
 tmp=`mktemp -d`
 
 csv2table() {
@@ -39,38 +39,38 @@ data_src() {
 
 # NB 25.10.17   perl -ne 'm,^(\w+)\s+\d+/\w+\s+#\s*(.*?)$, and $_=join(qq|\t|,map(/^\s*(.*?)\s*$/,$1,$2)).qq|\n| and !$h{$_}++ and print' \
 # NB 25.10.17     /etc/services \
-# NB 25.10.17     > "$tmp/service.csv"
-# NB 25.10.17   csv2table service "$tmp/service.csv"
+# NB 25.10.17     > "$TMP/service.csv"
+# NB 25.10.17   csv2table service "$TMP/service.csv"
 
   perl -ne 'm,^(\w+)\s+(\d+)/(\w+)\s+#\s*(.*?)$, and print join(qq|\t|,map(/^\s*(.*?)\s*$/,$2,$3,$1)).qq|\n|' \
     /etc/services \
-    > "$tmp/port.csv"
-  csv2table port "$tmp/port.csv"
+    > "$TMP/port.csv"
+  csv2table port "$TMP/port.csv"
 
 # NB 25.10.17   html-table2csv https://www.ovh.co.uk/dedicated_servers/|perl -ne \
 # NB 25.10.17     '/^\w/ or next; @F=split("\t",$_);splice(@F,6,1); $F[6] =~s/ ex.*$//; $F[6] =~s/[^\d\.]//g; print $_ if $_=join("\t",map{$_ eq "-" ? "" : $_} @F[0..6])."\n" and !$dbl{$_}++' \
-# NB 25.10.17     > "$tmp/ovh_dedicated.csv"
-# NB 25.10.17   csv2table ovh_dedicated "$tmp/ovh_dedicated.csv"
+# NB 25.10.17     > "$TMP/ovh_dedicated.csv"
+# NB 25.10.17   csv2table ovh_dedicated "$TMP/ovh_dedicated.csv"
 
   return 0
   w3m -dump -cols 999999 https://en.wikipedia.org/wiki/List_of_HTTP_status_codes| \
     perl -ne 'next unless /^\d\d\d / .. /^ /;s/.\[\d+\]//g; s/^ *//; s/^(\d+) ([\w _-]+).*?\n$/$1\t$2\t/;print' \
-    > "$tmp/http_status.csv"
-  csv2table http_status "$tmp/http_status.csv"
+    > "$TMP/http_status.csv"
+  csv2table http_status "$TMP/http_status.csv"
 
   curl -s http://pgl.yoyo.org/adservers/serverlist.php?hostformat=nohtml \
-    > "$tmp/adservers.csv"
-  csv2table adservers "$tmp/adservers.csv"
+    > "$TMP/adservers.csv"
+  csv2table adservers "$TMP/adservers.csv"
 
   curl -s 'http://www.user-agents.org/' | perl -e '$_=join(qq||,<>); print map {s/^\s*(.*?)(?:.nbsp.)?\s*$/$1/;qq|$_\n|} m,<td class=.right.>([^<]+),g'
-    > "$tmp/useragent.csv"
-  csv2table useragent "$tmp/useragent.csv"
+    > "$TMP/useragent.csv"
+  csv2table useragent "$TMP/useragent.csv"
 
 }
 
 (
 
-cat "$dir/sys.sql"
+cat "$DIR/*.sql"
 cat << EOF
 PRAGMA foreign_keys=OFF;
 .separator \t
@@ -86,15 +86,12 @@ COMMIT;
 VACUUM;
 EOF
 
-) | sqlite3 "$dir/sys.db"
+) | sqlite3 "$DIR/nb.db"
 
-#cp -a "$tmp/" /Users/nico/Downloads/commit/
-rm -rf "$tmp"
+rm -rf "$TMP"
 
 (
 
-export DBQ_PARAMS="db=nb-sys"
-
 for f in $(grep -ErlI '^.{1,3}<SHELL_REPLACE' $NB_ROOT); do
 
   echo "Update $f" | sed "s; $NB_ROOT/; ;"