Merge branch 'QA_4_4' into QA_4_5
[phpmyadmin.git] / libraries / mysql_charsets.lib.php
blobc8072292c91c07f799021f53e25343d98c688537
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Shared code for mysql charsets
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Generate charset dropdown box
15 * @param int $type Type
16 * @param string $name Element name
17 * @param string $id Element id
18 * @param null|string $default Default value
19 * @param bool $label Label
20 * @param bool $submitOnChange Submit on change
22 * @return string
24 function PMA_generateCharsetDropdownBox($type = PMA_CSDROPDOWN_COLLATION,
25 $name = null, $id = null, $default = null, $label = true,
26 $submitOnChange = false
27 ) {
28 global $mysql_charsets, $mysql_charsets_descriptions,
29 $mysql_charsets_available, $mysql_collations, $mysql_collations_available;
31 if (empty($name)) {
32 if ($type == PMA_CSDROPDOWN_COLLATION) {
33 $name = 'collation';
34 } else {
35 $name = 'character_set';
39 $return_str = '<select lang="en" dir="ltr" name="'
40 . htmlspecialchars($name) . '"'
41 . (empty($id) ? '' : ' id="' . htmlspecialchars($id) . '"')
42 . ($submitOnChange ? ' class="autosubmit"' : '') . '>' . "\n";
43 if ($label) {
44 $return_str .= '<option value="">'
45 . ($type == PMA_CSDROPDOWN_COLLATION ? __('Collation') : __('Charset'))
46 . '</option>' . "\n";
48 $return_str .= '<option value=""></option>' . "\n";
49 foreach ($mysql_charsets as $current_charset) {
50 if (!$mysql_charsets_available[$current_charset]) {
51 continue;
53 $current_cs_descr
54 = empty($mysql_charsets_descriptions[$current_charset])
55 ? $current_charset
56 : $mysql_charsets_descriptions[$current_charset];
58 if ($type == PMA_CSDROPDOWN_COLLATION) {
59 $return_str .= '<optgroup label="' . $current_charset
60 . '" title="' . $current_cs_descr . '">' . "\n";
61 foreach ($mysql_collations[$current_charset] as $current_collation) {
62 if (!$mysql_collations_available[$current_collation]) {
63 continue;
65 $return_str .= '<option value="' . $current_collation
66 . '" title="' . PMA_getCollationDescr($current_collation) . '"'
67 . ($default == $current_collation ? ' selected="selected"' : '')
68 . '>'
69 . $current_collation . '</option>' . "\n";
71 $return_str .= '</optgroup>' . "\n";
72 } else {
73 $return_str .= '<option value="' . $current_charset
74 . '" title="' . $current_cs_descr . '"'
75 . ($default == $current_charset ? ' selected="selected"' : '') . '>'
76 . $current_charset . '</option>' . "\n";
79 $return_str .= '</select>' . "\n";
81 return $return_str;
84 /**
85 * Generate the charset query part
87 * @param string $collation Collation
89 * @return string
91 function PMA_generateCharsetQueryPart($collation)
93 if (!PMA_DRIZZLE) {
94 list($charset) = explode('_', $collation);
95 return ' CHARACTER SET ' . $charset
96 . ($charset == $collation ? '' : ' COLLATE ' . $collation);
97 } else {
98 return ' COLLATE ' . $collation;
103 * returns collation of given db
105 * @param string $db name of db
107 * @return string collation of $db
109 function PMA_getDbCollation($db)
111 if ($GLOBALS['dbi']->isSystemSchema($db)) {
112 // We don't have to check the collation of the virtual
113 // information_schema database: We know it!
114 return 'utf8_general_ci';
117 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
118 // this is slow with thousands of databases
119 $sql = PMA_DRIZZLE
120 ? 'SELECT DEFAULT_COLLATION_NAME FROM data_dictionary.SCHEMAS'
121 . ' WHERE SCHEMA_NAME = \'' . PMA_Util::sqlAddSlashes($db)
122 . '\' LIMIT 1'
123 : 'SELECT DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA'
124 . ' WHERE SCHEMA_NAME = \'' . PMA_Util::sqlAddSlashes($db)
125 . '\' LIMIT 1';
126 return $GLOBALS['dbi']->fetchValue($sql);
127 } else {
128 $GLOBALS['dbi']->selectDb($db);
129 $return = $GLOBALS['dbi']->fetchValue(
130 'SHOW VARIABLES LIKE \'collation_database\'', 0, 1
132 if ($db !== $GLOBALS['db']) {
133 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
135 return $return;
140 * returns default server collation from show variables
142 * @return string $server_collation
144 function PMA_getServerCollation()
146 return $GLOBALS['dbi']->fetchValue(
147 'SHOW VARIABLES LIKE \'collation_server\'', 0, 1
152 * returns description for given collation
154 * @param string $collation MySQL collation string
156 * @return string collation description
158 function PMA_getCollationDescr($collation)
160 if ($collation == 'binary') {
161 return __('Binary');
163 $parts = explode('_', $collation);
164 if (count($parts) == 1) {
165 $parts[1] = 'general';
166 } elseif ($parts[1] == 'ci' || $parts[1] == 'cs') {
167 $parts[2] = $parts[1];
168 $parts[1] = 'general';
170 $descr = '';
171 switch ($parts[1]) {
172 case 'bulgarian':
173 $descr = __('Bulgarian');
174 break;
175 case 'chinese':
176 if ($parts[0] == 'gb2312' || $parts[0] == 'gbk') {
177 $descr = __('Simplified Chinese');
178 } elseif ($parts[0] == 'big5') {
179 $descr = __('Traditional Chinese');
181 break;
182 case 'ci':
183 $descr = __('case-insensitive');
184 break;
185 case 'cs':
186 $descr = __('case-sensitive');
187 break;
188 case 'croatian':
189 $descr = __('Croatian');
190 break;
191 case 'czech':
192 $descr = __('Czech');
193 break;
194 case 'danish':
195 $descr = __('Danish');
196 break;
197 case 'english':
198 $descr = __('English');
199 break;
200 case 'esperanto':
201 $descr = __('Esperanto');
202 break;
203 case 'estonian':
204 $descr = __('Estonian');
205 break;
206 case 'german1':
207 $descr = __('German') . ' (' . __('dictionary') . ')';
208 break;
209 case 'german2':
210 $descr = __('German') . ' (' . __('phone book') . ')';
211 break;
212 case 'hungarian':
213 $descr = __('Hungarian');
214 break;
215 case 'icelandic':
216 $descr = __('Icelandic');
217 break;
218 case 'japanese':
219 $descr = __('Japanese');
220 break;
221 case 'latvian':
222 $descr = __('Latvian');
223 break;
224 case 'lithuanian':
225 $descr = __('Lithuanian');
226 break;
227 case 'korean':
228 $descr = __('Korean');
229 break;
230 case 'persian':
231 $descr = __('Persian');
232 break;
233 case 'polish':
234 $descr = __('Polish');
235 break;
236 case 'roman':
237 $descr = __('West European');
238 break;
239 case 'romanian':
240 $descr = __('Romanian');
241 break;
242 case 'sinhala':
243 $descr = __('Sinhalese');
244 break;
245 case 'slovak':
246 $descr = __('Slovak');
247 break;
248 case 'slovenian':
249 $descr = __('Slovenian');
250 break;
251 case 'spanish':
252 $descr = __('Spanish');
253 break;
254 case 'spanish2':
255 $descr = __('Traditional Spanish');
256 break;
257 case 'swedish':
258 $descr = __('Swedish');
259 break;
260 case 'thai':
261 $descr = __('Thai');
262 break;
263 case 'turkish':
264 $descr = __('Turkish');
265 break;
266 case 'ukrainian':
267 $descr = __('Ukrainian');
268 break;
269 case 'unicode':
270 $descr = __('Unicode') . ' (' . __('multilingual') . ')';
271 break;
272 case 'vietnamese':
273 $descr = __('Vietnamese');
274 break;
275 /** @noinspection PhpMissingBreakStatementInspection */
276 case 'bin':
277 $is_bin = true;
278 // no break; statement here, continuing with 'general' section:
279 case 'general':
280 switch ($parts[0]) {
281 // Unicode charsets
282 case 'ucs2':
283 case 'utf8':
284 case 'utf8mb4':
285 $descr = __('Unicode') . ' (' . __('multilingual') . ')';
286 break;
287 // West European charsets
288 case 'ascii':
289 case 'cp850':
290 case 'dec8':
291 case 'hp8':
292 case 'latin1':
293 case 'macroman':
294 $descr = __('West European') . ' (' . __('multilingual') . ')';
295 break;
296 // Central European charsets
297 case 'cp1250':
298 case 'cp852':
299 case 'latin2':
300 case 'macce':
301 $descr = __('Central European') . ' (' . __('multilingual') . ')';
302 break;
303 // Russian charsets
304 case 'cp866':
305 case 'koi8r':
306 $descr = __('Russian');
307 break;
308 // Simplified Chinese charsets
309 case 'gb2312':
310 case 'gbk':
311 $descr = __('Simplified Chinese');
312 break;
313 // Japanese charsets
314 case 'sjis':
315 case 'ujis':
316 case 'cp932':
317 case 'eucjpms':
318 $descr = __('Japanese');
319 break;
320 // Baltic charsets
321 case 'cp1257':
322 case 'latin7':
323 $descr = __('Baltic') . ' (' . __('multilingual') . ')';
324 break;
325 // Other
326 case 'armscii8':
327 case 'armscii':
328 $descr = __('Armenian');
329 break;
330 case 'big5':
331 $descr = __('Traditional Chinese');
332 break;
333 case 'cp1251':
334 $descr = __('Cyrillic') . ' (' . __('multilingual') . ')';
335 break;
336 case 'cp1256':
337 $descr = __('Arabic');
338 break;
339 case 'euckr':
340 $descr = __('Korean');
341 break;
342 case 'hebrew':
343 $descr = __('Hebrew');
344 break;
345 case 'geostd8':
346 $descr = __('Georgian');
347 break;
348 case 'greek':
349 $descr = __('Greek');
350 break;
351 case 'keybcs2':
352 $descr = __('Czech-Slovak');
353 break;
354 case 'koi8u':
355 $descr = __('Ukrainian');
356 break;
357 case 'latin5':
358 $descr = __('Turkish');
359 break;
360 case 'swe7':
361 $descr = __('Swedish');
362 break;
363 case 'tis620':
364 $descr = __('Thai');
365 break;
366 default:
367 $descr = __('unknown');
368 break;
370 if (!empty($is_bin)) {
371 $descr .= ', ' . __('Binary');
373 break;
374 default: $descr = __('unknown');
376 if (!empty($parts[2])) {
377 if ($parts[2] == 'ci') {
378 $descr .= ', ' . __('case-insensitive');
379 } elseif ($parts[2] == 'cs') {
380 $descr .= ', ' . __('case-sensitive');
384 return $descr;