]> git.joonet.de Git - adminer.git/commitdiff
Compress translations
authorJakub Vrana <jakub@vrana.cz>
Sun, 2 Sep 2012 13:44:32 +0000 (06:44 -0700)
committerJakub Vrana <jakub@vrana.cz>
Fri, 7 Sep 2012 15:30:31 +0000 (08:30 -0700)
adminer/include/lang.inc.php
changes.txt
compile.php

index c6fd7df285ce2b58d552e16f9b176e868e46340f..760c3639d09a98434e6365addc5e14256b19bbd0 100644 (file)
@@ -78,6 +78,43 @@ function switch_lang() {
        echo "</div>\n</form>\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
index d34c84182a43643938e32011b2e6aa0ef8535544..f8e1a61d1e06f987d4942317e98e0a9316ebabdb 100644 (file)
@@ -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
 
index b3ca074d8a6e847d7edaa06ba7a5a4223950ca19..8ebb22c93196e4f1efaf8483ad39efc2c77f254d 100755 (executable)
@@ -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);