From: Nicolas Boisselier Date: Fri, 25 May 2018 14:24:44 +0000 (+0100) Subject: www/dbq/dbq.php X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=a2c9c6f5de038e1cd410efe9bda01625133da11b;p=nb.git www/dbq/dbq.php --- diff --git a/lib/lua/nb.lua b/lib/lua/nb.lua index f434e900..052d631f 100644 --- a/lib/lua/nb.lua +++ b/lib/lua/nb.lua @@ -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('') + out('') for k,v in pairs(r) do - ngx.print('' .. k .. '') + out('' .. k .. '') end - ngx.print('') + out('') end - ngx.print('') + out('') for k,v in pairs(r) do - ngx.print('' .. v .. '') + out('' .. v .. '') end - ngx.print('\n') + out('\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('\n') end + if fmt == "table" then out('
\n') end for i,r in pairs(rows) do fct(r,i) end - if fmt == "table" then ngx.print('
\n') end + if fmt == "table" then out('\n') end + + return html end return nb diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index a007aec5..0932b636 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -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;