Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / libraries / mysql_charsets.lib.php
blob3dad37a54e930fcee69c3636acb91291e9e16a64
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package phpMyAdmin
6 */
7 if (! defined('PHPMYADMIN')) {
8 exit;
11 /**
14 if (! PMA_cacheExists('mysql_charsets', true)) {
15 $sql = PMA_DRIZZLE
16 ? 'SELECT * FROM data_dictionary.CHARACTER_SETS'
17 : 'SELECT * FROM information_schema.CHARACTER_SETS';
18 $res = PMA_DBI_query($sql);
20 $mysql_charsets = array();
21 while ($row = PMA_DBI_fetch_assoc($res)) {
22 $mysql_charsets[] = $row['CHARACTER_SET_NAME'];
23 // never used
24 //$mysql_charsets_maxlen[$row['Charset']] = $row['Maxlen'];
25 $mysql_charsets_descriptions[$row['CHARACTER_SET_NAME']] = $row['DESCRIPTION'];
27 PMA_DBI_free_result($res);
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 $sql = PMA_DRIZZLE
35 ? 'SELECT * FROM data_dictionary.COLLATIONS'
36 : 'SELECT * FROM information_schema.COLLATIONS';
37 $res = PMA_DBI_query($sql);
38 while ($row = PMA_DBI_fetch_assoc($res)) {
39 if (! is_array($mysql_collations[$row['CHARACTER_SET_NAME']])) {
40 $mysql_collations[$row['CHARACTER_SET_NAME']] = array($row['COLLATION_NAME']);
41 } else {
42 $mysql_collations[$row['CHARACTER_SET_NAME']][] = $row['COLLATION_NAME'];
44 $mysql_collations_flat[] = $row['COLLATION_NAME'];
45 if ($row['IS_DEFAULT'] == 'Yes' || $row['IS_DEFAULT'] == '1') {
46 $mysql_default_collations[$row['CHARACTER_SET_NAME']] = $row['COLLATION_NAME'];
48 //$mysql_collations_available[$row['Collation']] = ! isset($row['Compiled']) || $row['Compiled'] == 'Yes';
49 $mysql_collations_available[$row['COLLATION_NAME']] = true;
50 $mysql_charsets_available[$row['CHARACTER_SET_NAME']] =
51 !empty($mysql_charsets_available[$row['CHARACTER_SET_NAME']])
52 || !empty($mysql_collations_available[$row['COLLATION_NAME']]);
54 PMA_DBI_free_result($res);
55 unset($res, $row);
57 if (PMA_DRIZZLE && isset($mysql_collations['utf8_general_ci']) && isset($mysql_collations['utf8'])) {
58 $mysql_collations['utf8'] = $mysql_collations['utf8_general_ci'];
59 $mysql_default_collations['utf8'] = $mysql_default_collations['utf8_general_ci'];
60 $mysql_charsets_available['utf8'] = $mysql_charsets_available['utf8_general_ci'];
61 unset($mysql_collations['utf8_general_ci'], $mysql_default_collations['utf8_general_ci'],
62 $mysql_charsets_available['utf8_general_ci']);
65 sort($mysql_collations_flat, SORT_STRING);
66 foreach ($mysql_collations AS $key => $value) {
67 sort($mysql_collations[$key], SORT_STRING);
68 reset($mysql_collations[$key]);
70 unset($key, $value);
72 PMA_cacheSet('mysql_charsets', $GLOBALS['mysql_charsets'], true);
73 PMA_cacheSet('mysql_charsets_descriptions', $GLOBALS['mysql_charsets_descriptions'], true);
74 PMA_cacheSet('mysql_charsets_available', $GLOBALS['mysql_charsets_available'], true);
75 PMA_cacheSet('mysql_collations', $GLOBALS['mysql_collations'], true);
76 PMA_cacheSet('mysql_default_collations', $GLOBALS['mysql_default_collations'], true);
77 PMA_cacheSet('mysql_collations_flat', $GLOBALS['mysql_collations_flat'], true);
78 PMA_cacheSet('mysql_collations_available', $GLOBALS['mysql_collations_available'], true);
79 } else {
80 $GLOBALS['mysql_charsets'] = PMA_cacheGet('mysql_charsets', true);
81 $GLOBALS['mysql_charsets_descriptions'] = PMA_cacheGet('mysql_charsets_descriptions', true);
82 $GLOBALS['mysql_charsets_available'] = PMA_cacheGet('mysql_charsets_available', true);
83 $GLOBALS['mysql_collations'] = PMA_cacheGet('mysql_collations', true);
84 $GLOBALS['mysql_default_collations'] = PMA_cacheGet('mysql_default_collations', true);
85 $GLOBALS['mysql_collations_flat'] = PMA_cacheGet('mysql_collations_flat', true);
86 $GLOBALS['mysql_collations_available'] = PMA_cacheGet('mysql_collations_available', true);
89 define('PMA_CSDROPDOWN_COLLATION', 0);
90 define('PMA_CSDROPDOWN_CHARSET', 1);
92 function PMA_generateCharsetDropdownBox($type = PMA_CSDROPDOWN_COLLATION,
93 $name = null, $id = null, $default = null, $label = true, $indent = 0,
94 $submitOnChange = false, $displayUnavailable = false)
96 global $mysql_charsets, $mysql_charsets_descriptions,
97 $mysql_charsets_available, $mysql_collations, $mysql_collations_available;
99 if (empty($name)) {
100 if ($type == PMA_CSDROPDOWN_COLLATION) {
101 $name = 'collation';
102 } else {
103 $name = 'character_set';
107 $return_str = '<select xml:lang="en" dir="ltr" name="'
108 . htmlspecialchars($name) . '"'
109 . (empty($id) ? '' : ' id="' . htmlspecialchars($id) . '"')
110 . ($submitOnChange ? ' onchange="this.form.submit();"' : '') . '>' . "\n";
111 if ($label) {
112 $return_str .= '<option value="">'
113 . ($type == PMA_CSDROPDOWN_COLLATION ? __('Collation') : __('Charset'))
114 . '</option>' . "\n";
116 $return_str .= '<option value=""></option>' . "\n";
117 foreach ($mysql_charsets as $current_charset) {
118 if (!$mysql_charsets_available[$current_charset]) {
119 continue;
121 $current_cs_descr =
122 empty($mysql_charsets_descriptions[$current_charset])
123 ? $current_charset
124 : $mysql_charsets_descriptions[$current_charset];
126 if ($type == PMA_CSDROPDOWN_COLLATION) {
127 $return_str .= '<optgroup label="' . $current_charset
128 . '" title="' . $current_cs_descr . '">' . "\n";
129 foreach ($mysql_collations[$current_charset] as $current_collation) {
130 if (!$mysql_collations_available[$current_collation]) {
131 continue;
133 $return_str .= '<option value="' . $current_collation
134 . '" title="' . PMA_getCollationDescr($current_collation) . '"'
135 . ($default == $current_collation ? ' selected="selected"' : '') . '>'
136 . $current_collation . '</option>' . "\n";
138 $return_str .= '</optgroup>' . "\n";
139 } else {
140 $return_str .= '<option value="' . $current_charset
141 . '" title="' . $current_cs_descr . '"'
142 . ($default == $current_charset ? ' selected="selected"' : '') . '>'
143 . $current_charset . '</option>' . "\n";
146 $return_str .= '</select>' . "\n";
148 return $return_str;
151 function PMA_generateCharsetQueryPart($collation) {
152 if (!PMA_DRIZZLE) {
153 list($charset) = explode('_', $collation);
154 return ' CHARACTER SET ' . $charset . ($charset == $collation ? '' : ' COLLATE ' . $collation);
155 } else {
156 return ' COLLATE ' . $collation;
161 * returns collation of given db
163 * @param string $db name of db
164 * @return string collation of $db
166 function PMA_getDbCollation($db) {
167 if (strtolower($db) == 'information_schema' || (PMA_DRIZZLE && strtolower($db) == 'data_dictionary')) {
168 // We don't have to check the collation of the virtual
169 // information_schema database: We know it!
170 return 'utf8_general_ci';
173 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
174 // this is slow with thousands of databases
175 $sql = PMA_DRIZZLE
176 ? 'SELECT DEFAULT_COLLATION_NAME FROM data_dictionary.SCHEMAS WHERE SCHEMA_NAME = \'' . PMA_sqlAddSlashes($db) . '\' LIMIT 1'
177 : 'SELECT DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = \'' . PMA_sqlAddSlashes($db) . '\' LIMIT 1';
178 return PMA_DBI_fetch_value($sql);
179 } else {
180 PMA_DBI_select_db($db);
181 $return = PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'collation_database\'', 0, 1);
182 if ($db !== $GLOBALS['db']) {
183 PMA_DBI_select_db($GLOBALS['db']);
185 return $return;
190 * returns default server collation from show variables
192 * @return string $server_collation
194 function PMA_getServerCollation() {
195 return PMA_DBI_fetch_value(
196 'SHOW VARIABLES LIKE \'collation_server\'', 0, 1);
200 * returns description for given collation
203 * @param string $collation MySQL collation string
204 * @return string collation description
206 function PMA_getCollationDescr($collation) {
207 if ($collation == 'binary') {
208 return __('Binary');
210 $parts = explode('_', $collation);
211 if (count($parts) == 1) {
212 $parts[1] = 'general';
213 } elseif ($parts[1] == 'ci' || $parts[1] == 'cs') {
214 $parts[2] = $parts[1];
215 $parts[1] = 'general';
217 $descr = '';
218 switch ($parts[1]) {
219 case 'bulgarian':
220 $descr = __('Bulgarian');
221 break;
222 case 'chinese':
223 if ($parts[0] == 'gb2312' || $parts[0] == 'gbk') {
224 $descr = __('Simplified Chinese');
225 } elseif ($parts[0] == 'big5') {
226 $descr = __('Traditional Chinese');
228 break;
229 case 'ci':
230 $descr = __('case-insensitive');
231 break;
232 case 'cs':
233 $descr = __('case-sensitive');
234 break;
235 case 'croatian':
236 $descr = __('Croatian');
237 break;
238 case 'czech':
239 $descr = __('Czech');
240 break;
241 case 'danish':
242 $descr = __('Danish');
243 break;
244 case 'english':
245 $descr = __('English');
246 break;
247 case 'esperanto':
248 $descr = __('Esperanto');
249 break;
250 case 'estonian':
251 $descr = __('Estonian');
252 break;
253 case 'german1':
254 $descr = __('German') . ' (' . __('dictionary') . ')';
255 break;
256 case 'german2':
257 $descr = __('German') . ' (' . __('phone book') . ')';
258 break;
259 case 'hungarian':
260 $descr = __('Hungarian');
261 break;
262 case 'icelandic':
263 $descr = __('Icelandic');
264 break;
265 case 'japanese':
266 $descr = __('Japanese');
267 break;
268 case 'latvian':
269 $descr = __('Latvian');
270 break;
271 case 'lithuanian':
272 $descr = __('Lithuanian');
273 break;
274 case 'korean':
275 $descr = __('Korean');
276 break;
277 case 'persian':
278 $descr = __('Persian');
279 break;
280 case 'polish':
281 $descr = __('Polish');
282 break;
283 case 'roman':
284 $descr = __('West European');
285 break;
286 case 'romanian':
287 $descr = __('Romanian');
288 break;
289 case 'slovak':
290 $descr = __('Slovak');
291 break;
292 case 'slovenian':
293 $descr = __('Slovenian');
294 break;
295 case 'spanish':
296 $descr = __('Spanish');
297 break;
298 case 'spanish2':
299 $descr = __('Traditional Spanish');
300 break;
301 case 'swedish':
302 $descr = __('Swedish');
303 break;
304 case 'thai':
305 $descr = __('Thai');
306 break;
307 case 'turkish':
308 $descr = __('Turkish');
309 break;
310 case 'ukrainian':
311 $descr = __('Ukrainian');
312 break;
313 case 'unicode':
314 $descr = __('Unicode') . ' (' . __('multilingual') . ')';
315 break;
316 case 'bin':
317 $is_bin = true;
318 case 'general':
319 switch ($parts[0]) {
320 // Unicode charsets
321 case 'ucs2':
322 case 'utf8':
323 $descr = __('Unicode') . ' (' . __('multilingual') . ')';
324 break;
325 // West European charsets
326 case 'ascii':
327 case 'cp850':
328 case 'dec8':
329 case 'hp8':
330 case 'latin1':
331 case 'macroman':
332 $descr = __('West European') . ' (' . __('multilingual') . ')';
333 break;
334 // Central European charsets
335 case 'cp1250':
336 case 'cp852':
337 case 'latin2':
338 case 'macce':
339 $descr = __('Central European') . ' (' . __('multilingual') . ')';
340 break;
341 // Russian charsets
342 case 'cp866':
343 case 'koi8r':
344 $descr = __('Russian');
345 break;
346 // Simplified Chinese charsets
347 case 'gb2312':
348 case 'gbk':
349 $descr = __('Simplified Chinese');
350 break;
351 // Japanese charsets
352 case 'sjis':
353 case 'ujis':
354 case 'cp932':
355 case 'eucjpms':
356 $descr = __('Japanese');
357 break;
358 // Baltic charsets
359 case 'cp1257':
360 case 'latin7':
361 $descr = __('Baltic') . ' (' . __('multilingual') . ')';
362 break;
363 // Other
364 case 'armscii8':
365 case 'armscii':
366 $descr = __('Armenian');
367 break;
368 case 'big5':
369 $descr = __('Traditional Chinese');
370 break;
371 case 'cp1251':
372 $descr = __('Cyrillic') . ' (' . __('multilingual') . ')';
373 break;
374 case 'cp1256':
375 $descr = __('Arabic');
376 break;
377 case 'euckr':
378 $descr = __('Korean');
379 break;
380 case 'hebrew':
381 $descr = __('Hebrew');
382 break;
383 case 'geostd8':
384 $descr = __('Georgian');
385 break;
386 case 'greek':
387 $descr = __('Greek');
388 break;
389 case 'keybcs2':
390 $descr = __('Czech-Slovak');
391 break;
392 case 'koi8u':
393 $descr = __('Ukrainian');
394 break;
395 case 'latin5':
396 $descr = __('Turkish');
397 break;
398 case 'swe7':
399 $descr = __('Swedish');
400 break;
401 case 'tis620':
402 $descr = __('Thai');
403 break;
404 default:
405 $descr = __('unknown');
406 break;
408 if (!empty($is_bin)) {
409 $descr .= ', ' . __('Binary');
411 break;
412 default: $descr = __('unknown');
414 if (!empty($parts[2])) {
415 if ($parts[2] == 'ci') {
416 $descr .= ', ' . __('case-insensitive');
417 } elseif ($parts[2] == 'cs') {
418 $descr .= ', ' . __('case-sensitive');
422 return $descr;