PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/classes/Charsets.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 236 lines | 133 code | 36 blank | 67 comment | 13 complexity | 411212e72a48877f2b8559421f8a74b5 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. <?php
  2. /**
  3. * MySQL charset metadata and manipulations
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin;
  7. use PhpMyAdmin\Charsets\Charset;
  8. use PhpMyAdmin\Charsets\Collation;
  9. use function __;
  10. use function array_keys;
  11. use function count;
  12. use function explode;
  13. use function is_string;
  14. use function ksort;
  15. use const SORT_STRING;
  16. /**
  17. * Class used to manage MySQL charsets
  18. */
  19. class Charsets
  20. {
  21. /**
  22. * MySQL charsets map
  23. *
  24. * @var array
  25. */
  26. public static $mysqlCharsetMap = [
  27. 'big5' => 'big5',
  28. 'cp-866' => 'cp866',
  29. 'euc-jp' => 'ujis',
  30. 'euc-kr' => 'euckr',
  31. 'gb2312' => 'gb2312',
  32. 'gbk' => 'gbk',
  33. 'iso-8859-1' => 'latin1',
  34. 'iso-8859-2' => 'latin2',
  35. 'iso-8859-7' => 'greek',
  36. 'iso-8859-8' => 'hebrew',
  37. 'iso-8859-8-i' => 'hebrew',
  38. 'iso-8859-9' => 'latin5',
  39. 'iso-8859-13' => 'latin7',
  40. 'iso-8859-15' => 'latin1',
  41. 'koi8-r' => 'koi8r',
  42. 'shift_jis' => 'sjis',
  43. 'tis-620' => 'tis620',
  44. 'utf-8' => 'utf8',
  45. 'windows-1250' => 'cp1250',
  46. 'windows-1251' => 'cp1251',
  47. 'windows-1252' => 'latin1',
  48. 'windows-1256' => 'cp1256',
  49. 'windows-1257' => 'cp1257',
  50. ];
  51. /**
  52. * The charset for the server
  53. *
  54. * @var Charset|null
  55. */
  56. private static $serverCharset = null;
  57. /** @var array<string, Charset> */
  58. private static $charsets = [];
  59. /** @var array<string, array<string, Collation>> */
  60. private static $collations = [];
  61. /**
  62. * Loads charset data from the server
  63. *
  64. * @param DatabaseInterface $dbi DatabaseInterface instance
  65. * @param bool $disableIs Disable use of INFORMATION_SCHEMA
  66. */
  67. private static function loadCharsets(DatabaseInterface $dbi, bool $disableIs): void
  68. {
  69. /* Data already loaded */
  70. if (count(self::$charsets) > 0) {
  71. return;
  72. }
  73. $sql = 'SELECT `CHARACTER_SET_NAME` AS `Charset`,'
  74. . ' `DEFAULT_COLLATE_NAME` AS `Default collation`,'
  75. . ' `DESCRIPTION` AS `Description`,'
  76. . ' `MAXLEN` AS `Maxlen`'
  77. . ' FROM `information_schema`.`CHARACTER_SETS`';
  78. if ($disableIs) {
  79. $sql = 'SHOW CHARACTER SET';
  80. }
  81. $res = $dbi->query($sql);
  82. self::$charsets = [];
  83. while ($row = $dbi->fetchAssoc($res)) {
  84. self::$charsets[$row['Charset']] = Charset::fromServer($row);
  85. }
  86. $dbi->freeResult($res);
  87. ksort(self::$charsets, SORT_STRING);
  88. }
  89. /**
  90. * Loads collation data from the server
  91. *
  92. * @param DatabaseInterface $dbi DatabaseInterface instance
  93. * @param bool $disableIs Disable use of INFORMATION_SCHEMA
  94. */
  95. private static function loadCollations(DatabaseInterface $dbi, bool $disableIs): void
  96. {
  97. /* Data already loaded */
  98. if (count(self::$collations) > 0) {
  99. return;
  100. }
  101. $sql = 'SELECT `COLLATION_NAME` AS `Collation`,'
  102. . ' `CHARACTER_SET_NAME` AS `Charset`,'
  103. . ' `ID` AS `Id`,'
  104. . ' `IS_DEFAULT` AS `Default`,'
  105. . ' `IS_COMPILED` AS `Compiled`,'
  106. . ' `SORTLEN` AS `Sortlen`'
  107. . ' FROM `information_schema`.`COLLATIONS`';
  108. if ($disableIs) {
  109. $sql = 'SHOW COLLATION';
  110. }
  111. $res = $dbi->query($sql);
  112. self::$collations = [];
  113. while ($row = $dbi->fetchAssoc($res)) {
  114. self::$collations[$row['Charset']][$row['Collation']] = Collation::fromServer($row);
  115. }
  116. $dbi->freeResult($res);
  117. foreach (array_keys(self::$collations) as $charset) {
  118. ksort(self::$collations[$charset], SORT_STRING);
  119. }
  120. }
  121. /**
  122. * Get current server charset
  123. *
  124. * @param DatabaseInterface $dbi DatabaseInterface instance
  125. * @param bool $disableIs Disable use of INFORMATION_SCHEMA
  126. */
  127. public static function getServerCharset(DatabaseInterface $dbi, bool $disableIs): Charset
  128. {
  129. if (self::$serverCharset !== null) {
  130. return self::$serverCharset;
  131. }
  132. self::loadCharsets($dbi, $disableIs);
  133. $serverCharset = $dbi->getVariable('character_set_server');
  134. if (! is_string($serverCharset)) {// MySQL 5.7.8 fallback, issue #15614
  135. $serverCharset = $dbi->fetchValue('SELECT @@character_set_server;');
  136. }
  137. self::$serverCharset = self::$charsets[$serverCharset] ?? null;
  138. // MySQL 8.0.11+ fallback, issue #16931
  139. if (self::$serverCharset === null && $serverCharset === 'utf8mb3') {
  140. // See: https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-11.html#mysqld-8-0-11-charset
  141. // The utf8mb3 character set will be replaced by utf8mb4 in a future MySQL version.
  142. // The utf8 character set is currently an alias for utf8mb3,
  143. // but will at that point become a reference to utf8mb4.
  144. // To avoid ambiguity about the meaning of utf8,
  145. // consider specifying utf8mb4 explicitly for character set references instead of utf8.
  146. // Warning: #3719 'utf8' is currently an alias for the character set UTF8MB3 [...]
  147. return self::$charsets['utf8'];
  148. }
  149. if (self::$serverCharset === null) {// Fallback in case nothing is found
  150. return Charset::fromServer(
  151. [
  152. 'Charset' => __('Unknown'),
  153. 'Description' => __('Unknown'),
  154. ]
  155. );
  156. }
  157. return self::$serverCharset;
  158. }
  159. /**
  160. * Get all server charsets
  161. *
  162. * @param DatabaseInterface $dbi DatabaseInterface instance
  163. * @param bool $disableIs Disable use of INFORMATION_SCHEMA
  164. *
  165. * @return array
  166. */
  167. public static function getCharsets(DatabaseInterface $dbi, bool $disableIs): array
  168. {
  169. self::loadCharsets($dbi, $disableIs);
  170. return self::$charsets;
  171. }
  172. /**
  173. * Get all server collations
  174. *
  175. * @param DatabaseInterface $dbi DatabaseInterface instance
  176. * @param bool $disableIs Disable use of INFORMATION_SCHEMA
  177. *
  178. * @return array
  179. */
  180. public static function getCollations(DatabaseInterface $dbi, bool $disableIs): array
  181. {
  182. self::loadCollations($dbi, $disableIs);
  183. return self::$collations;
  184. }
  185. /**
  186. * @param DatabaseInterface $dbi DatabaseInterface instance
  187. * @param bool $disableIs Disable use of INFORMATION_SCHEMA
  188. * @param string|null $name Collation name
  189. */
  190. public static function findCollationByName(DatabaseInterface $dbi, bool $disableIs, ?string $name): ?Collation
  191. {
  192. $pieces = explode('_', (string) $name);
  193. if ($pieces === false || ! isset($pieces[0])) {
  194. return null;
  195. }
  196. $charset = $pieces[0];
  197. $collations = self::getCollations($dbi, $disableIs);
  198. return $collations[$charset][$name] ?? null;
  199. }
  200. }