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