]> git.nbdom.net Git - nb.git/commitdiff
www/dbq/dbq.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 25 May 2018 14:24:44 +0000 (15:24 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 25 May 2018 14:24:44 +0000 (15:24 +0100)
lib/lua/nb.lua
www/dbq/dbq.php

index f434e900cca6f21ac30de73502070a31576069e3..052d631f9888bf73bf279324a9ef7b8894f04ad9 100644 (file)
@@ -42,6 +42,10 @@ function nb:ngx_location_tmpl(fheader,ffooter)
        if not ffooter then ffooter = root .. "/footer.tmpl.html" end
 
        local template = require "resty.template"
+-- NB 25.05.18         template.print = function(s)
+-- NB 25.05.18           ngx.print(s)
+-- NB 25.05.18         end
+       template.nb = self
        if self.dev then template.caching(false) end
 
        local content = self:readall(file)
@@ -205,9 +209,17 @@ function nb:md2html(content)
        return content
 end
 
+-- function nb.api(url,fmt) return nb:api(url,fmt) end
 function nb:api(url,fmt)
        local res = ngx.location.capture(url)
+       if res.status >= 500 then ngx.exit(res.status) end
+       if not res.body then return end
+       local json = require('json')
        local rows = json.decode(res.body)
+       local html = ''
+       local out = function(s)
+               html = html .. s
+       end
 
        local fct
 
@@ -223,17 +235,17 @@ function nb:api(url,fmt)
        then
                fct = function(r,i) 
                        if i == 1 and (fmt == "table" or fmt == "table-th") then
-                               ngx.print('<tr>')
+                               out('<tr>')
                                for k,v in pairs(r) do
-                                       ngx.print('<th class="' .. k .. '">' .. k .. '</th>')
+                                       out('<th class="' .. k .. '">' .. k .. '</th>')
                                end
-                               ngx.print('</tr>')
+                               out('</tr>')
                        end
-                       ngx.print('<tr>')
+                       out('<tr>')
                        for k,v in pairs(r) do
-                               ngx.print('<td>' .. v .. '</td>')
+                               out('<td>' .. v .. '</td>')
                        end
-                       ngx.print('<tr>\n')
+                       out('<tr>\n')
                end
 
        -- Default
@@ -244,22 +256,24 @@ function nb:api(url,fmt)
                        for k,v in pairs(r) do
                                i = i + 1
                                if i == 1 then
-                                       ngx.print(v)
+                                       out(v)
                                else
-                                       ngx.print(string.format(fmt,v))
+                                       out(string.format(fmt,v))
                                end
                        end
-                       ngx.print('\n')
+                       out('\n')
                end
 
        end
 
        -- Run
-       if fmt == "table" then ngx.print('<table>\n') end
+       if fmt == "table" then out('<table>\n') end
        for i,r in pairs(rows) do
                fct(r,i)
        end
-       if fmt == "table" then ngx.print('</table>\n') end
+       if fmt == "table" then out('</table>\n') end
+
+       return html
 end
 
 return nb
index a007aec5881437fce94b5887b839d74b81836b3e..0932b63639a6c8c9669ecf523792b1eb2fe38f25 100644 (file)
@@ -48,6 +48,7 @@ class DbQ extends nb {
        public $title = 'Dbq';
        public $sep_title = ' / ';
 
+       public $formats = [ 'html','div','csv','xml','json','yaml','sh','sql','php' ];
        public $format_cli = 'csv';
        public $format_html = 'table';
        public $format_html_ua_exp = '/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/';
@@ -74,7 +75,6 @@ class DbQ extends nb {
        #public $limits = [];
        public $default_limit = '30';
        public $limits = ['30','100','500','1000'];
-       public $formats = [ 'html','div','csv','xml','json','yaml','sh','sql','php' ];
        public $is_html;
 
        # Page
@@ -258,10 +258,9 @@ class DbQ extends nb {
 
                // Write output
                if ($this->expires and !preg_match('/^(dump|insert|update|replace|rm|vi)$/',$this->params['action'])) $this->page->expires = $this->expires;
-               #if ($this->is_html and $this->params['format'] != 'html') $this->_nopage = true;
+               $this->page->headers_no_cache();
                if (empty($this->_nopage)) {
 
-                       $this->page->headers_no_cache();
                        $this->page->js = $this->js;
 
                        if ($this->base) {
@@ -714,7 +713,7 @@ class DbQ extends nb {
                $this->uri = $path;
                $this->uri_params = explode($this->expode_args,$args);
 
-               if (preg_match('/\.(\w+)$/',$path,$m)) {
+               if (preg_match('/\.(\w+(\.html)?)$/',$path,$m)) {
                        $values[] = $m[1];
                        $path = substr($path,0,strlen($path)-strlen($m[1])-1);
                } else {
@@ -951,6 +950,14 @@ EOF;
 
                // Affect values to objects
                if ($format == 'html') $format = $this->format_html;
+               #bye($this->params['format']);
+#bye(preg_match('/^(.*?)\.html$/',$this->params['format']));
+               // Set _nopage
+               if (preg_match('/^(.*?)\.html$/',$this->format_html)) if (preg_match('/^(.*?)\.html$/',$format,$m)) {
+                       $format = $m[1];
+               } else {
+                       $this->_nopage = true;
+               }
                $this->format($format);
                $this->method_post_accept_decode();
                $this->is_html = strpos($content_type,'html') ? true : false;