home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / xamp / xampp-win32.exe / xampp / mysql_charsets.lib.php < prev    next >
Encoding:
PHP Script  |  2005-02-22  |  13.6 KB  |  346 lines

  1. <?php
  2. /* $Id: mysql_charsets.lib.php,v 2.24.2.1 2005/02/23 13:53:55 rabus Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. if (PMA_MYSQL_INT_VERSION >= 40100){
  6.  
  7.     $res = PMA_DBI_query('SHOW CHARACTER SET;');
  8.  
  9.     $mysql_charsets = array();
  10.     while ($row = PMA_DBI_fetch_assoc($res)) {
  11.         $mysql_charsets[] = $row['Charset'];
  12.         $mysql_charsets_maxlen[$row['Charset']] = $row['Maxlen'];
  13.         $mysql_charsets_descriptions[$row['Charset']] = $row['Description'];
  14.     }
  15.     @PMA_DBI_free_result($res);
  16.     unset($res, $row);
  17.  
  18.     $res = PMA_DBI_query('SHOW COLLATION;');
  19.  
  20.     $mysql_charsets_count = count($mysql_charsets);
  21.     sort($mysql_charsets, SORT_STRING);
  22.  
  23.     $mysql_collations = array_flip($mysql_charsets);
  24.     $mysql_default_collations = $mysql_collations_flat = array();;
  25.     while ($row = PMA_DBI_fetch_assoc($res)) {
  26.         if (!is_array($mysql_collations[$row['Charset']])) {
  27.             $mysql_collations[$row['Charset']] = array($row['Collation']);
  28.         } else {
  29.             $mysql_collations[$row['Charset']][] = $row['Collation'];
  30.         }
  31.         $mysql_collations_flat[] = $row['Collation'];
  32.         if ((isset($row['D']) && $row['D'] == 'Y') || (isset($row['Default']) && $row['Default'] == 'Yes')) {
  33.             $mysql_default_collations[$row['Charset']] = $row['Collation'];
  34.         }
  35.     }
  36.  
  37.     $mysql_collations_count = count($mysql_collations_flat);
  38.     sort($mysql_collations_flat, SORT_STRING);
  39.     foreach ($mysql_collations AS $key => $value) {
  40.         sort($mysql_collations[$key], SORT_STRING);
  41.         reset($mysql_collations[$key]);
  42.     }
  43.  
  44.     @PMA_DBI_free_result($res);
  45.     unset($res, $row);
  46.  
  47.     function PMA_getCollationDescr($collation) {
  48.         static $collation_cache;
  49.  
  50.         if (!is_array($collation_cache)) {
  51.             $collation_cache = array();
  52.         } elseif (isset($collation_cache[$collation])) {
  53.             return $collation_cache[$collation];
  54.         }
  55.  
  56.         if ($collation == 'binary') {
  57.             return $GLOBALS['strBinary'];
  58.         }
  59.         $parts = explode('_', $collation);
  60.         if (count($parts) == 1) {
  61.             $parts[1] = 'general';
  62.         } elseif ($parts[1] == 'ci' || $parts[1] == 'cs') {
  63.             $parts[2] = $parts[1];
  64.             $parts[1] = 'general';
  65.         }
  66.         $descr = '';
  67.         switch ($parts[1]) {
  68.             case 'bulgarian':
  69.                 $descr = $GLOBALS['strBulgarian'];
  70.                 break;
  71.             case 'chinese':
  72.                 if ($parts[0] == 'gb2312' || $parts[0] == 'gbk') {
  73.                     $descr = $GLOBALS['strSimplifiedChinese'];
  74.                 } elseif ($parts[0] == 'big5') {
  75.                     $descr = $GLOBALS['strTraditionalChinese'];
  76.                 }
  77.                 break;
  78.             case 'ci':
  79.                 $descr = $GLOBALS['strCaseInsensitive'];
  80.                 break;
  81.             case 'cs':
  82.                 $descr = $GLOBALS['strCaseSensitive'];
  83.                 break;
  84.             case 'croatian':
  85.                 $descr = $GLOBALS['strCroatian'];
  86.                 break;
  87.             case 'czech':
  88.                 $descr = $GLOBALS['strCzech'];
  89.                 break;
  90.             case 'danish':
  91.                 $descr = $GLOBALS['strDanish'];
  92.                 break;
  93.             case 'english':
  94.                 $descr = $GLOBALS['strEnglish'];
  95.                 break;
  96.             case 'estonian':
  97.                 $descr = $GLOBALS['strEstonian'];
  98.                 break;
  99.             case 'german1':
  100.                 $descr = $GLOBALS['strGerman'] . ' (' . $GLOBALS['strDictionary'] . ')';
  101.                 break;
  102.             case 'german2':
  103.                 $descr = $GLOBALS['strGerman'] . ' (' . $GLOBALS['strPhoneBook'] . ')';
  104.                 break;
  105.             case 'hungarian':
  106.                 $descr = $GLOBALS['strHungarian'];
  107.                 break;
  108.             case 'icelandic':
  109.                 $descr = $GLOBALS['strIcelandic'];
  110.                 break;
  111.             case 'japanese':
  112.                 $descr = $GLOBALS['strJapanese'];
  113.                 break;
  114.             case 'latvian':
  115.                 $descr = $GLOBALS['strLatvian'];
  116.                 break;
  117.             case 'lithuanian':
  118.                 $descr = $GLOBALS['strLithuanian'];
  119.                 break;
  120.             case 'korean':
  121.                 $descr = $GLOBALS['strKorean'];
  122.                 break;
  123.             case 'persian':
  124.                 $descr = $GLOBALS['strPersian'];
  125.                 break;
  126.             case 'polish':
  127.                 $descr = $GLOBALS['strPolish'];
  128.                 break;
  129.             case 'roman':
  130.                 $descr = $GLOBALS['strWestEuropean'];
  131.                 break;
  132.             case 'romanian':
  133.                 $descr = $GLOBALS['strRomanian'];
  134.                 break;
  135.             case 'slovak':
  136.                 $descr = $GLOBALS['strSlovak'];
  137.                 break;
  138.             case 'slovenian':
  139.                 $descr = $GLOBALS['strSlovenian'];
  140.                 break;
  141.             case 'spanish':
  142.                 $descr = $GLOBALS['strSpanish'];
  143.                 break;
  144.             case 'spanish2':
  145.                 $descr = $GLOBALS['strTraditionalSpanish'];
  146.                 break;
  147.             case 'swedish':
  148.                 $descr = $GLOBALS['strSwedish'];
  149.                 break;
  150.             case 'thai':
  151.                 $descr = $GLOBALS['strThai'];
  152.                 break;
  153.             case 'turkish':
  154.                 $descr = $GLOBALS['strTurkish'];
  155.                 break;
  156.             case 'ukrainian':
  157.                 $descr = $GLOBALS['strUkrainian'];
  158.                 break;
  159.             case 'unicode':
  160.                 $descr = $GLOBALS['strUnicode'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  161.                 break;
  162.             case 'bin':
  163.                 $is_bin = TRUE;
  164.             case 'general':
  165.                 switch ($parts[0]) {
  166.                     // Unicode charsets
  167.                     case 'ucs2':
  168.                     case 'utf8':
  169.                         $descr = $GLOBALS['strUnicode'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  170.                         break;
  171.                     // West European charsets
  172.                     case 'ascii':
  173.                     case 'cp850':
  174.                     case 'dec8':
  175.                     case 'hp8':
  176.                     case 'latin1':
  177.                     case 'macroman':
  178.                         $descr = $GLOBALS['strWestEuropean'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  179.                         break;
  180.                     // Central European charsets
  181.                     case 'cp1250':
  182.                     case 'cp852':
  183.                     case 'latin2':
  184.                     case 'macce':
  185.                         $descr = $GLOBALS['strCentralEuropean'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  186.                         break;
  187.                     // Russian charsets
  188.                     case 'cp866':
  189.                     case 'koi8r':
  190.                         $descr = $GLOBALS['strRussian'];
  191.                         break;
  192.                     // Simplified Chinese charsets
  193.                     case 'gb2312':
  194.                     case 'gbk':
  195.                         $descr = $GLOBALS['strSimplifiedChinese'];
  196.                         break;
  197.                     // Japanese charsets
  198.                     case 'sjis':
  199.                     case 'ujis':
  200.                     case 'cp932':
  201.                     case 'eucjpms':
  202.                         $descr = $GLOBALS['strJapanese'];
  203.                         break;
  204.                     // Baltic charsets
  205.                     case 'cp1257':
  206.                     case 'latin7':
  207.                         $descr = $GLOBALS['strBaltic'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  208.                         break;
  209.                     // Other
  210.                     case 'armscii8':
  211.                     case 'armscii':
  212.                         $descr = $GLOBALS['strArmenian'];
  213.                         break;
  214.                     case 'big5':
  215.                         $descr = $GLOBALS['strTraditionalChinese'];
  216.                         break;
  217.                     case 'cp1251':
  218.                         $descr = $GLOBALS['strCyrillic'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  219.                         break;
  220.                     case 'cp1256':
  221.                         $descr = $GLOBALS['strArabic'];
  222.                         break;
  223.                     case 'euckr':
  224.                         $descr = $GLOBALS['strKorean'];
  225.                         break;
  226.                     case 'hebrew':
  227.                         $descr = $GLOBALS['strHebrew'];
  228.                         break;
  229.                     case 'geostd8':
  230.                         $descr = $GLOBALS['strGeorgian'];
  231.                         break;
  232.                     case 'greek':
  233.                         $descr = $GLOBALS['strGreek'];
  234.                         break;
  235.                     case 'keybcs2':
  236.                         $descr = $GLOBALS['strCzechSlovak'];
  237.                         break;
  238.                     case 'koi8u':
  239.                         $descr = $GLOBALS['strUkrainian'];
  240.                         break;
  241.                     case 'latin5':
  242.                         $descr = $GLOBALS['strTurkish'];
  243.                         break;
  244.                     case 'swe7':
  245.                         $descr = $GLOBALS['strSwedish'];
  246.                         break;
  247.                     case 'tis620':
  248.                         $descr = $GLOBALS['strThai'];
  249.                         break;
  250.                     default:
  251.                         $descr = $GLOBALS['strUnknown'];
  252.                         break;
  253.                 }
  254.                 if (!empty($is_bin)) {
  255.                     $descr .= ', ' . $GLOBALS['strBinary'];
  256.                 }
  257.                 break;
  258.             default: $descr = $GLOBALS['strUnknown'];
  259.         }
  260.         if (!empty($parts[2])) {
  261.             if ($parts[2] == 'ci') {
  262.                 $descr .= ', ' . $GLOBALS['strCaseInsensitive'];
  263.             } elseif ($parts[2] == 'cs') {
  264.                 $descr .= ', ' . $GLOBALS['strCaseSensitive'];
  265.             }
  266.         }
  267.  
  268.         $collation_cache[$collation] = $descr;
  269.         return $descr;
  270.     }
  271.  
  272.     function PMA_getDbCollation($db) {
  273.         global $userlink;
  274.         if (PMA_MYSQL_INT_VERSION >= 40101) {
  275.             // MySQL 4.1.0 does not support seperate charset settings
  276.             // for databases.
  277.             $res = PMA_DBI_query('SHOW CREATE DATABASE ' . PMA_backquote($db) . ';', NULL, PMA_DBI_QUERY_STORE);
  278.             $row = PMA_DBI_fetch_row($res);
  279.             PMA_DBI_free_result($res);
  280.             $tokenized = explode(' ', $row[1]);
  281.             unset($row, $res, $sql_query);
  282.  
  283.             for ($i = 1; $i + 3 < count($tokenized); $i++) {
  284.                 if ($tokenized[$i] == 'DEFAULT' && $tokenized[$i + 1] == 'CHARACTER' && $tokenized[$i + 2] == 'SET') {
  285.                     // We've found the character set!
  286.                     if (isset($tokenized[$i + 5]) && $tokenized[$i + 4] == 'COLLATE') {
  287.                         return $tokenized[$i + 5]; // We found the collation!
  288.                     } else {
  289.                         // We did not find the collation, so let's return the
  290.                         // default collation for the charset we've found.
  291.                         return $GLOBALS['mysql_default_collations'][$tokenized [$i + 3]];
  292.                     }
  293.                 }
  294.             }
  295.         }
  296.         return '';
  297.     }
  298.  
  299.     define('PMA_CSDROPDOWN_COLLATION', 0);
  300.     define('PMA_CSDROPDOWN_CHARSET',   1);
  301.  
  302.     function PMA_generateCharsetDropdownBox($type = PMA_CSDROPDOWN_COLLATION, $name = NULL, $id = NULL, $default = NULL, $label = TRUE, $indent = 0, $submitOnChange = FALSE) {
  303.         global $mysql_charsets, $mysql_charsets_descriptions, $mysql_collations;
  304.  
  305.         if (empty($name)) {
  306.             if ($type == PMA_CSDROPDOWN_COLLATION) {
  307.                 $name = 'collation';
  308.             } else {
  309.                 $name = 'character_set';
  310.             }
  311.         }
  312.  
  313.         $spacer = '';
  314.         for ($i = 1; $i <= $indent; $i++) $spacer .= '    ';
  315.  
  316.         $return_str  = $spacer . '<select name="' . htmlspecialchars($name) . '"' . (empty($id) ? '' : ' id="' . htmlspecialchars($id) . '"') . ($submitOnChange ? ' onchange="this.form.submit();"' : '') . '>' . "\n";
  317.         if ($label) {
  318.             $return_str .= $spacer . '    <option value="">' . ($type == PMA_CSDROPDOWN_COLLATION ? $GLOBALS['strCollation'] : $GLOBALS['strCharset']) . '</option>' . "\n";
  319.         }
  320.         $return_str .= $spacer . '    <option value=""></option>' . "\n";
  321.         foreach ($mysql_charsets as $current_charset) {
  322.             $current_cs_descr = empty($mysql_charsets_descriptions[$current_charset]) ? $current_charset : $mysql_charsets_descriptions[$current_charset];
  323.             if ($type == PMA_CSDROPDOWN_COLLATION) {
  324.                 $return_str .= $spacer . '    <optgroup label="' . $current_charset . '" title="' . $current_cs_descr . '">' . "\n";
  325.                 foreach ($mysql_collations[$current_charset] as $current_collation) {
  326.                     $return_str .= $spacer . '        <option value="' . $current_collation . '" title="' . PMA_getCollationDescr($current_collation) . '"' . ($default == $current_collation ? ' selected="selected"' : '') . '>' . $current_collation . '</option>' . "\n";
  327.                 }
  328.                 $return_str .= $spacer . '    </optgroup>' . "\n";
  329.             } else {
  330.                 $return_str .= $spacer . '    <option value="' . $current_charset . '" title="' . $current_cs_descr . '"' . ($default == $current_charset ? ' selected="selected"' : '') . '>' . $current_charset . '</option>' . "\n";
  331.             }
  332.         }
  333.         $return_str .= $spacer . '</select>' . "\n";
  334.  
  335.         return $return_str;
  336.     }
  337.  
  338.     function PMA_generateCharsetQueryPart($collation) {
  339.         list($charset) = explode('_', $collation);
  340.         return ' CHARACTER SET ' . $charset . ($charset == $collation ? '' : ' COLLATE ' . $collation);
  341.     }
  342.  
  343. }
  344.  
  345. ?>
  346.