public $default_table;
public $sort;
public $extras;
+ public $format = 'json';
public $formats = [ 'table','div','csv','xml','json','yaml','sh' ];
public $limits = ['10','20','50','100','500','1000'];
public function out($rows,$head=[]) {
- if ($this->p('format') == 'null') return true;
+ if ($this->format) {
+ $format = $this->format;
+
+ } elseif ($this->p('format') == 'null') {
+ return true;
+
+ } elseif ($format=$this->p('format')) {
+
+ } else {
+ # NB 04.12.16: TODEL one day !
+ $format = $this->out->php_cli() ? 'csv' : 'table';
+ }
+
// Handle limit
if ($this->p('limit')) {
}
- $this->out->rows($this->p('format',$this->out->php_cli() ? 'csv' : 'table'),$rows,$head);
+ $this->out->rows($format,$rows,$head);
return true;
}
# Db
#
$Db = new self();
- if (empty($Db)) return false;
#
# Params
}
public function db() {
- if (!empty($this->table) and !empty($this->table->db)) return $this->table->db();
+ if (!empty($this->table) and !empty($this->table->db())) return $this->table->db();
global $Db;
return ( isset($Db) ? $Db : null );
}
<?php
-require_once(realpath(dirname(__FILE__).'/../config.php'));
+# NB 04.12.16 require_once(realpath(dirname(__FILE__).'/../config.php'));
# NB 04.12.16 require_once(realpath(dirname(__FILE__).'/../db.php'));
require_once(realpath(dirname(__FILE__).'/config.php'));
} else {
$format = empty($opt['format']) ? $this->p('format') : $opt['format'];
- if (!$format) bye("Parameter `format` missing!");
+ if (!$format and $this->db()->format) {
+ $format = $this->db()->format;
+ #$this->pset('format',$format);
+ }
+ if (!$format) $this->bye("Parameter `format` missing!");
}
list($sql,$where,$limit,$select_count) = $this->rows_sql($opt);
if (empty($key)) {
if (!self::p($k)) continue;
+ } elseif(is_array($key)) {
+ if (!in_array($k,$key)) continue;
} else {
if ($k!=$key) continue;
}
if (is_array($this->status[$k])) $this->status[$k] = count($this->status[$k]);
}
- if (!empty($key)) return ( empty($this->status[$key]) ? '' : $this->status[$key] );
+ if (!empty($key) and is_scalar($key)) return ( empty($this->status[$key]) ? '' : $this->status[$key] );
return $this->status;
}
function bye($msg='__bye__',$backtrace_deep=0) {
#if ($msg) err($msg,'bye',$backtrace_deep === 0 ? 1 : $backtrace_deep);
+
if ($msg!=='__bye__') {
#throw new Exception($msg);
err($msg,'bye',( $backtrace_deep !== false ? (1+-1+$backtrace_deep) : $backtrace_deep ));
exit(1);
}
+
#if ($msg) err($msg,'bye',1+$backtrace_deep);
exit;
#die("Can't exit from BYE!!!");
function err($msg='__err__',$preff='err',$backtrace_deep=0) {
$msg = is_scalar($msg) ? $msg : print_r($msg,true);
$preff_msg = $preff ? strtoupper($preff).': ' : '';
+
if ($msg !== '__err__' and $backtrace_deep !== false) {
$msg = trim($preff_msg.$msg).' '
.nb::debug_backtrace_msg(1+$backtrace_deep,NULL,$preff_msg)
#.nb::debug_backtrace_msg(1+$backtrace_deep,NULL)
;
+
} else {
$msg = $preff_msg.$msg;
+
}
#nb::msg( !nb::php_cli()
protected static $content_types_aliases = array(
'text' => 'txt',
'human' => 'txt',
+ 'table' => 'html',
+ 'div' => 'html',
);
protected static $content_types = array(
E_DEPRECATED => 'E_DEPRECATED ERROR',
E_USER_DEPRECATED => 'E_USER_DEPRECATED ERROR',
);
- if (isset($call_info['type']) and isset($errorType[$call_info['type']])) $call_info['type'] = $errorType[$call_info['type']].'='.$call_info['type'];
+ if (isset($call_info['type'])
+ and isset($errorType[$call_info['type']])
+ ) $call_info['type'] = $errorType[$call_info['type']].'='.$call_info['type'];
#else ($call_info['type']) $call_info['type'] = $call_info['type'];
// Default values
;
// Get infos
- $msg = array();
- foreach (array(
+ $msg = [];
+ foreach ([
'file',
'line',
'function',
'type',
'message',
'code',
- ) as $i) {
+ ] as $i) {
- if (array_key_exists($i,$call_info)) $msg[] = "$i=".$call_info[$i];
+ if (!array_key_exists($i,$call_info)) continue;
+ if ($i == 'type' and preg_match('/^\W\W/',$call_info[$i])) continue;
+ $msg[] = "$i=".$call_info[$i];
}
* Function: tag
* Return html code for a tag
*/
+ public static function h1($content='',$attrs='') { return self::tag('h1',$content,$attrs); }
+ public static function div($content='',$attrs='') { return self::tag('div',$content,$attrs); }
+ public static function pp($content='',$attrs='') { return self::tag('p',$content,$attrs); }
public static function tag($tag,$content='',$attrs='') {
static $is_html = null;
if ($is_html === null) $is_html = preg_match('/ml$/',self::content_type()) ? true : false;
if ($is == 'xhtml') return preg_match('/xhtml$/',self::$content_type);
if ($is == 'html') return preg_match('/html$/',self::$content_type);
if ($is == 'xml') return preg_match('/xml$/',self::$content_type);
+ if ($is == 'text') return preg_match('/^text\//',self::$content_type);
die("Db->is(): unknow argument '$is'. Accepted values are xhtml, html, xml");
}
<?php
+# Make it work in php_cli
if (empty($_SERVER['DOCUMENT_ROOT'])) $_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);
-if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = '';
-function parse_uri() {
- $path = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']);
- $values = explode('/',trim($path,'/'));
-
- $P = [
- 'db' => '',
- 'table' => '',
- 'action' => '',
- 'format' => '',
- ];
+#var_export([$_SERVER['REQUEST_URI']]);
+if (empty($_SERVER['SERVER_PROTOCOL'])) $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
+
+function run() {
+ $params = parse_uri([
+ 'db' => 'ls',
+ 'table' => 'ls',
+ 'action' => 'ls',
+ 'format' => 'human',
+ ]);
+
+ if (!$params['db']) not_implemented();
+
+ #
+ # Page
+ require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/page.php');
+ $Page = new Page();
+ $Page->content_type($Page->ext2mime($params['format']) ? $Page->ext2mime($params['format']) : 'text/plain');
+ #$Page->content_type('text/plain');
+ #if ($Page->is('text') and !$Page->is('html')) $Page->content_type('text/plain');
+ $Page->headers_no_cache();
+ $Page->begin();
+
+ #
+ # Db Init
+ global $DB_CONFS;
+ require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/db/config.php');
+ #if (empty($DB_CONFS[$params['db']])) not_implemented();
+ #$Db = new Db($DB_CONFS[$params['db']]+['conf'=>$DB_CONFS]);
+ $Db = new Db(['conf'=>$DB_CONFS]);
+ $Db->format = $params['format'];
+
+ #
+ # Db actions pre conn
+ if ($params['db'] == 'help') {
+ #$this->out->rows($format,$rows,$head);
+ $Db->out([
+ [ 'help', 'This help' ],
+ [ 'ls', 'List databases' ],
+ ],['command','description']);
+ return $Page->end();
+
+ } elseif ($params['db'] == 'ls') {
+ $Db->out($Db->ls());
+ return $Page->end();
+
+ }
+
+ #
+ # Connections post conn
+ if (empty($DB_CONFS[$params['db']])) not_implemented();
+ $Db->__construct($DB_CONFS[$params['db']]);
+ $Db->connect();
+
+ #
+ # Db actions
+ if ($params['table'] == 'help') {
+ #$this->out->rows($format,$rows,$head);
+ $Db->out([
+ [ 'help', 'This help' ],
+ [ 'ls', 'List tables' ],
+ ],['command','description']);
+ return $Page->end();
+
+ } elseif ($params['table'] == 'ls') {
+ $Db->action('db.tables');
+ return $Page->end();
+
+ } elseif ($params['table'] == 'status') {
+ $Db->out($Db->status());
+ return $Page->end();
+
+ } elseif ($params['table'] == 'databases') {
+ $Db->out($Db->databases());
+ return $Page->end();
+
+ }
+
+ #
+ # Table Init
+ $Table = $Db->table($params['table']);
+ if (!$Table) not_implemented();
+
+ #
+ # Table action
+ if ($params['action'] == 'help') {
+ #$this->out->rows($format,$rows,$head);
+ $Db->out([
+ [ 'help', 'This help' ],
+ [ 'ls', 'List records' ],
+ ],['command','description']);
+ return $Page->end();
+
+ } elseif ($params['action'] == 'ls') {
+ $Table->rows();
+ return $Page->end();
+
+ } elseif ($params['action'] == 'status') {
+ $Db->out($Table->status()+$Table->status(['fields','sql']));
+ return $Page->end();
+
+ } elseif ($params['action'] == 'fields') {
+ $Table->action('table.fields');
+ return $Page->end();
+
+ }
+ return false;
+}
+
+function parse_uri($params=[]) {
+ if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = join('&',count($argv)>0 ? array_slice($argv,1) : []);
+ $path = trim(preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']),'/');
+ $values = $path ? explode('/',$path) : [];
+#var_export($values);
$i=0;
$count = count($values);
- foreach ($P as $p => $default) {
+ foreach ($params as $p => $default) {
if ($i>=$count) break;
- $P[$p] = $values[$i];
+ $params[$p] = $values[$i];
$i++;
}
+ return $params;
}
-#var_dump(parse_uri());
+run();
+exit;
$params = [
'db',
'format',
];
if (false and strpos($_SERVER['REQUEST_URI'],'?')!==false) {
- #return http_response_code(501);
- $msg = '501 Not Implemented';
- header($_SERVER['SERVER_PROTOCOL'].' '.$msg);
- echo "$msg\n";
- return;
+ return not_implemented();
}
$_SERVER['REQUEST_URI'] = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']);
$values = explode('/',trim($_SERVER['REQUEST_URI'],'/'));
#debug($_SERVER['REQUEST_URI']);
#debug($_SERVER['QUERY_STRING']);
#debug($_SERVER);
+
+function not_implemented() {
+ #return http_response_code(501);
+ $msg = '501 Not Implemented';
+ header($_SERVER['SERVER_PROTOCOL'].' '.$msg);
+ echo "$msg\n";
+ exit;
+}
?>