diff --git a/_compile.php b/_compile.php index b8976f5c..29118193 100644 --- a/_compile.php +++ b/_compile.php @@ -79,24 +79,33 @@ 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')); + $special_functions = array_flip(array('Min_MySQLi', 'Min_MySQLResult', '__construct')); + $defined_functions = array(); 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]])) { + } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && !isset($special_functions[$token[1]])) { $short_functions[$token[1]]++; + if ($tokens[$i-2][0] === T_FUNCTION) { + $defined_functions[$token[1]] = true; + } } } + 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')) . '_'); + $number = 0; + foreach ($short_functions as $key => $val) { + if (isset($defined_functions[$key])) { + $short_functions[$key] = short_identifier($number, implode("", range('a', 'z')) . '_'); + $number++; + } } $set = array_flip(preg_split('//', '!"#$&\'()*+,-./:;<=>?@[\]^`{|}')); @@ -117,11 +126,11 @@ function php_shrink($input) { } } 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]]) + } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && isset($defined_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)])) { + } elseif ($token[0] == T_CONSTANT_ENCAPSED_STRING && (($tokens[$i-1] === '(' && in_array($tokens[$i-2][1], array('array_map', 'set_exception_handler'), true)) || $token[1] == "'normalize_enum'") && isset($defined_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}])) { diff --git a/abstraction.inc.php b/abstraction.inc.php index 24a9633b..3824e0d8 100644 --- a/abstraction.inc.php +++ b/abstraction.inc.php @@ -4,12 +4,12 @@ if (extension_loaded("mysqli")) { var $extension = "MySQLi"; function Min_MySQLi() { - parent::init(); + $this->init(); } function connect($server, $username, $password) { list($host, $port) = explode(":", $server, 2); - return @parent::real_connect( + return @$this->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")), @@ -181,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(); - parent::setAttribute(13, array('Min_PDOStatement')); // PDO::ATTR_STATEMENT_CLASS + $this->setAttribute(13, array('Min_PDOStatement')); // PDO::ATTR_STATEMENT_CLASS $this->server_info = $this->result($this->query("SELECT VERSION()")); return true; } @@ -193,7 +193,7 @@ if (extension_loaded("mysqli")) { function query($query) { $_result = parent::query($query); if (!$_result) { - $errorInfo = parent::errorInfo(); + $errorInfo = $this->errorInfo(); $this->error = $errorInfo[2]; return false; } @@ -227,7 +227,7 @@ if (extension_loaded("mysqli")) { } function escape_string($string) { - return substr(parent::quote($string), 1, -1); + return substr($this->quote($string), 1, -1); } } @@ -235,15 +235,15 @@ if (extension_loaded("mysqli")) { var $_offset = 0, $num_rows; function fetch_assoc() { - return parent::fetch(2); // PDO::FETCH_ASSOC + return $this->fetch(2); // PDO::FETCH_ASSOC } function fetch_row() { - return parent::fetch(3); // PDO::FETCH_NUM + return $this->fetch(3); // PDO::FETCH_NUM } function fetch_field() { - $row = (object) parent::getColumnMeta($this->_offset++); + $row = (object) $this->getColumnMeta($this->_offset++); $row->orgtable = $row->table; $row->orgname = $row->name; $row->charsetnr = (in_array("blob", $row->flags) ? 63 : 0); diff --git a/todo.txt b/todo.txt index c7568be2..e5e84070 100644 --- a/todo.txt +++ b/todo.txt @@ -10,3 +10,4 @@ Function to fix database encoding - http://php.vrana.cz/prevod-kodovani-mysql.ph ? Execution time in sql.inc.php ? Save token also to cookie - for session expiration and login in other window ? Save uploaded files after error to session variable instead of hidden field +? Aliasing of built-in functions can save 7 KB, substitution of $_GET and friends can save 2 KB, remove of base64_decode() + using chars 127-255 in minification can save 1 KB