Fixed: 'Browse foreign values' not working properly
[phpmyadmin/ammaryasirr.git] / libraries / mysql_charsets.lib.php
blob05a5c3b83c113469e339668a4355f6d42fd52924
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') {
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 unset($mysql_collations['utf8_general_ci']);
62 sort($mysql_collations_flat, SORT_STRING);
63 foreach ($mysql_collations AS $key => $value) {
64 sort($mysql_collations[$key], SORT_STRING);
65 reset($mysql_collations[$key]);
67 unset($key, $value);
69 PMA_cacheSet('mysql_charsets', $GLOBALS['mysql_charsets'], true);
70 PMA_cacheSet('mysql_charsets_descriptions', $GLOBALS['mysql_charsets_descriptions'], true);
71 PMA_cacheSet('mysql_charsets_available', $GLOBALS['mysql_charsets_available'], true);
72 PMA_cacheSet('mysql_collations', $GLOBALS['mysql_collations'], true);
73 PMA_cacheSet('mysql_default_collations', $GLOBALS['mysql_default_collations'], true);
74 PMA_cacheSet('mysql_collations_flat', $GLOBALS['mysql_collations_flat'], true);
75 PMA_cacheSet('mysql_collations_available', $GLOBALS['mysql_collations_available'], true);
76 } else {
77 $GLOBALS['mysql_charsets'] = PMA_cacheGet('mysql_charsets', true);
78 $GLOBALS['mysql_charsets_descriptions'] = PMA_cacheGet('mysql_charsets_descriptions', true);
79 $GLOBALS['mysql_charsets_available'] = PMA_cacheGet('mysql_charsets_available', true);
80 $GLOBALS['mysql_collations'] = PMA_cacheGet('mysql_collations', true);
81 $GLOBALS['mysql_default_collations'] = PMA_cacheGet('mysql_default_collations', true);
82 $GLOBALS['mysql_collations_flat'] = PMA_cacheGet('mysql_collations_flat', true);
83 $GLOBALS['mysql_collations_available'] = PMA_cacheGet('mysql_collations_available', true);
86 define('PMA_CSDROPDOWN_COLLATION', 0);
87 define('PMA_CSDROPDOWN_CHARSET', 1);
89 function PMA_generateCharsetDropdownBox($type = PMA_CSDROPDOWN_COLLATION,
90 $name = null, $id = null, $default = null, $label = true, $indent = 0,
91 $submitOnChange = false, $displayUnavailable = false)
93 global $mysql_charsets, $mysql_charsets_descriptions,
94 $mysql_charsets_available, $mysql_collations, $mysql_collations_available;
96 if (empty($name)) {
97 if ($type == PMA_CSDROPDOWN_COLLATION) {
98 $name = 'collation';
99 } else {
100 $name = 'character_set';
104 $return_str = '<select xml:lang="en" dir="ltr" name="'
105 . htmlspecialchars($name) . '"'
106 . (empty($id) ? '' : ' id="' . htmlspecialchars($id) . '"')
107 . ($submitOnChange ? ' class="autosubmit"' : '') . '>' . "\n";
108 if ($label) {
109 $return_str .= '<option value="">'
110 . ($type == PMA_CSDROPDOWN_COLLATION ? __('Collation') : __('Charset'))
111 . '</option>' . "\n";
113 $return_str .= '<option value=""></option>' . "\n";
114 foreach ($mysql_charsets as $current_charset) {
115 if (!$mysql_charsets_available[$current_charset]) {
116 continue;
118 $current_cs_descr =
119 empty($mysql_charsets_descriptions[$current_charset])
120 ? $current_charset
121 : $mysql_charsets_descriptions[$current_charset];
123 if ($type == PMA_CSDROPDOWN_COLLATION) {
124 $return_str .= '<optgroup label="' . $current_charset
125 . '" title="' . $current_cs_descr . '">' . "\n";
126 foreach ($mysql_collations[$current_charset] as $current_collation) {
127 if (!$mysql_collations_available[$current_collation]) {
128 continue;
130 $return_str .= '<option value="' . $current_collation
131 . '" title="' . PMA_getCollationDescr($current_collation) . '"'
132 . ($default == $current_collation ? ' selected="selected"' : '') . '>'
133 . $current_collation . '</option>' . "\n";
135 $return_str .= '</optgroup>' . "\n";
136 } else {
137 $return_str .= '<option value="' . $current_charset
138 . '" title="' . $current_cs_descr . '"'
139 . ($default == $current_charset ? ' selected="selected"' : '') . '>'
140 . $current_charset . '</option>' . "\n";
143 $return_str .= '</select>' . "\n";
145 return $return_str;
148 function PMA_generateCharsetQueryPart($collation)
150 list($charset) = explode('_', $collation);
151 return ' CHARACTER SET ' . $charset . ($charset == $collation ? '' : ' COLLATE ' . $collation);
155 * returns collation of given db
157 * @param string $db name of db
158 * @return string collation of $db
160 function PMA_getDbCollation($db)
162 if ($db == 'information_schema') {
163 // We don't have to check the collation of the virtual
164 // information_schema database: We know it!
165 return 'utf8_general_ci';
168 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
169 // this is slow with thousands of databases
170 return PMA_DBI_fetch_value('SELECT DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = \'' . PMA_sqlAddSlashes($db) . '\' LIMIT 1;');
171 } else {
172 PMA_DBI_select_db($db);
173 $return = PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'collation_database\'', 0, 1);
174 if ($db !== $GLOBALS['db']) {
175 PMA_DBI_select_db($GLOBALS['db']);
177 return $return;
182 * returns default server collation from show variables
184 * @return string $server_collation
186 function PMA_getServerCollation()
188 return PMA_DBI_fetch_value(
189 'SHOW VARIABLES LIKE \'collation_server\'', 0, 1);
193 * returns description for given collation
196 * @param string $collation MySQL collation string
197 * @return string collation description
199 function PMA_getCollationDescr($collation)
201 if ($collation == 'binary') {
202 return __('Binary');
204 $parts = explode('_', $collation);
205 if (count($parts) == 1) {
206 $parts[1] = 'general';
207 } elseif ($parts[1] == 'ci' || $parts[1] == 'cs') {
208 $parts[2] = $parts[1];
209 $parts[1] = 'general';
211 $descr = '';
212 switch ($parts[1]) {
213 case 'bulgarian':
214 $descr = __('Bulgarian');
215 break;
216 case 'chinese':
217 if ($parts[0] == 'gb2312' || $parts[0] == 'gbk') {
218 $descr = __('Simplified Chinese');
219 } elseif ($parts[0] == 'big5') {
220 $descr = __('Traditional Chinese');
222 break;
223 case 'ci':
224 $descr = __('case-insensitive');
225 break;
226 case 'cs':
227 $descr = __('case-sensitive');
228 break;
229 case 'croatian':
230 $descr = __('Croatian');
231 break;
232 case 'czech':
233 $descr = __('Czech');
234 break;
235 case 'danish':
236 $descr = __('Danish');
237 break;
238 case 'english':
239 $descr = __('English');
240 break;
241 case 'esperanto':
242 $descr = __('Esperanto');
243 break;
244 case 'estonian':
245 $descr = __('Estonian');
246 break;
247 case 'german1':
248 $descr = __('German') . ' (' . __('dictionary') . ')';
249 break;
250 case 'german2':
251 $descr = __('German') . ' (' . __('phone book') . ')';
252 break;
253 case 'hungarian':
254 $descr = __('Hungarian');
255 break;
256 case 'icelandic':
257 $descr = __('Icelandic');
258 break;
259 case 'japanese':
260 $descr = __('Japanese');
261 break;
262 case 'latvian':
263 $descr = __('Latvian');
264 break;
265 case 'lithuanian':
266 $descr = __('Lithuanian');
267 break;
268 case 'korean':
269 $descr = __('Korean');
270 break;
271 case 'persian':
272 $descr = __('Persian');
273 break;
274 case 'polish':
275 $descr = __('Polish');
276 break;
277 case 'roman':
278 $descr = __('West European');
279 break;
280 case 'romanian':
281 $descr = __('Romanian');
282 break;
283 case 'slovak':
284 $descr = __('Slovak');
285 break;
286 case 'slovenian':
287 $descr = __('Slovenian');
288 break;
289 case 'spanish':
290 $descr = __('Spanish');
291 break;
292 case 'spanish2':
293 $descr = __('Traditional Spanish');
294 break;
295 case 'swedish':
296 $descr = __('Swedish');
297 break;
298 case 'thai':
299 $descr = __('Thai');
300 break;
301 case 'turkish':
302 $descr = __('Turkish');
303 break;
304 case 'ukrainian':
305 $descr = __('Ukrainian');
306 break;
307 case 'unicode':
308 $descr = __('Unicode') . ' (' . __('multilingual') . ')';
309 break;
310 case 'bin':
311 $is_bin = true;
312 case 'general':
313 switch ($parts[0]) {
314 // Unicode charsets
315 case 'ucs2':
316 case 'utf8':
317 $descr = __('Unicode') . ' (' . __('multilingual') . ')';
318 break;
319 // West European charsets
320 case 'ascii':
321 case 'cp850':
322 case 'dec8':
323 case 'hp8':
324 case 'latin1':
325 case 'macroman':
326 $descr = __('West European') . ' (' . __('multilingual') . ')';
327 break;
328 // Central European charsets
329 case 'cp1250':
330 case 'cp852':
331 case 'latin2':
332 case 'macce':
333 $descr = __('Central European') . ' (' . __('multilingual') . ')';
334 break;
335 // Russian charsets
336 case 'cp866':
337 case 'koi8r':
338 $descr = __('Russian');
339 break;
340 // Simplified Chinese charsets
341 case 'gb2312':
342 case 'gbk':
343 $descr = __('Simplified Chinese');
344 break;
345 // Japanese charsets
346 case 'sjis':
347 case 'ujis':
348 case 'cp932':
349 case 'eucjpms':
350 $descr = __('Japanese');
351 break;
352 // Baltic charsets
353 case 'cp1257':
354 case 'latin7':
355 $descr = __('Baltic') . ' (' . __('multilingual') . ')';
356 break;
357 // Other
358 case 'armscii8':
359 case 'armscii':
360 $descr = __('Armenian');
361 break;
362 case 'big5':
363 $descr = __('Traditional Chinese');
364 break;
365 case 'cp1251':
366 $descr = __('Cyrillic') . ' (' . __('multilingual') . ')';
367 break;
368 case 'cp1256':
369 $descr = __('Arabic');
370 break;
371 case 'euckr':
372 $descr = __('Korean');
373 break;
374 case 'hebrew':
375 $descr = __('Hebrew');
376 break;
377 case 'geostd8':
378 $descr = __('Georgian');
379 break;
380 case 'greek':
381 $descr = __('Greek');
382 break;
383 case 'keybcs2':
384 $descr = __('Czech-Slovak');
385 break;
386 case 'koi8u':
387 $descr = __('Ukrainian');
388 break;
389 case 'latin5':
390 $descr = __('Turkish');
391 break;
392 case 'swe7':
393 $descr = __('Swedish');
394 break;
395 case 'tis620':
396 $descr = __('Thai');
397 break;
398 default:
399 $descr = __('unknown');
400 break;
402 if (!empty($is_bin)) {
403 $descr .= ', ' . __('Binary');
405 break;
406 default: $descr = __('unknown');
408 if (!empty($parts[2])) {
409 if ($parts[2] == 'ci') {
410 $descr .= ', ' . __('case-insensitive');
411 } elseif ($parts[2] == 'cs') {
412 $descr .= ', ' . __('case-sensitive');
416 return $descr;