]> git.nbdom.net Git - nb.git/commitdiff
optimize
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 29 Jun 2015 00:24:36 +0000 (01:24 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 29 Jun 2015 00:24:36 +0000 (01:24 +0100)
lib/css/db.css
lib/php/db.php
lib/php/default.php
lib/php/functions.php
lib/php/page.php

index 76d3f0c59ff1b61ce2cc42a59960466481046442..3ed06846a216154de7db0464a2d92330f3434a78 100644 (file)
@@ -38,12 +38,22 @@ form.db.edit .buttons input {
   border: solid 1px red;
 */
 .sel:before { content: ">"; }
+
+div.db.menu label {
+  margin-right: 0.1em;
+}
+div.db.menu label:after {
+  content: ": ";
+}
+
 .db.menu {
   float: left;
+  margin-right: 1em;
+}
+ul.db.menu {
   /*
   margin: -4em 0.4em 0 -7em;
   */
-  margin-right: 1em;
   max-width: 7em;
 }
 
@@ -64,3 +74,23 @@ td.email,
 td.phone {
   white-space: nowrap;
 }
+
+.button {
+       display: inline-block;
+       text-decoration: none;
+       font-size: 13px;
+       line-height: 26px;
+       height: 28px;
+       margin: 0;
+       padding: 0 10px 1px;
+       cursor: pointer;
+       border-width: 1px;
+       border-style: solid;
+       -webkit-appearance: none;
+       -webkit-border-radius: 3px;
+       border-radius: 3px;
+       white-space: nowrap;
+       -webkit-box-sizing: border-box;
+       -moz-box-sizing: border-box;
+       box-sizing: border-box;
+}
index 68773107cf431f9c1c6e24f1955a3178e8c5c6f6..dee9e6650c3664af2db358874b0b899b981258e0 100644 (file)
@@ -7,6 +7,7 @@
 
 *****************************************************************************/
 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')) {
@@ -200,6 +201,25 @@ EOF;
 
     $r = '';
 
+    $r .= '<div class="db menu center">'.PHP_EOL;
+    $r .= '<label>Tables</label>'
+      .'<select class="tables" onchange="if (this.value) location.href = \'?table=\'+this.value;">'
+    ;
+
+    foreach ($this->tables() as $table) {
+      #$count = $table->rowCount();
+      $r .= '<option value="'.$table.'"';
+      #$pretty = prettyText($table);
+      $r .= $table == @$_REQUEST['table'] ? ' selected="selected"' : '';
+      $r .= ">$table</option>".PHP_EOL;
+    }
+
+    $r .= '</select>'.PHP_EOL;
+
+    $r .= '</div>'.PHP_EOL;
+
+    return $r;
+
     $r .= '<ul class="db menu">'.PHP_EOL;
     $r .= '<li class="tables">Tables';
     $r .= '<ul>'.PHP_EOL;
@@ -301,7 +321,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
         $this->fields[$row['name']]['type'] = $row['type'];
 
-        if (array_key_exists('notnull',$row)) {
+        if (isset($row['notnull'])) {
           $this->fields[$row['name']]['null'] = $row['notnull'] == '0' ? 1 : 0;
 
         } else {
@@ -310,12 +330,12 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
         }
 
         foreach (array('dflt_value') as $f) {
-          if (!array_key_exists($f,$row)) continue;
+          if (!isset($row[$f])) continue;
           $this->fields[$row['name']]['default'] = $row[$f];
         }
 
         foreach (array('pk','Key') as $f) {
-          if (!array_key_exists($f,$row)) continue;
+          if (!isset($row[$f])) continue;
           $this->fields[$row['name']]['key'] = preg_match('/^1|yes|t/i',$row[$f]) ? 1 : 0;
         }
 
@@ -401,15 +421,15 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   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 "DEBUG: $msg\n";
+
+    if (!@$GLOBALS['PAGE']['is_text']) {
+      echo '<pre class="debug">'
+        .(@$_SERVER['HTTP_HOST'] ? htmlentities($msg) : $msg)
+      .'</pre>'.PHP_EOL;
       return;
     }
-    echo '<pre class="debug">'
-      .(@$_SERVER['HTTP_HOST'] ? htmlentities($msg) : $msg)
-    .'</pre>'.PHP_EOL;
+      echo "DEBUG: $msg\n";
 
   }
 
@@ -745,7 +765,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       }
 
       #debug($this->url_edit($row));
-      if ($format == 'table') array_unshift($row,'<a href="'.$this->url_edit($row,'&amp;').'">EDIT</a>');
+      if ($format == 'table') array_unshift($row,'<a class="edit button" href="'.$this->url_edit($row,'&amp;').'">'.DB_HTML_EDIT.'</a>');
       echo $this->{"rows_rec_$format"}($row);
 
     }
