]> git.nbdom.net Git - nb.git/commitdiff
bin/dbq-vi
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 5 Sep 2024 19:01:51 +0000 (21:01 +0200)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 5 Sep 2024 19:01:51 +0000 (21:01 +0200)
bin/dbq-vi
bin/dbq.php
www/dbq/dbq.php

index 6d7832312eaf3a3ee27fdc97bfff2f2f62359788..96f510ca4623755768e3f8b19a41be8f1be3fbcc 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/env bash
 tmp=/tmp/$USER-dbq-vi-$$
 
-main() {
+_main() {
        #exec echo $tmp
        dbq.php "$1/vi/$2.sh" > "$tmp" || return
 
@@ -15,7 +15,29 @@ main() {
        bash -f "$tmp" || return
        #echo $md5 " == $(md5sum "$tmp")"
 
-       eval "dbq.php $1/update/ format=human $(cat "$tmp")"
+       eval dbq.php $1/update/ format=yaml $(cat "$tmp")
+
+}
+
+main() {
+       #exec echo $tmp
+       cat <<EOF > "$tmp"
+#
+# dbq-vi
+# Use "key: |" for new lines
+#
+EOF
+
+       dbq.php "$1/vi/$2.yaml" >> "$tmp" || return
+       md5=$(md5sum "$tmp")
+
+       vi "$tmp"
+       [ -s "$tmp" ] || return 
+
+       [ "$md5" == "$(md5sum "$tmp")" ] && return 
+       #echo $md5 " == $(md5sum "$tmp")"
+
+       dbq.php $1/update.yaml __YAML__="$tmp"
 
 }
 
@@ -26,6 +48,7 @@ Usage: dbq-vi /db/table id
 EOF
        exit
 fi
+
 main $@
 
 rm -f "$tmp"
index 12d91a09df217a3255e2463ae0e8f05c255afce6..81ee0efc38cf4f67c39824215b5d88fa23f885d0 100755 (executable)
@@ -1,5 +1,6 @@
 #!/usr/bin/env php
 <?php
+require_once(realpath(dirname(__FILE__).'/../lib/php/nb.php'));
 /**
 * @copyright (C) 2022 Nicolas Boisselier
 * @author Nicolas Boisselier nicolas.boisselier@gmail.com
@@ -17,13 +18,27 @@ if (isset($GLOBALS['argv']) and count($GLOBALS['argv'])>1) {
        $_SERVER['REQUEST_URI'] = $GLOBALS['argv'][1];
 
        for($i=2;$i<count($GLOBALS['argv']);$i++) {
+
                list($k,$v) = explode('=',trim($GLOBALS['argv'][$i]));  
-               $_GET[$k]= $v;
-               $_SERVER['QUERY_STRING'] .= (empty($_SERVER['QUERY_STRING']) ? '' : '&').trim($GLOBALS['argv'][$i]);
+
+               if ($k == '__YAML__') {
+                       #$str = file_get_contents($GLOBALS['argv'][$i+1]);
+                       $str = file_get_contents($v);
+                       #bye($str);
+                       $_GET = nb::yaml_decode($str)[0];
+
+               } else {
+                       $_GET[$k]= $v;
+
+                       # NB 05.09.24 $_SERVER['QUERY_STRING'] .= (empty($_SERVER['QUERY_STRING']) ? '' : '&').trim($GLOBALS['argv'][$i]);
+               }
+
        }
 
-       if (!empty($_SERVER['QUERY_STRING'])) $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
+       # NB 05.09.24 if (!empty($_SERVER['QUERY_STRING'])) $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
 }
 
+#bye($_GET);
+$GLOBALS['argv'] = []; # to cancel argv2request
 require(realpath(dirname(__FILE__).'/../www/dbq/dbq.php'));
 ?>
index 41ab6e69e6c245e8056a1ac232a900968cd9e4a6..98dc6185ca449b626d9fe3b822ea456d0eba9ff8 100644 (file)
@@ -757,12 +757,6 @@ class DbQ extends nb {
                foreach (['json','yaml'] as $type) {
 
                        if (!strpos($content_type,'/'.$type)) continue;
-# NB 20.07.18                  if ($raw) $_POST = [ $type => $raw ];
-# NB 20.07.18                  if (empty($_POST[$type])) continue;
-# NB 20.07.18                  #bye($_POST);
-# NB 20.07.18                  $post = $_POST;
-# NB 20.07.18 
-# NB 20.07.18                  unset($post[$type]);
 
                        $fct = $type . '_decode';
                        $_POST = (array)$this->$fct(file_get_contents("php://input"));
@@ -789,6 +783,7 @@ class DbQ extends nb {
                # NB 30.08.24: Done in bin/dbq.php 
                //if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = join('/',( count($GLOBALS['argv'])>0 ) ? array_slice($GLOBALS['argv'],1) : []);
 
+               if (!isset($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = '/';
                list ($path,$args) = strpos($_SERVER['REQUEST_URI'],'?') ? explode('?',$_SERVER['REQUEST_URI']) : [$_SERVER['REQUEST_URI'],''];
                $this->uri = $path;
                $this->uri_params = explode($this->explode_args,$args);
@@ -1200,41 +1195,6 @@ EOF;
                foreach (['table','action','args'] as $k) { if (!empty($this->params[$k])) $arg[] = $this->params[$k]; }
                $arg = join('/',$arg);
 
-/*
-# NB 14.12.22: What use ???  
-               $conf = [];
-               $fct_help = function() use ($conf,$arg) {
-                       $this->page($this->db,$conf['root']);
-               };
-               $conf = [ 
-                       'root' => [
-                               [ 'help', 'This help', $fct_help ],
-                               # NB 14.12.22 [ 'help', 'This help', function() use ($conf) { $this->page($this->db,$conf['root']); } ],
-                               [ 'ls', 'List configured databases', function() {
-                                       $this->page($this->db,'ls',[],function(&$r){
-                                               if ($this->perm < self::ADMIN) unset($r['host'],$r['type']);
-                                               $r['id'] = $this->page->tag('a',$r['id'],'href="'.$this->db->base.'/'.$r['id'].'"');
-                                       });
-                               } ],
-                               [ 'random_str', 'Generate a random string', function() { $this->page($this->random_str()); } ],
-# NB 30.01.19                          [ 'databases', 'List others databases', function() {
-# NB 30.01.19                                  $this->page($this->db,'databases');
-# NB 30.01.19                          }, self::ADMIN ],
-                       ],
-               ];
-               #$conf['root'][0][2] = function() use ($conf) { $this->page($this->db,$conf['root']); };
-               foreach ($conf['root'] as $cmd) {
-                       $action_ = $cmd[0];
-                       $laius = $cmd[1];
-                       $fct = $cmd[2];
-                       $perm = empty($cmd[3]) ? 0 : $cmd[3];
-
-                       if ($action != $action_) continue;
-                       $this->deniedUnless(!$perm or $this->perm >= $perm);
-                       #debug($action_);
-                       $fct();
-               }
-*/
                if ($action == 'help') {
 
                        $this->page($this->db
@@ -1445,25 +1405,25 @@ EOF;
 
                } elseif ($action == 'insert') {
                        $this->deniedUnless($this->perm >= self::WRITE);
-                       if (!$this->table->insert(empty($_POST) ? $_GET : $_POST,$info)) $this->error('insert: '.print_r($info,true));
+                       if (!$this->table->insert($this->getData(),$info)) $this->error('insert: '.print_r($info,true));
                        header('Location: '.$this->table->base.'/');
                        $this->page($info);
 
                } elseif ($action == 'update') {
                        $this->deniedUnless($this->perm >= self::WRITE);
-                       if (!$this->table->update(empty($_POST) ? $_GET : $_POST,$info)) $this->error('update: '.print_r($info,true));
+                       if (!$this->table->update($this->getData(),$info)) $this->error('update: '.print_r($info,true));
                        $this->redirect_if_no_referer($this->table->base.'/');
                        $this->page($info);
 
                } elseif ($action == 'replace') {
                        $this->deniedUnless($this->perm >= self::WRITE);
-                       if (!$this->table->replace(empty($_POST) ? $_GET : $_POST,$info)) $this->error('replace: '.print_r($info,true));
+                       if (!$this->table->replace($this->getData(),$info)) $this->error('replace: '.print_r($info,true));
                        $this->redirect_if_no_referer($this->table->base.'/');
                        $this->page($info);
 
                } elseif ($action == 'rm') {
                        $this->deniedUnless($this->perm >= self::DELETE);
-                       if (!$this->table->delete(empty($_POST) ? $_GET : $_POST,$info)) $this->error('rm: '.print_r($info,true));
+                       if (!$this->table->delete($this->getData(),$info)) $this->error('rm: '.print_r($info,true));
                        $this->redirect_if_no_referer($this->table->base.'/');
                        $this->page($info);
 
@@ -1486,6 +1446,16 @@ EOF;
 
        }
 
+       private function getData() {
+               $data = [];
+               if (empty($data) and !empty($_POST)) $data = $_POST;
+               if (empty($data) and !empty($_GET)) $data = $_GET;
+               # NB 05.09.24 if (!empty($data['__YAML__'])) $data = $this->yaml_decode($data['__YAML__']);
+               # NB 05.09.24 if (!empty($data['__JSON__'])) $data = $this->yaml_decode($data['__JSON__']);
+#bye($data);
+               return $data;
+       }
+
        public function redirect_if_no_referer($url) {
                if (!empty($_SERVER['HTTP_REFERER'])) $url = $_SERVER['HTTP_REFERER'];
                if ($this->p('referer')) $url = urldecode($this->p('referer'));