Function minification

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@581 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
jakubvrana 2009-04-15 14:31:05 +00:00
parent 9b9379d628
commit 16f421020b
4 changed files with 105 additions and 31 deletions

View 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 = '';

View 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);

View 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

View file

@ -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