]> git.nbdom.net Git - nb.git/commitdiff
dbq
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 29 Feb 2016 21:59:15 +0000 (21:59 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 29 Feb 2016 21:59:15 +0000 (21:59 +0000)
bin/dbq
lib/php/db.php
lib/php/page.php

diff --git a/bin/dbq b/bin/dbq
index 18203056b008cb7ee98af3e97077d294dc7b5d07..dbc9886a64be2437a1bea6bd0eaa00169ed06927 100755 (executable)
--- a/bin/dbq
+++ b/bin/dbq
@@ -8,7 +8,6 @@ use Encode;
 #
 #################################################################################
 my $VERSION = '0.0.1';
-# 
 # NB 28.02.16
 # - create script
 
@@ -20,12 +19,9 @@ my $VERSION = '0.0.1';
 my ($NAME) = $0 =~ m,([^/]+)$,;
 my $UC_NAME = uc($NAME);
 my %ACTION = (
-  'format' => $ENV{$UC_NAME.'_FORMAT'} ? $ENV{$UC_NAME.'_FORMAT'} : 'json',
+  'format' => $ENV{$UC_NAME.'_FORMAT'} ? $ENV{$UC_NAME.'_FORMAT'} : 'nc',
   'action' => $ENV{$UC_NAME.'_ACTION'} ? $ENV{$UC_NAME.'_ACTION'} : 'rows',
-);
-my @CURL_OPT = (
-  'dump-header|D=s',
-  'get|G!',
+  'txt_errors' => $ENV{$UC_NAME.'_TXT_ERRORS'} ? $ENV{$UC_NAME.'_TXT_ERRORS'} : '1',
 );
 
 my %CMD_ALIASE = (
@@ -35,6 +31,22 @@ my %CMD_ALIASE = (
   'l' => 'limit',
 );
 
+my @CURL_OPT = (
+  'dump-header|D=s',
+  'get|G!',
+  'compressed|z!',
+);
+my $CURL_OPT_EXP = join('|',map{ s/\|.*$//; $_ } @CURL_OPT);
+# NB 29.02.16 add_argv('--compressed');
+# NB 29.02.16 sub add_argv {
+# NB 29.02.16   #my $search = shift;
+# NB 29.02.16   my $found = 0;
+# NB 29.02.16   foreach my $search (@_) {
+# NB 29.02.16     foreach (@ARGV) { $found == 1 and last if $_ eq $search; }
+# NB 29.02.16     #die @ARGV,$search unless $found;
+# NB 29.02.16     #push @ARGV,$search unless $found;
+# NB 29.02.16   }
+# NB 29.02.16 }
 
 #################################################################################
 #
@@ -46,10 +58,14 @@ my $DEBUG = $main::DEBUG = 0;
 
 csv2txt("\t",shift @ARGV) if @ARGV and $ARGV[0] eq '--csv2txt' and shift @ARGV;
 curl_help() if @ARGV and $ARGV[0] eq '--curl_help' and shift @ARGV;
+parse_debug() if @ARGV and $ARGV[0] eq '--parse_debug' and shift @ARGV;
+curl_help_hash() if @ARGV and $ARGV[0] eq '--curl_help_hash' and shift @ARGV;
 
 my %Opt; get_options(\%Opt,\@CURL_OPT);
 #use Data::Dumper; die Dumper(\%Opt);
 
+$Opt{compressed} = 1 if exists $Opt{compressed};
+
 for (@CURL_OPT) {
   s/\|.*$//;
   next unless $Opt{$_};
@@ -65,12 +81,22 @@ for (@CURL_OPT) {
 # BEGIN
 #
 #################################################################################
+
+#################################################################################
+#
+# Build command
+#
 my @cmd = (
   #'echo',
        'curl', '-s', ($ENV{$UC_NAME.'_URL'} ? $ENV{$UC_NAME.'_URL'} : 'http://127.0.0.1'),
        '-H', 'Host: '.($ENV{$UC_NAME.'_HOST'} ? $ENV{$UC_NAME.'_HOST'} : 'rent'),
 );
 
+#
+# Add args
+#
+push (@cmd,$_) if ($_ = '-'.'v'x($VERBOSE-1) ) ne '-';
+
 #
 # Distinct arg from key=value
 #
@@ -85,11 +111,6 @@ while ($_ = shift @ARGV) {
 
 }
 
-#
-# Add args
-#
-push (@cmd,$_) if ($_ = '-'.'v'x($VERBOSE-1) ) ne '-';
-
 #
 # Defaults Actions
 #
@@ -100,30 +121,35 @@ while (my ($k,$v) = each %ACTION) {
 
 $keys{header} = '0' if $keys{action} ne 'rows';
 
+#################################################################################
 #
-# Process specifics
+# Parser
 #
-#die if !$keys{table} or $keys{action} !~ /rows/;
+if ($keys{format} eq 'nc') {
+  $keys{format} = 'csv';
+  open(STDOUT,"|$0 --parse_debug|$0 --csv2txt ".( ( defined($keys{header}) ? $keys{header} : '') eq '0' ? '1' : '0'))
 
-open(STDOUT,"|$0 --csv2txt ".( ( defined($keys{header}) ? $keys{header} : '') eq '0' ? '1' : '0'))
-  if $keys{format} eq 'nc' and $keys{format}='csv';
+} elsif ( $keys{format} eq 'jq' and $keys{format}='json' ) {
+  open(STDOUT,"|$0 --parse_debug|jq .")
 
-open(STDOUT,"|jq .") if $keys{format} eq 'jq' and $keys{format}='json';
-
-#
-# Push key=value
-#
-push(@cmd,map {('--data-binary',esc($_).'='.esc($keys{$_}))} sort keys %keys);
+} elsif ( $keys{debug} ) {
+  open(STDOUT,"|$0 --parse_debug")
 
+}
 #use Data::Dumper; die Dumper(\@cmd,\%keys);
 #warn 'Uri: '.join("&",@keys)."\n" if $VERBOSE>1;
-warn 'Command:',map{(/^--data/?"\n  ":" ").$_} @cmd,"\n" if $VERBOSE;
+#warn 'Command:',map{(/^--data/?"\n  ":" ").$_} @cmd,"\n" if $VERBOSE;
 #die join(" ",@cmd);
 #################################################################################
 #
-# END
+# END - Exec command
 #
 #################################################################################
+
+# Push key=value
+push(@cmd,map {('--data-binary',esc($_).'='.esc($keys{$_}))} sort keys %keys);
+
+warn "$NAME: Command: ",join(" ",map{/\s+/ ? '"'.$_.'"' : $_} @cmd),"\n" if $VERBOSE;
 system @cmd;
 close STDOUT;
 exit 0;
@@ -133,6 +159,17 @@ exit 0;
 # Functions
 #
 #################################################################################
+sub parse_debug {
+  while (<>) {
+
+    if (/DEBUG: /) { print STDERR $_ }
+    elsif ( /^(<\w+>)?Fatal error/ .. /^\s+thrown in/) { s/<[\w\/ ]+>//g; print STDERR $_ }
+    else { print $_ }
+
+  }
+  exit;
+}
+
 sub csv2txt {
   my $sep = shift;
   my $noheader = shift;
@@ -140,6 +177,10 @@ sub csv2txt {
   my @lines = ();
   my $l;
 
+  # sprintf missing argument warning message ????
+  # Can't open XXX: No such file or directory
+  no warnings;
+
   #open(STDOUT,">&2");
   #
   # Store lines and lengths in memory
@@ -178,7 +219,6 @@ sub csv2txt {
 
   print $sep_line;
 
-  no warnings; # sprintf missing argument warning message ????
   while ($_ = shift @lines) {
     #warn $format,scalar(@$_);
     #warn ">>$format: ".$_->[0];
@@ -252,8 +292,8 @@ sub version { print "$NAME: version [$VERSION]\n"; exit 0; }
 #------------------------------------------------------------------------------
 sub get_options {
 
-  my $hash = shift @_;
-  my $default = shift @_;
+  my $hash = $_[0]; #shift @_;
+  my $default = $_[1]; #shift @_;
        use Getopt::Long qw(:config no_ignore_case no_auto_abbrev);
 
        #my @Opt = @_>1 ? @{$_[1]} : ();
@@ -282,9 +322,28 @@ sub get_options {
 
 }
 
+sub curl_help_hash {
+  open(CURL,'curl --help |') or die "Can't run curl";
+  my $o = $CURL_OPT_EXP;
+  while (<CURL>) {
+    #print $_;
+    #/^\s*(?:-(\w+),\s*)?--([\w-]+)(.*?)\$/ or next;
+    @_ = /(?:-(\w+).*?)?--($o)\s+(.*?)$/ or next;
+    #warn $_;
+    push(@_,'') if @_<3; $_[0] = '' unless $_[0];
+    $_[1] .= '|' if $_[0];
+
+    my $type = '!';
+    $type = '=s' if $_[2] =~ /^[A-Z]{2,}/;
+    #print "=item B<option[$_[1]$_[0]$type]> $_[2]\n\n";
+    print "'$_[1]$_[0]$type', # $_[2]\n";
+  }
+  close CURL;
+  exit;
+}
 sub curl_help {
   open(CURL,'curl --help |') or die "Can't run curl";
-  my $o = 'get|dump-header|cert-status';
+  my $o = $CURL_OPT_EXP;
   while (<CURL>) {
     #print $_;
     #/^\s*(?:-(\w+),\s*)?--([\w-]+)(.*?)\$/ or next;
index 552caa7afd13ba53d8f4e5453886ffbce70f792a..6bfe1e0d2199d871b314b138cd33876862127298 100644 (file)
@@ -264,6 +264,7 @@ EOF;
 
     if (!$mime=$this->ext2mime($format)) return false;
     header('Content-type: '.$mime);
+
     return $mime;
   }
 
@@ -397,6 +398,7 @@ EOF;
   }
 
   function action($table) {
+    ini_set('html_errors', false);
     if ($this->p('format') == 'table') $this->pset('format','');
     $action = $this->p('action');
 
index 18ff574590c6cc8068010165f8bf9fe590f6ca1a..27604b763b8ec8eaee58ee728820510feb3bd15a 100644 (file)
@@ -239,8 +239,11 @@ class Page extends nb {
    */
   function client_content_type() {
     $headers = getallheaders();
-    if (!isset($headers['Accept'])) return '';
-    $v = split(',', $headers['Accept']);
+    if (0
+      or !isset($headers['Accept'])
+      or $headers['Accept'] == '*/*'
+    ) return '';
+    $v = explode(',', $headers['Accept']);
     return $v ? $v[0] : '';
   }