2.11.11.3 release
[phpmyadmin/arisferyanto.git] / libraries / mysql_charsets.lib.php
blobe5f20f011fc3b887847868bf85b9cdcc8b870092
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 */
7 if (! defined('PHPMYADMIN')) {
8 exit;
11 /**
14 if (PMA_MYSQL_INT_VERSION >= 40100){
16 $res = PMA_DBI_query('SHOW CHARACTER SET;');
18 $mysql_charsets = array();
19 while ($row = PMA_DBI_fetch_assoc($res)) {
20 $mysql_charsets[] = $row['Charset'];
21 // never used
22 //$mysql_charsets_maxlen[$row['Charset']] = $row['Maxlen'];
23 $mysql_charsets_descriptions[$row['Charset']] = $row['Description'];
25 @PMA_DBI_free_result($res);
27 $mysql_charsets_count = count($mysql_charsets);
28 sort($mysql_charsets, SORT_STRING);
30 $mysql_collations = array_flip($mysql_charsets);
31 $mysql_default_collations = $mysql_collations_flat = $mysql_charsets_available = $mysql_collations_available = array();
33 $res = PMA_DBI_query('SHOW COLLATION;');
34 while ($row = PMA_DBI_fetch_assoc($res)) {
35 if (!is_array($mysql_collations[$row['Charset']])) {
36 $mysql_collations[$row['Charset']] = array($row['Collation']);
37 } else {
38 $mysql_collations[$row['Charset']][] = $row['Collation'];
40 $mysql_collations_flat[] = $row['Collation'];
41 if ((isset($row['D']) && $row['D'] == 'Y') || (isset($row['Default']) && $row['Default'] == 'Yes')) {
42 $mysql_default_collations[$row['Charset']] = $row['Collation'];
44 //$mysql_collations_available[$row['Collation']] = !isset($row['Compiled']) || $row['Compiled'] == 'Yes';
45 $mysql_collations_available[$row['Collation']] = TRUE;
46 $mysql_charsets_available[$row['Charset']] = !empty($mysql_charsets_available[$row['Charset']]) || !empty($mysql_collations_available[$row['Collation']]);
48 @PMA_DBI_free_result($res);
49 unset($res, $row);
51 $mysql_collations_count = count($mysql_collations_flat);
52 sort($mysql_collations_flat, SORT_STRING);
53 foreach ($mysql_collations AS $key => $value) {
54 sort($mysql_collations[$key], SORT_STRING);
55 reset($mysql_collations[$key]);
57 unset($key, $value);
59 define('PMA_CSDROPDOWN_COLLATION', 0);
60 define('PMA_CSDROPDOWN_CHARSET', 1);
62 function PMA_generateCharsetDropdownBox($type = PMA_CSDROPDOWN_COLLATION, $name = null, $id = null, $default = null, $label = TRUE, $indent = 0, $submitOnChange = FALSE, $displayUnavailable = FALSE) {
63 global $mysql_charsets, $mysql_charsets_descriptions, $mysql_charsets_available, $mysql_collations, $mysql_collations_available;
65 if (empty($name)) {
66 if ($type == PMA_CSDROPDOWN_COLLATION) {
67 $name = 'collation';
68 } else {
69 $name = 'character_set';
73 $spacer = '';
74 for ($i = 1; $i <= $indent; $i++) $spacer .= ' ';
76 $return_str = $spacer . '<select xml:lang="en" dir="ltr" name="' . htmlspecialchars($name) . '"' . (empty($id) ? '' : ' id="' . htmlspecialchars($id) . '"') . ($submitOnChange ? ' onchange="this.form.submit();"' : '') . '>' . "\n";
77 if ($label) {
78 $return_str .= $spacer . ' <option value="">' . ($type == PMA_CSDROPDOWN_COLLATION ? $GLOBALS['strCollation'] : $GLOBALS['strCharset']) . '</option>' . "\n";
80 $return_str .= $spacer . ' <option value="">&nbsp;</option>' . "\n";
81 foreach ($mysql_charsets as $current_charset) {
82 if (!$mysql_charsets_available[$current_charset]) {
83 continue;
85 $current_cs_descr = empty($mysql_charsets_descriptions[$current_charset]) ? $current_charset : $mysql_charsets_descriptions[$current_charset];
86 if ($type == PMA_CSDROPDOWN_COLLATION) {
87 $return_str .= $spacer . ' <optgroup label="' . $current_charset . '" title="' . $current_cs_descr . '">' . "\n";
88 foreach ($mysql_collations[$current_charset] as $current_collation) {
89 if (!$mysql_collations_available[$current_collation]) {
90 continue;
92 $return_str .= $spacer . ' <option value="' . $current_collation . '" title="' . PMA_getCollationDescr($current_collation) . '"' . ($default == $current_collation ? ' selected="selected"' : '') . '>' . $current_collation . '</option>' . "\n";
94 $return_str .= $spacer . ' </optgroup>' . "\n";
95 } else {
96 $return_str .= $spacer . ' <option value="' . $current_charset . '" title="' . $current_cs_descr . '"' . ($default == $current_charset ? ' selected="selected"' : '') . '>' . $current_charset . '</option>' . "\n";
99 $return_str .= $spacer . '</select>' . "\n";
101 return $return_str;
104 function PMA_generateCharsetQueryPart($collation) {
105 list($charset) = explode('_', $collation);
106 return ' CHARACTER SET ' . $charset . ($charset == $collation ? '' : ' COLLATE ' . $collation);
110 * returns collation of given db
112 * @uses PMA_MYSQL_INT_VERSION
113 * @uses PMA_DBI_fetch_value()
114 * @uses PMA_DBI_select_db()
115 * @uses PMA_sqlAddSlashes()
116 * @uses $GLOBALS['db']
117 * @param string $db name of db
118 * @return string collation of $db
120 function PMA_getDbCollation($db) {
121 if (PMA_MYSQL_INT_VERSION >= 50000 && $db == 'information_schema') {
122 // We don't have to check the collation of the virtual
123 // information_schema database: We know it!
124 return 'utf8_general_ci';
126 if (PMA_MYSQL_INT_VERSION >= 50006) {
127 // Since MySQL 5.0.6, we don't have to parse SHOW CREATE DATABASE anymore.
128 return PMA_DBI_fetch_value('SELECT DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = \'' . PMA_sqlAddSlashes($db) . '\' LIMIT 1;');
129 } elseif (PMA_MYSQL_INT_VERSION >= 40101) {
130 // MySQL 4.1.0 does not support seperate charset settings
131 // for databases.
132 PMA_DBI_select_db($db);
133 // the query does not work if this string is in double quotes
134 // and MySQL is running in ANSI mode
135 $return = PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'collation_database\'', 0, 1);
136 if ($db !== $GLOBALS['db']) {
137 PMA_DBI_select_db($GLOBALS['db']);
139 return $return;
141 return '';
144 } else {
145 function PMA_getDbCollation($db) { return PMA_getServerCollation(); }
149 * returns default server collation from show variables
151 * @uses PMA_DBI_fetch_value()
152 * @return string $server_collation
154 function PMA_getServerCollation() {
155 return PMA_DBI_fetch_value(
156 'SHOW VARIABLES LIKE \'collation_server\'', 0, 1);
160 * returns description for given collation
162 * @uses is_array()
163 * @uses explode()
164 * @uses count()
165 * @uses $GLOBALS['str[Languages|Sorting]']
167 * @param string $collation MySQL collation string
168 * @return string collation description
170 function PMA_getCollationDescr($collation) {
171 static $collation_cache;
173 if (!is_array($collation_cache)) {
174 $collation_cache = array();
175 } elseif (isset($collation_cache[$collation])) {
176 return $collation_cache[$collation];
179 if ($collation == 'binary') {
180 return $GLOBALS['strBinary'];
182 $parts = explode('_', $collation);
183 if (count($parts) == 1) {
184 $parts[1] = 'general';
185 } elseif ($parts[1] == 'ci' || $parts[1] == 'cs') {
186 $parts[2] = $parts[1];
187 $parts[1] = 'general';
189 $descr = '';
190 switch ($parts[1]) {
191 case 'bulgarian':
192 $descr = $GLOBALS['strBulgarian'];
193 break;
194 case 'chinese':
195 if ($parts[0] == 'gb2312' || $parts[0] == 'gbk') {
196 $descr = $GLOBALS['strSimplifiedChinese'];
197 } elseif ($parts[0] == 'big5') {
198 $descr = $GLOBALS['strTraditionalChinese'];
200 break;
201 case 'ci':
202 $descr = $GLOBALS['strCaseInsensitive'];
203 break;
204 case 'cs':
205 $descr = $GLOBALS['strCaseSensitive'];
206 break;
207 case 'croatian':
208 $descr = $GLOBALS['strCroatian'];
209 break;
210 case 'czech':
211 $descr = $GLOBALS['strCzech'];
212 break;
213 case 'danish':
214 $descr = $GLOBALS['strDanish'];
215 break;
216 case 'english':
217 $descr = $GLOBALS['strEnglish'];
218 break;
219 case 'esperanto':
220 $descr = $GLOBALS['strEsperanto'];
221 break;
222 case 'estonian':
223 $descr = $GLOBALS['strEstonian'];
224 break;
225 case 'german1':
226 $descr = $GLOBALS['strGerman'] . ' (' . $GLOBALS['strDictionary'] . ')';
227 break;
228 case 'german2':
229 $descr = $GLOBALS['strGerman'] . ' (' . $GLOBALS['strPhoneBook'] . ')';
230 break;
231 case 'hungarian':
232 $descr = $GLOBALS['strHungarian'];
233 break;
234 case 'icelandic':
235 $descr = $GLOBALS['strIcelandic'];
236 break;
237 case 'japanese':
238 $descr = $GLOBALS['strJapanese'];
239 break;
240 case 'latvian':
241 $descr = $GLOBALS['strLatvian'];
242 break;
243 case 'lithuanian':
244 $descr = $GLOBALS['strLithuanian'];
245 break;
246 case 'korean':
247 $descr = $GLOBALS['strKorean'];
248 break;
249 case 'persian':
250 $descr = $GLOBALS['strPersian'];
251 break;
252 case 'polish':
253 $descr = $GLOBALS['strPolish'];
254 break;
255 case 'roman':
256 $descr = $GLOBALS['strWestEuropean'];
257 break;
258 case 'romanian':
259 $descr = $GLOBALS['strRomanian'];
260 break;
261 case 'slovak':
262 $descr = $GLOBALS['strSlovak'];
263 break;
264 case 'slovenian':
265 $descr = $GLOBALS['strSlovenian'];
266 break;
267 case 'spanish':
268 $descr = $GLOBALS['strSpanish'];
269 break;
270 case 'spanish2':
271 $descr = $GLOBALS['strTraditionalSpanish'];
272 break;
273 case 'swedish':
274 $descr = $GLOBALS['strSwedish'];
275 break;
276 case 'thai':
277 $descr = $GLOBALS['strThai'];
278 break;
279 case 'turkish':
280 $descr = $GLOBALS['strTurkish'];
281 break;
282 case 'ukrainian':
283 $descr = $GLOBALS['strUkrainian'];
284 break;
285 case 'unicode':
286 $descr = $GLOBALS['strUnicode'] . ' (' . $GLOBALS['strMultilingual'] . ')';
287 break;
288 case 'bin':
289 $is_bin = TRUE;
290 case 'general':
291 switch ($parts[0]) {
292 // Unicode charsets
293 case 'ucs2':
294 case 'utf8':
295 $descr = $GLOBALS['strUnicode'] . ' (' . $GLOBALS['strMultilingual'] . ')';
296 break;
297 // West European charsets
298 case 'ascii':
299 case 'cp850':
300 case 'dec8':
301 case 'hp8':
302 case 'latin1':
303 case 'macroman':
304 $descr = $GLOBALS['strWestEuropean'] . ' (' . $GLOBALS['strMultilingual'] . ')';
305 break;
306 // Central European charsets
307 case 'cp1250':
308 case 'cp852':
309 case 'latin2':
310 case 'macce':
311 $descr = $GLOBALS['strCentralEuropean'] . ' (' . $GLOBALS['strMultilingual'] . ')';
312 break;
313 // Russian charsets
314 case 'cp866':
315 case 'koi8r':
316 $descr = $GLOBALS['strRussian'];
317 break;
318 // Simplified Chinese charsets
319 case 'gb2312':
320 case 'gbk':
321 $descr = $GLOBALS['strSimplifiedChinese'];
322 break;
323 // Japanese charsets
324 case 'sjis':
325 case 'ujis':
326 case 'cp932':
327 case 'eucjpms':
328 $descr = $GLOBALS['strJapanese'];
329 break;
330 // Baltic charsets
331 case 'cp1257':
332 case 'latin7':
333 $descr = $GLOBALS['strBaltic'] . ' (' . $GLOBALS['strMultilingual'] . ')';
334 break;
335 // Other
336 case 'armscii8':
337 case 'armscii':
338 $descr = $GLOBALS['strArmenian'];
339 break;
340 case 'big5':
341 $descr = $GLOBALS['strTraditionalChinese'];
342 break;
343 case 'cp1251':
344 $descr = $GLOBALS['strCyrillic'] . ' (' . $GLOBALS['strMultilingual'] . ')';
345 break;
346 case 'cp1256':
347 $descr = $GLOBALS['strArabic'];
348 break;
349 case 'euckr':
350 $descr = $GLOBALS['strKorean'];
351 break;
352 case 'hebrew':
353 $descr = $GLOBALS['strHebrew'];
354 break;
355 case 'geostd8':
356 $descr = $GLOBALS['strGeorgian'];
357 break;
358 case 'greek':
359 $descr = $GLOBALS['strGreek'];
360 break;
361 case 'keybcs2':
362 $descr = $GLOBALS['strCzechSlovak'];
363 break;
364 case 'koi8u':
365 $descr = $GLOBALS['strUkrainian'];
366 break;
367 case 'latin5':
368 $descr = $GLOBALS['strTurkish'];
369 break;
370 case 'swe7':
371 $descr = $GLOBALS['strSwedish'];
372 break;
373 case 'tis620':
374 $descr = $GLOBALS['strThai'];
375 break;
376 default:
377 $descr = $GLOBALS['strUnknown'];
378 break;
380 if (!empty($is_bin)) {
381 $descr .= ', ' . $GLOBALS['strBinary'];
383 break;
384 default: $descr = $GLOBALS['strUnknown'];
386 if (!empty($parts[2])) {
387 if ($parts[2] == 'ci') {
388 $descr .= ', ' . $GLOBALS['strCaseInsensitive'];
389 } elseif ($parts[2] == 'cs') {
390 $descr .= ', ' . $GLOBALS['strCaseSensitive'];
394 $collation_cache[$collation] = $descr;
395 return $descr;