From 5d257861e199d651967ff6779189ff989849982a Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Sun, 2 Sep 2012 06:44:32 -0700 Subject: [PATCH] Compress translations --- adminer/include/lang.inc.php | 37 ++++++++++++++++++++++++ changes.txt | 1 + compile.php | 54 ++++++++++++++++++++++++++++++------ 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/adminer/include/lang.inc.php b/adminer/include/lang.inc.php index c6fd7df2..760c3639 100644 --- a/adminer/include/lang.inc.php +++ b/adminer/include/lang.inc.php @@ -78,6 +78,43 @@ function switch_lang() { echo "\n\n"; } +function lzw_decompress($binary) { + // convert binary string to codes + $dictionary_count = 256; + $bits = 8; // ceil(log($dictionary_count, 2)) + $codes = array(); + $rest = 0; + $rest_length = 0; + for ($i=0; $i < strlen($binary); $i++) { + $rest = ($rest << 8) + ord($binary[$i]); + $rest_length += 8; + if ($rest_length >= $bits) { + $rest_length -= $bits; + $codes[] = $rest >> $rest_length; + $rest &= (1 << $rest_length) - 1; + $dictionary_count++; + if ($dictionary_count >> $bits) { + $bits++; + } + } + } + // decompression + $dictionary = range("\0", "\xFF"); + $return = ""; + foreach ($codes as $i => $code) { + $element = $dictionary[$code]; + if (!isset($element)) { + $element = $word . $word[0]; + } + $return .= $element; + if ($i) { + $dictionary[] = $word . $element[0]; + } + $word = $element; + } + return $return; +} + if (isset($_POST["lang"]) && $_SESSION["token"] == $_POST["token"]) { // $token and $error not yet available cookie("adminer_lang", $_POST["lang"]); $_SESSION["lang"] = $_POST["lang"]; // cookies may be disabled diff --git a/changes.txt b/changes.txt index d34c8418..f8e1a61d 100644 --- a/changes.txt +++ b/changes.txt @@ -6,6 +6,7 @@ Use VALUES() in INSERT+UPDATE export Style logout button as link Ctrl+click and Shift+click on button opens form to a blank window Switch language by POST +Compress translations selectQueryBuild() method (customization) Serbian translation diff --git a/compile.php b/compile.php index b3ca074d..8ebb22c9 100755 --- a/compile.php +++ b/compile.php @@ -61,12 +61,49 @@ function lang(\$translation, \$number) { } } +function lzw_compress($string) { + // compression + $dictionary = array_flip(range("\0", "\xFF")); + $word = ""; + $codes = array(); + for ($i=0; $i <= strlen($string); $i++) { + $x = $string[$i]; + if (strlen($x) && isset($dictionary[$word . $x])) { + $word .= $x; + } elseif ($i) { + $codes[] = $dictionary[$word]; + $dictionary[$word . $x] = count($dictionary); + $word = $x; + } + } + // convert codes to binary string + $dictionary_count = 256; + $bits = 8; // ceil(log($dictionary_count, 2)) + $return = ""; + $rest = 0; + $rest_length = 0; + foreach ($codes as $code) { + $rest = ($rest << $bits) + $code; + $rest_length += $bits; + $dictionary_count++; + if ($dictionary_count >> $bits) { + $bits++; + } + while ($rest_length > 7) { + $rest_length -= 8; + $return .= chr($rest >> $rest_length); + $rest &= (1 << $rest_length) - 1; + } + } + return $return . ($rest_length ? chr($rest << (8 - $rest_length)) : ""); +} + function put_file_lang($match) { global $lang_ids, $project, $langs; if ($_SESSION["lang"]) { return ""; } - $return = ""; + $all_translations = array(); foreach ($langs as $lang => $val) { include dirname(__FILE__) . "/adminer/lang/$lang.inc.php"; // assign $translations $translation_ids = array_flip($lang_ids); // default translation @@ -75,13 +112,14 @@ function put_file_lang($match) { $translation_ids[$lang_ids[$key]] = $val; } } - $return .= "\tcase \"$lang\": \$translations = array("; - foreach ($translation_ids as $val) { - $return .= (is_array($val) ? "array('" . implode("', '", array_map('add_apo_slashes', $val)) . "')" : "'" . add_apo_slashes($val) . "'") . ", "; - } - $return = substr($return, 0, -2) . "); break;\n"; + $all_translations[$lang] = $translation_ids; } - return "switch (\$LANG) {\n$return}\n"; + return '$translations = &$_SESSION["translations"]; +if ($_GET["lang"] || !$translations) { + $all_translations = unserialize(lzw_decompress(\'' . add_apo_slashes(lzw_compress(serialize($all_translations))) . '\')); + $translations = $all_translations[$LANG]; +} +'; } function short_identifier($number, $chars) { @@ -281,8 +319,8 @@ foreach (array("adminer", "editor") as $project) { } } $file = preg_replace_callback("~lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])~s", 'lang_ids', $file); - $file = preg_replace_callback('~\\b(include|require) "([^"]*\\$LANG.inc.php)";~', 'put_file_lang', $file); $file = str_replace("\r", "", $file); + $file = preg_replace_callback('~\\b(include|require) "([^"]*\\$LANG.inc.php)";~', 'put_file_lang', $file); if ($_SESSION["lang"]) { // single language version $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file);