From: nboisselier Date: Wed, 1 Jul 2015 00:03:02 +0000 (+0100) Subject: improvment X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=446f5c15afae8b2dc8edb251bea8427b2df36cde;p=nb.git improvment --- diff --git a/lib/php/db.php b/lib/php/db.php index eba3955d..d01fba53 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -2,7 +2,7 @@ /***************************************************************************** Class DB - Render different output format ex: html, xml, yaml, csv + Render different output format example: html, xml, yaml, csv from any database *****************************************************************************/ @@ -10,12 +10,6 @@ require_once(dirname(__FILE__).'/functions.php'); if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit'); if (!defined('DEBUG')) define('DEBUG',0); -if (!function_exists('err')) { - function err($err) { - die( is_scalar($err) ? $err : print_r($err) ); - } -} - class db { var $tables = array(); @@ -166,11 +160,9 @@ EOF; } else { - @$GLOBALS['PAGE']['is_text'] = false; return false; } - @$GLOBALS['PAGE']['is_text'] = true; return true; } @@ -193,8 +185,6 @@ EOF; #$_REQUEST['op'] = strtoupper(@$_REQUEST['op']) == 'AND' ? 'AND' : 'OR'; $_REQUEST['op'] = strtoupper(@$_REQUEST['op']) == 'OR' ? 'OR' : 'AND'; - @$GLOBALS['PAGE']['js_code'] = 'var sem_ui_tables = ['.join(',',ar_map('str_quote($a)',$tables)).']; var sem_ui_table = '.str_quote(@$_REQUEST['table']).';'; - } function html_menu() { @@ -281,7 +271,64 @@ class table { return $this->fields(); } - function fields() { + /* + * Function db.page + * + * print any pages according to params + * + * @param (array) + */ + function page($Page,$param=null) { + if ($param === null) $param = $_REQUEST; + elseif (!is_array($param)) $param = array('name' => $param); + + foreach (array( + 'action' => 'list', + ) as $k => $v) { + } + + $Page->title = prettyText($this->name); + + #if ($Page->is('html')) $this->form_criterias(); + + if (@$_REQUEST['format']) $this->print_header($_REQUEST['format']); + if (@$_REQUEST['format']) { + $this->rows(array('format' => @$_REQUEST['format'])); + return 0; + } + + #die($Page->content_type); + $Page->begin(); + echo '
'.PHP_EOL; + echo '
'.PHP_EOL; + #$Page->debug($this->tables()); + + + echo $this->html_menu(); + + /* + Rows + */ + if (@$this) $this->rows(array( + 'format' => @$_REQUEST['format'], + )); + + // END + echo '
'.PHP_EOL; + #$Page->out($Db->query2v("SELECT 1")); + $Page->end(); + return 0; + } + + /* + * Function db.fields + * + * return all or one fields from a table + * + * @name (string) name of the field to return. Default: null + * @return (array) return null where name does not exsts + */ + function fields($name=null) { if (!$this->fields) { @@ -343,6 +390,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } + if ($name !== null ) { + if (!isset($this->fields[$name])) return null; + return $this->fields[$name]; + } + return $this->fields; } @@ -419,28 +471,13 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $this; } - function debug($msg,$level=0) { - if ($level and $level>DEBUG) return; - $msg = is_scalar($msg) ? $msg : print_r($msg,true); - - if (!@$GLOBALS['PAGE']['is_text']) { - echo '
'
-        .(@$_SERVER['HTTP_HOST'] ? htmlentities($msg) : $msg)
-      .'
'.PHP_EOL; - return; - } - echo "DEBUG: $msg\n"; - - } + function debug($msg,$level=0) { return debug($msg,$level); } function url_params($k='',$v='') { $params = array(); - foreach (array_merge( - $this->params, - array_keys($this->fields()) - ) as $f) { + foreach (array_merge( $this->params, array_keys($this->fields()) ) as $f) { if (@strcmp($_REQUEST[$f],'')==0) continue; $params[$f] = $_REQUEST[$f]; diff --git a/lib/php/functions.php b/lib/php/functions.php index c306eed0..9dea64b3 100644 --- a/lib/php/functions.php +++ b/lib/php/functions.php @@ -91,30 +91,6 @@ function txt2md($txt) { return $_txt2md->text($txt); } -function benchmark($function=null,$limit=1000000) { - global $_benchmark; - - if ($_benchmark === null) { - $_benchmark = array('.' => microtime(true)); - } - - if ($function === null) { - $prev = $_benchmark['.']; - foreach ($_benchmark as $lib => $sec) { - if ($lib === '.') continue; - printf("%-30s %s\n",$lib,($sec-$prev)); - $prev = $sec; - } - return; - } - - for ($i=0; $i<$limit; $i++) { - $function(); - } - - $_benchmark[$function] = microtime(true); -} - function bye($msg='') { if ($msg) err($msg); exit; @@ -126,19 +102,21 @@ function warn ($msg) { function err($msg) { $msg = is_scalar($msg) ? $msg : print_r($msg,true); - echo( !@$GLOBALS['PAGE']['is_text'] ? '
'.$msg.'
' : "ERR: $msg").PHP_EOL; + echo( preg_match('/ml/i',get_header('Content-type')) ? '
'.$msg.'
' : "ERR: $msg").PHP_EOL; } function debug($msg,$level=0) { if ($level and $level>DEBUG) return; - $msg = is_scalar($msg) ? $msg : print_r($msg,true); - if (!@$GLOBALS['PAGE']['is_text']) { + + if (preg_match('/ml/i',get_header('Content-type'))) { echo '
'
       .(@$_SERVER['HTTP_HOST'] ? htmlentities($msg) : $msg)
     .'
'.PHP_EOL; + } else { echo "DEBUG: $msg\n"; + } } @@ -242,4 +220,38 @@ function argv2request() { return $new_argv; } + +function benchmark($function=null,$limit=1000000) { + global $_benchmark; + + if ($_benchmark === null) { + $_benchmark = array('.' => microtime(true)); + } + + if ($function === null) { + $prev = $_benchmark['.']; + echo "Iteration: $limit\n"; + foreach ($_benchmark as $lib => $sec) { + if ($lib === '.') continue; + printf("%-30s %s\n",$lib,($sec-$prev)); + $prev = $sec; + } + return; + } + + for ($i=0; $i<$limit; $i++) { + $function(); + } + + $_benchmark[$function] = microtime(true); +} + +function get_header($name,$value=null) { + $v = array_map(create_function('$a', + #'return $a;' + 'return( preg_match("/^'.$name.':\s+(\S+)/i",$a,$m) ? $m[1] : null );' + ),headers_list()); + + return $v ? $v[0] : null; +} ?> diff --git a/lib/php/page.php b/lib/php/page.php index d973db83..9d279a67 100644 --- a/lib/php/page.php +++ b/lib/php/page.php @@ -211,6 +211,21 @@ class Page { } + /* + * Function client_content_type + * Return the head Accept from the client + */ + function client_content_type() { + echo split(',', getallheaders()['Accept'])[0]; + } + + function header($name,$value=null) { + + if ($value === null) return get_header($name); + header("$name: $value"); + + } + function headers() { #header('Content-type: ' . $this->content_type);