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