]> git.joonet.de Git - adminer.git/commitdiff
Function minification
authorjakubvrana <jakubvrana@7c3ca157-0c34-0410-bff1-cbf682f78f5c>
Wed, 15 Apr 2009 14:31:05 +0000 (14:31 +0000)
committerjakubvrana <jakubvrana@7c3ca157-0c34-0410-bff1-cbf682f78f5c>
Wed, 15 Apr 2009 14:31:05 +0000 (14:31 +0000)
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@581 7c3ca157-0c34-0410-bff1-cbf682f78f5c

_compile.php
abstraction.inc.php
changes.txt
todo.txt

index 72e8899e45ff9d9e82cb5772ae58ce15009210fc..b8976f5c98a66d79fe1c4e2f38ed961dce8d7626 100644 (file)
@@ -65,9 +65,8 @@ function put_file($match) {
        return $return;
 }
 
-function short_variable($number) {
-       $chars = implode("", range('a', 'z')) . '_' . implode("", range('A', 'Z')); // could use also numbers and \x7f-\xff
-       $return = '$';
+function short_identifier($number, $chars) {
+       $return = '';
        while ($number >= 0) {
                $return .= $chars{$number % strlen($chars)};
                $number = floor($number / strlen($chars)) - 1;
@@ -75,16 +74,35 @@ function short_variable($number) {
        return $return;
 }
 
-// Dgx's PHP shrinker
+// Based on Dgx's PHP shrinker
 function php_shrink($input) {
        $special_variables = array_flip(array('$TOKENS', '$this', '$GLOBALS', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_SERVER'));
        static $short_variables = array();
        $shortening = true;
+       $special_functions = array_flip(array('Min_MySQLi', 'Min_MySQLResult', 'normalize_enum', '__construct'));
+       static $short_functions = array();
+       $tokens = token_get_all($input);
+       
+       foreach ($tokens as $i => $token) {
+               if ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
+                       $short_variables[$token[1]]++;
+               } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && !is_callable($token[1]) && !isset($special_functions[$token[1]])) {
+                       $short_functions[$token[1]]++;
+               }
+       }
+       arsort($short_variables);
+       foreach (array_keys($short_variables) as $number => $key) {
+               $short_variables[$key] = short_identifier($number, implode("", range('a', 'z')) . '_' . implode("", range('A', 'Z'))); // could use also numbers and \x7f-\xff
+       }
+       arsort($short_functions);
+       foreach (array_keys($short_functions) as $number => $key) {
+               $short_functions[$key] = short_identifier($number, implode("", range('a', 'z')) . '_');
+       }
        
        $set = array_flip(preg_split('//', '!"#$&\'()*+,-./:;<=>?@[\]^`{|}'));
        $space = '';
        $output = '';
-       foreach (token_get_all($input) as $token) {
+       foreach ($tokens as $i => $token) {
                if (!is_array($token)) {
                        $token = array(0, $token);
                }
@@ -97,11 +115,14 @@ function php_shrink($input) {
                                if ($token[1] == ';') {
                                        $shortening = true;
                                }
-                       } elseif ($token[0] == T_VARIABLE && !isset($special_variables[$token[1]])) {
-                               if (!isset($short_variables[$token[1]])) {
-                                       $short_variables[$token[1]] = short_variable(count($short_variables));
-                               }
-                               $token[1] = $short_variables[$token[1]];
+                       } elseif ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
+                               $token[1] = '$' . $short_variables[$token[1]];
+                       } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && !is_callable($token[1]) && !isset($special_functions[$token[1]])
+                       && $tokens[$i-1][0] !== T_DOUBLE_COLON && $tokens[$i-2][0] !== T_NEW && !in_array($tokens[$i-2][1], array('_result', '$_result'), true)
+                       ) {
+                               $token[1] = $short_functions[$token[1]];
+                       } elseif ($token[0] == T_CONSTANT_ENCAPSED_STRING && $tokens[$i-1] === '(' && in_array($tokens[$i-2][1], array('array_map', 'set_exception_handler'), true) && isset($short_functions[substr($token[1], 1, -1)])) {
+                               $token[1] = "'" . $short_functions[substr($token[1], 1, -1)] . "'";
                        }
                        if (isset($set[substr($output, -1)]) || isset($set[$token[1]{0}])) {
                                $space = '';
index a0a60b6687500a98709c542c0446af43f96d3eda..24a9633be84627c0d84ad103e9bbaa64678c5517 100644 (file)
@@ -4,12 +4,12 @@ if (extension_loaded("mysqli")) {
                var $extension = "MySQLi";
                
                function Min_MySQLi() {
-                       $this->init();
+                       parent::init();
                }
                
                function connect($server, $username, $password) {
                        list($host, $port) = explode(":", $server, 2);
-                       return @$this->real_connect(
+                       return @parent::real_connect(
                                (strlen($server) ? $host : ini_get("mysqli.default_host")),
                                (strlen("$server$username") ? $username : ini_get("mysqli.default_user")),
                                (strlen("$server$username$password") ? $password : ini_get("mysqli.default_pw")),
@@ -22,9 +22,62 @@ if (extension_loaded("mysqli")) {
                        if (!$result) {
                                return false;
                        }
-                       $row = $result->fetch_array();
+                       $row = $result->_result->fetch_array();
                        return $row[$field];
                }
+               
+               // minification compatibility start
+               function select_db($database) {
+                       return parent::select_db($database);
+               }
+               
+               function query($query) {
+                       $result = parent::query($query);
+                       return (is_object($result) ? new Min_MySQLiResult($result) : $result);
+               }
+               
+               function multi_query($query) {
+                       return parent::multi_query($query);
+               }
+               
+               function store_result() {
+                       $result = parent::store_result();
+                       return (is_object($result) ? new Min_MySQLiResult($result) : $result);
+               }
+               
+               function next_result() {
+                       return parent::next_result();
+               }
+               
+               function escape_string($string) {
+                       return parent::escape_string($string);
+               }
+       }
+       
+       class Min_MySQLiResult {
+               var $_result, $num_rows;
+               
+               function __construct($result) {
+                       $this->_result = $result;
+                       $this->num_rows = $result->num_rows;
+               }
+               
+               function fetch_assoc() {
+                       return $this->_result->fetch_assoc();
+               }
+               
+               function fetch_row() {
+                       return $this->_result->fetch_row();
+               }
+               
+               function fetch_field() {
+                       return $this->_result->fetch_field();
+               }
+               
+               function free() {
+                       return $this->_result->free();
+               }
+               // minification compatibility end
        }
        
        $mysql = new Min_MySQLi;
@@ -128,7 +181,7 @@ if (extension_loaded("mysqli")) {
                        set_exception_handler('auth_error'); // try/catch is not compatible with PHP 4
                        parent::__construct("mysql:host=" . str_replace(":", ";port=", $server), $username, $password);
                        restore_exception_handler();
-                       $this->setAttribute(13, array('Min_PDOStatement')); // PDO::ATTR_STATEMENT_CLASS
+                       parent::setAttribute(13, array('Min_PDOStatement')); // PDO::ATTR_STATEMENT_CLASS
                        $this->server_info = $this->result($this->query("SELECT VERSION()"));
                        return true;
                }
@@ -138,19 +191,19 @@ if (extension_loaded("mysqli")) {
                }
                
                function query($query) {
-                       $result = parent::query($query);
-                       if (!$result) {
-                               $errorInfo = $this->errorInfo();
+                       $_result = parent::query($query);
+                       if (!$_result) {
+                               $errorInfo = parent::errorInfo();
                                $this->error = $errorInfo[2];
                                return false;
                        }
-                       $this->_result = $result;
-                       if (!$result->columnCount()) {
-                               $this->affected_rows = $result->rowCount();
+                       $this->_result = $_result;
+                       if (!$_result->columnCount()) {
+                               $this->affected_rows = $_result->rowCount();
                                return true;
                        }
-                       $result->num_rows = $result->rowCount();
-                       return $result;
+                       $_result->num_rows = $_result->rowCount();
+                       return $_result;
                }
                
                function multi_query($query) {
@@ -165,16 +218,16 @@ if (extension_loaded("mysqli")) {
                        return $this->_result->nextRowset();
                }
                
-               function result($result, $field = 0) {
-                       if (!$result) {
+               function result($_result, $field = 0) {
+                       if (!$_result) {
                                return false;
                        }
-                       $row = $result->fetch();
+                       $row = $_result->fetch();
                        return $row[$field];
                }
                
                function escape_string($string) {
-                       return substr($this->quote($string), 1, -1);
+                       return substr(parent::quote($string), 1, -1);
                }
        }
        
@@ -182,15 +235,15 @@ if (extension_loaded("mysqli")) {
                var $_offset = 0, $num_rows;
                
                function fetch_assoc() {
-                       return $this->fetch(2); // PDO::FETCH_ASSOC
+                       return parent::fetch(2); // PDO::FETCH_ASSOC
                }
                
                function fetch_row() {
-                       return $this->fetch(3); // PDO::FETCH_NUM
+                       return parent::fetch(3); // PDO::FETCH_NUM
                }
                
                function fetch_field() {
-                       $row = (object) $this->getColumnMeta($this->_offset++);
+                       $row = (object) parent::getColumnMeta($this->_offset++);
                        $row->orgtable = $row->table;
                        $row->orgname = $row->name;
                        $row->charsetnr = (in_array("blob", $row->flags) ? 63 : 0);
index 6697bd60317cff635a8c3147782817c788f9659e..0f55acd86cb0a98c74dce74d35845b6aa4d05ce5 100644 (file)
@@ -7,7 +7,7 @@ Cross links to select and table (bug #2236232), link new item
 Suhosin compatibility (thanks to Klemens Hackel)
 Remove max_allowed_packet from export
 Read style from phpMinAdmin.css if exists
-Size reduction by minification of variables
+Size reduction by minification of variables and functions
 
 phpMinAdmin 1.9.1:
 Update translations
index be6a2a8f62152c4eaf7f4437ec76138e7cf79394..c7568be2be4edbb12d9a771028574292fbc99216 100644 (file)
--- a/todo.txt
+++ b/todo.txt
@@ -5,7 +5,7 @@ MySQL 5 BIT data type
 Transactions in export
 Compress export and import
 Create view and routine options
-Import CSV
+SQL queries history - utilize in edit link in .message and .error
 Function to fix database encoding - http://php.vrana.cz/prevod-kodovani-mysql.php
 ? Execution time in sql.inc.php
 ? Save token also to cookie - for session expiration and login in other window