3.3.6-rc1
[phpmyadmin/dkf.git] / libraries / mysql_charsets.lib.php
blob0b583d6a099785e1e1db853a38fe1a6cd97e8941
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 * @package phpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
16 if (! PMA_cacheExists('mysql_charsets_count', true)) {
17 $res = PMA_DBI_query('SHOW CHARACTER SET;');
19 $mysql_charsets = array();
20 while ($row = PMA_DBI_fetch_assoc($res)) {
21 $mysql_charsets[] = $row['Charset'];
22 // never used
23 //$mysql_charsets_maxlen[$row['Charset']] = $row['Maxlen'];
24 $mysql_charsets_descriptions[$row['Charset']] = $row['Description'];
26 PMA_DBI_free_result($res);
28 $mysql_charsets_count = count($mysql_charsets);
29 sort($mysql_charsets, SORT_STRING);
31 $mysql_collations = array_flip($mysql_charsets);
32 $mysql_default_collations = $mysql_collations_flat = $mysql_charsets_available = $mysql_collations_available = array();
34 $res = PMA_DBI_query('SHOW COLLATION;');
35 while ($row = PMA_DBI_fetch_assoc($res)) {
36 if (!is_array($mysql_collations[$row['Charset']])) {
37 $mysql_collations[$row['Charset']] = array($row['Collation']);
38 } else {
39 $mysql_collations[$row['Charset']][] = $row['Collation'];
41 $mysql_collations_flat[] = $row['Collation'];
42 if ((isset($row['D']) && $row['D'] == 'Y') || (isset($row['Default']) && $row['Default'] == 'Yes')) {
43 $mysql_default_collations[$row['Charset']] = $row['Collation'];
45 //$mysql_collations_available[$row['Collation']] = !isset($row['Compiled']) || $row['Compiled'] == 'Yes';
46 $mysql_collations_available[$row['Collation']] = TRUE;
47 $mysql_charsets_available[$row['Charset']] =
48 !empty($mysql_charsets_available[$row['Charset']])
49 || !empty($mysql_collations_available[$row['Collation']]);
51 PMA_DBI_free_result($res);
52 unset($res, $row);
54 $mysql_collations_count = count($mysql_collations_flat);
55 sort($mysql_collations_flat, SORT_STRING);
56 foreach ($mysql_collations AS $key => $value) {
57 sort($mysql_collations[$key], SORT_STRING);
58 reset($mysql_collations[$key]);
60 unset($key, $value);
62 PMA_cacheSet('mysql_charsets', $GLOBALS['mysql_charsets'], true);
63 PMA_cacheSet('mysql_charsets_descriptions', $GLOBALS['mysql_charsets_descriptions'], true);
64 PMA_cacheSet('mysql_charsets_count', $GLOBALS['mysql_charsets_count'], true);
65 PMA_cacheSet('mysql_charsets_available', $GLOBALS['mysql_charsets_available'], true);
66 PMA_cacheSet('mysql_collations', $GLOBALS['mysql_collations'], true);
67 PMA_cacheSet('mysql_default_collations', $GLOBALS['mysql_default_collations'], true);
68 PMA_cacheSet('mysql_collations_flat', $GLOBALS['mysql_collations_flat'], true);
69 PMA_cacheSet('mysql_collations_count', $GLOBALS['mysql_collations_count'], true);
70 PMA_cacheSet('mysql_collations_available', $GLOBALS['mysql_collations_available'], true);
71 } else {
72 $GLOBALS['mysql_charsets'] = PMA_cacheGet('mysql_charsets', true);
73 $GLOBALS['mysql_charsets_descriptions'] = PMA_cacheGet('mysql_charsets_descriptions', true);
74 $GLOBALS['mysql_charsets_count'] = PMA_cacheGet('mysql_charsets_count', true);
75 $GLOBALS['mysql_charsets_available'] = PMA_cacheGet('mysql_charsets_available', true);
76 $GLOBALS['mysql_collations'] = PMA_cacheGet('mysql_collations', true);
77 $GLOBALS['mysql_default_collations'] = PMA_cacheGet('mysql_default_collations', true);
78 $GLOBALS['mysql_collations_flat'] = PMA_cacheGet('mysql_collations_flat', true);
79 $GLOBALS['mysql_collations_count'] = PMA_cacheGet('mysql_collations_count', true);
80 $GLOBALS['mysql_collations_available'] = PMA_cacheGet('mysql_collations_available', true);
83 define('PMA_CSDROPDOWN_COLLATION', 0);
84 define('PMA_CSDROPDOWN_CHARSET', 1);
86 function PMA_generateCharsetDropdownBox($type = PMA_CSDROPDOWN_COLLATION,
87 $name = null, $id = null, $default = null, $label = TRUE, $indent = 0,
88 $submitOnChange = FALSE, $displayUnavailable = FALSE)
90 global $mysql_charsets, $mysql_charsets_descriptions,
91 $mysql_charsets_available, $mysql_collations, $mysql_collations_available;
93 if (empty($name)) {
94 if ($type == PMA_CSDROPDOWN_COLLATION) {
95 $name = 'collation';
96 } else {
97 $name = 'character_set';
101 $return_str = '<select xml:lang="en" dir="ltr" name="'
102 . htmlspecialchars($name) . '"'
103 . (empty($id) ? '' : ' id="' . htmlspecialchars($id) . '"')
104 . ($submitOnChange ? ' onchange="this.form.submit();"' : '') . '>' . "\n";
105 if ($label) {
106 $return_str .= '<option value="">'
107 . ($type == PMA_CSDROPDOWN_COLLATION ? $GLOBALS['strCollation'] : $GLOBALS['strCharset'])
108 . '</option>' . "\n";
110 $return_str .= '<option value=""></option>' . "\n";
111 foreach ($mysql_charsets as $current_charset) {
112 if (!$mysql_charsets_available[$current_charset]) {
113 continue;
115 $current_cs_descr =
116 empty($mysql_charsets_descriptions[$current_charset])
117 ? $current_charset
118 : $mysql_charsets_descriptions[$current_charset];
120 if ($type == PMA_CSDROPDOWN_COLLATION) {
121 $return_str .= '<optgroup label="' . $current_charset
122 . '" title="' . $current_cs_descr . '">' . "\n";
123 foreach ($mysql_collations[$current_charset] as $current_collation) {
124 if (!$mysql_collations_available[$current_collation]) {
125 continue;
127 $return_str .= '<option value="' . $current_collation
128 . '" title="' . PMA_getCollationDescr($current_collation) . '"'
129 . ($default == $current_collation ? ' selected="selected"' : '') . '>'
130 . $current_collation . '</option>' . "\n";
132 $return_str .= '</optgroup>' . "\n";
133 } else {
134 $return_str .= '<option value="' . $current_charset
135 . '" title="' . $current_cs_descr . '"'
136 . ($default == $current_charset ? ' selected="selected"' : '') . '>'
137 . $current_charset . '</option>' . "\n";
140 $return_str .= '</select>' . "\n";
142 return $return_str;
145 function PMA_generateCharsetQueryPart($collation) {
146 list($charset) = explode('_', $collation);
147 return ' CHARACTER SET ' . $charset . ($charset == $collation ? '' : ' COLLATE ' . $collation);
151 * returns collation of given db
153 * @uses PMA_DBI_fetch_value()
154 * @uses PMA_DBI_select_db()
155 * @uses PMA_sqlAddSlashes()
156 * @uses $GLOBALS['db']
157 * @param string $db name of db
158 * @return string collation of $db
160 function PMA_getDbCollation($db) {
161 if ($db == 'information_schema') {
162 // We don't have to check the collation of the virtual
163 // information_schema database: We know it!
164 return 'utf8_general_ci';
167 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
168 // this is slow with thousands of databases
169 return PMA_DBI_fetch_value('SELECT DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = \'' . PMA_sqlAddSlashes($db) . '\' LIMIT 1;');
170 } else {
171 PMA_DBI_select_db($db);
172 $return = PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'collation_database\'', 0, 1);
173 if ($db !== $GLOBALS['db']) {
174 PMA_DBI_select_db($GLOBALS['db']);
176 return $return;
181 * returns default server collation from show variables
183 * @uses PMA_DBI_fetch_value()
184 * @return string $server_collation
186 function PMA_getServerCollation() {
187 return PMA_DBI_fetch_value(
188 'SHOW VARIABLES LIKE \'collation_server\'', 0, 1);
192 * returns description for given collation
194 * @uses is_array()
195 * @uses explode()
196 * @uses count()
197 * @uses $GLOBALS['str[Languages|Sorting]']
199 * @param string $collation MySQL collation string
200 * @return string collation description
202 function PMA_getCollationDescr($collation) {
203 static $collation_cache;
205 if (!is_array($collation_cache)) {
206 $collation_cache = array();
207 } elseif (isset($collation_cache[$collation])) {
208 return $collation_cache[$collation];
211 if ($collation == 'binary') {
212 return $GLOBALS['strBinary'];
214 $parts = explode('_', $collation);
215 if (count($parts) == 1) {
216 $parts[1] = 'general';
217 } elseif ($parts[1] == 'ci' || $parts[1] == 'cs') {
218 $parts[2] = $parts[1];
219 $parts[1] = 'general';
221 $descr = '';
222 switch ($parts[1]) {
223 case 'bulgarian':
224 $descr = $GLOBALS['strBulgarian'];
225 break;
226 case 'chinese':
227 if ($parts[0] == 'gb2312' || $parts[0] == 'gbk') {
228 $descr = $GLOBALS['strSimplifiedChinese'];
229 } elseif ($parts[0] == 'big5') {
230 $descr = $GLOBALS['strTraditionalChinese'];
232 break;
233 case 'ci':
234 $descr = $GLOBALS['strCaseInsensitive'];
235 break;
236 case 'cs':
237 $descr = $GLOBALS['strCaseSensitive'];
238 break;
239 case 'croatian':
240 $descr = $GLOBALS['strCroatian'];
241 break;
242 case 'czech':
243 $descr = $GLOBALS['strCzech'];
244 break;
245 case 'danish':
246 $descr = $GLOBALS['strDanish'];
247 break;
248 case 'english':
249 $descr = $GLOBALS['strEnglish'];
250 break;
251 case 'esperanto':
252 $descr = $GLOBALS['strEsperanto'];
253 break;
254 case 'estonian':
255 $descr = $GLOBALS['strEstonian'];
256 break;
257 case 'german1':
258 $descr = $GLOBALS['strGerman'] . ' (' . $GLOBALS['strDictionary'] . ')';
259 break;
260 case 'german2':
261 $descr = $GLOBALS['strGerman'] . ' (' . $GLOBALS['strPhoneBook'] . ')';
262 break;
263 case 'hungarian':
264 $descr = $GLOBALS['strHungarian'];
265 break;
266 case 'icelandic':
267 $descr = $GLOBALS['strIcelandic'];
268 break;
269 case 'japanese':
270 $descr = $GLOBALS['strJapanese'];
271 break;
272 case 'latvian':
273 $descr = $GLOBALS['strLatvian'];
274 break;
275 case 'lithuanian':
276 $descr = $GLOBALS['strLithuanian'];
277 break;
278 case 'korean':
279 $descr = $GLOBALS['strKorean'];
280 break;
281 case 'persian':
282 $descr = $GLOBALS['strPersian'];
283 break;
284 case 'polish':
285 $descr = $GLOBALS['strPolish'];
286 break;
287 case 'roman':
288 $descr = $GLOBALS['strWestEuropean'];
289 break;
290 case 'romanian':
291 $descr = $GLOBALS['strRomanian'];
292 break;
293 case 'slovak':
294 $descr = $GLOBALS['strSlovak'];
295 break;
296 case 'slovenian':
297 $descr = $GLOBALS['strSlovenian'];
298 break;
299 case 'spanish':
300 $descr = $GLOBALS['strSpanish'];
301 break;
302 case 'spanish2':
303 $descr = $GLOBALS['strTraditionalSpanish'];
304 break;
305 case 'swedish':
306 $descr = $GLOBALS['strSwedish'];
307 break;
308 case 'thai':
309 $descr = $GLOBALS['strThai'];
310 break;
311 case 'turkish':
312 $descr = $GLOBALS['strTurkish'];
313 break;
314 case 'ukrainian':
315 $descr = $GLOBALS['strUkrainian'];
316 break;
317 case 'unicode':
318 $descr = $GLOBALS['strUnicode'] . ' (' . $GLOBALS['strMultilingual'] . ')';
319 break;
320 case 'bin':
321 $is_bin = TRUE;
322 case 'general':
323 switch ($parts[0]) {
324 // Unicode charsets
325 case 'ucs2':
326 case 'utf8':
327 $descr = $GLOBALS['strUnicode'] . ' (' . $GLOBALS['strMultilingual'] . ')';
328 break;
329 // West European charsets
330 case 'ascii':
331 case 'cp850':
332 case 'dec8':
333 case 'hp8':
334 case 'latin1':
335 case 'macroman':
336 $descr = $GLOBALS['strWestEuropean'] . ' (' . $GLOBALS['strMultilingual'] . ')';
337 break;
338 // Central European charsets
339 case 'cp1250':
340 case 'cp852':
341 case 'latin2':
342 case 'macce':
343 $descr = $GLOBALS['strCentralEuropean'] . ' (' . $GLOBALS['strMultilingual'] . ')';
344 break;
345 // Russian charsets
346 case 'cp866':
347 case 'koi8r':
348 $descr = $GLOBALS['strRussian'];
349 break;
350 // Simplified Chinese charsets
351 case 'gb2312':
352 case 'gbk':
353 $descr = $GLOBALS['strSimplifiedChinese'];
354 break;
355 // Japanese charsets
356 case 'sjis':
357 case 'ujis':
358 case 'cp932':
359 case 'eucjpms':
360 $descr = $GLOBALS['strJapanese'];
361 break;
362 // Baltic charsets
363 case 'cp1257':
364 case 'latin7':
365 $descr = $GLOBALS['strBaltic'] . ' (' . $GLOBALS['strMultilingual'] . ')';
366 break;
367 // Other
368 case 'armscii8':
369 case 'armscii':
370 $descr = $GLOBALS['strArmenian'];
371 break;
372 case 'big5':
373 $descr = $GLOBALS['strTraditionalChinese'];
374 break;
375 case 'cp1251':
376 $descr = $GLOBALS['strCyrillic'] . ' (' . $GLOBALS['strMultilingual'] . ')';
377 break;
378 case 'cp1256':
379 $descr = $GLOBALS['strArabic'];
380 break;
381 case 'euckr':
382 $descr = $GLOBALS['strKorean'];
383 break;
384 case 'hebrew':
385 $descr = $GLOBALS['strHebrew'];
386 break;
387 case 'geostd8':
388 $descr = $GLOBALS['strGeorgian'];
389 break;
390 case 'greek':
391 $descr = $GLOBALS['strGreek'];
392 break;
393 case 'keybcs2':
394 $descr = $GLOBALS['strCzechSlovak'];
395 break;
396 case 'koi8u':
397 $descr = $GLOBALS['strUkrainian'];
398 break;
399 case 'latin5':
400 $descr = $GLOBALS['strTurkish'];
401 break;
402 case 'swe7':
403 $descr = $GLOBALS['strSwedish'];
404 break;
405 case 'tis620':
406 $descr = $GLOBALS['strThai'];
407 break;
408 default:
409 $descr = $GLOBALS['strUnknown'];
410 break;
412 if (!empty($is_bin)) {
413 $descr .= ', ' . $GLOBALS['strBinary'];
415 break;
416 default: $descr = $GLOBALS['strUnknown'];
418 if (!empty($parts[2])) {
419 if ($parts[2] == 'ci') {
420 $descr .= ', ' . $GLOBALS['strCaseInsensitive'];
421 } elseif ($parts[2] == 'cs') {
422 $descr .= ', ' . $GLOBALS['strCaseSensitive'];
426 $collation_cache[$collation] = $descr;
427 return $descr;