@@ -852,7 +872,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     $html = '<table class="'.$this->name.' rows border">'.PHP_EOL;
 
     $html .= '<tr class="'.$this->name.' row bold">';
-    $html .= '<th class="EDIT"></th>';
+    $html .= '<th class="'.DB_HTML_EDIT.'"></th>';
     foreach (array_keys($this->fields()) as $f) {
       $html .= '<th class="'.$f.'">'.$this->url_sort($f).'</th>';
     }
index c987e882dcae9e2793759a86332bc9ce8353da3f..c05ae91897accc0afadb4ce46a97d81db2005381 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-@define('DEBUG',(int)$_REQUEST['debug']);
+if (!defined('DEBUG')) define('DEBUG',(int)$_REQUEST['debug']);
 ini_set('include_path', realpath(dirname(__FILE__)).':'.ini_get('include_path'));
 
 require_once((dirname(__FILE__).'/functions.php'));
index cbd8bacbece4a87bba7aac26b5b38503b0f4bcd5..c306eed0d865ad1a28da2b4307256842214e084f 100644 (file)
@@ -1,7 +1,14 @@
 <?php
-@define('DEBUG',(int)$_REQUEST['debug']);
-require_once((dirname(__FILE__).'/default.php'));
-require_once(dirname(__FILE__).'/parsedown/Parsedown.php');
+/*
+Vim:
+
+s/array_key_exists(\([^,]\+\),\([^)]\+\))/isset(\2[\1])/g
+
+s/^define(\([^,]\+\)/if (!defined(\1)) define(\1/
+
+*/
+if (!defined('DEBUG')) define('DEBUG',(int)@$_REQUEST['debug']);
+// NB 28.06.15 require_once((dirname(__FILE__).'/default.php'));
 
 function file_write($file,$data,$mode='w') {
 
@@ -76,10 +83,36 @@ function ar_map($return,$array,$as_hash=false) {
 }
 
 function txt2md($txt) {
-  static $MD = null;
-  if ($MD === null) $MD = new Parsedown();
+  global $_txt2md;
+  if ($_txt2md === null) {
+    require_once(dirname(__FILE__).'/parsedown/Parsedown.php');
+    $_txt2md = new Parsedown();
+  }
+  return $_txt2md->text($txt);
+}
+
+function benchmark($function=null,$limit=1000000) {
+  global $_benchmark;
+
+  if ($_benchmark === null) {
+    $_benchmark = array('.' => microtime(true));
+  }
 
-  return $MD->text($txt);
+  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='') {
@@ -93,20 +126,20 @@ function warn ($msg) {
 
 function err($msg) {
   $msg = is_scalar($msg) ? $msg : print_r($msg,true);
-  echo( @$GLOBALS['PAGE']['is_text'] ? "ERR: $msg" : '<pre class="err">'.$msg.'</pre>').PHP_EOL;
+  echo( !@$GLOBALS['PAGE']['is_text'] ? '<pre class="err">'.$msg.'</pre>' : "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 (!@$GLOBALS['PAGE']['is_text']) {
+    echo '<pre class="debug">'
+      .(@$_SERVER['HTTP_HOST'] ? htmlentities($msg) : $msg)
+    .'</pre>'.PHP_EOL;
+  } else {
     echo "DEBUG: $msg\n";
-    return;
   }
-  echo '<pre class="debug">'
-    .(@$_SERVER['HTTP_HOST'] ? htmlentities($msg) : $msg)
-  .'</pre>'.PHP_EOL;
 
 }
 
index 669cbcc82900937265380d1b37ce918f2e209a50..d1b6186e72b65c279111355e9afb1d35708e167a 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+/*
 if (
   !@$_SERVER['DOCUMENT_ROOT'] and (realpath($argv[0]) == __FILE__)
 ) {
@@ -12,6 +13,7 @@ $Page = new Page(array(
   ),
 ));
 }
+*/
 
 class Page {
 
@@ -41,7 +43,7 @@ class Page {
     }
     
     // Defaults
-    if ( ! array_key_exists('title',$opt) )
+    if ( ! isset($opt['title']) )
       #$this->title = $GLOBALS['argv'][0]
       $this->title = $this->filename2title($_SERVER['SCRIPT_NAME']);
       #$this->title = preg_replace('@^.*?([^/\.]+)[^/]*@','\1',$_SERVER['SCRIPT_NAME']);