Charsets.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * MySQL charset metadata and manipulations
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin;
  10. use PhpMyAdmin\Charsets\Charset;
  11. use PhpMyAdmin\Charsets\Collation;
  12. /**
  13. * Class used to manage MySQL charsets
  14. *
  15. * @package PhpMyAdmin
  16. */
  17. class Charsets
  18. {
  19. /**
  20. * MySQL charsets map
  21. *
  22. * @var array
  23. */
  24. public static $mysqlCharsetMap = [
  25. 'big5' => 'big5',
  26. 'cp-866' => 'cp866',
  27. 'euc-jp' => 'ujis',
  28. 'euc-kr' => 'euckr',
  29. 'gb2312' => 'gb2312',
  30. 'gbk' => 'gbk',
  31. 'iso-8859-1' => 'latin1',
  32. 'iso-8859-2' => 'latin2',
  33. 'iso-8859-7' => 'greek',
  34. 'iso-8859-8' => 'hebrew',
  35. 'iso-8859-8-i' => 'hebrew',
  36. 'iso-8859-9' => 'latin5',
  37. 'iso-8859-13' => 'latin7',
  38. 'iso-8859-15' => 'latin1',
  39. 'koi8-r' => 'koi8r',
  40. 'shift_jis' => 'sjis',
  41. 'tis-620' => 'tis620',
  42. 'utf-8' => 'utf8',
  43. 'windows-1250' => 'cp1250',
  44. 'windows-1251' => 'cp1251',
  45. 'windows-1252' => 'latin1',
  46. 'windows-1256' => 'cp1256',
  47. 'windows-1257' => 'cp1257',
  48. ];
  49. /**
  50. * The charset for the server
  51. * @var Charset|null
  52. */
  53. private static $serverCharset = null;
  54. /**
  55. * @var array<string, Charset>
  56. */
  57. private static $charsets = [];
  58. /**
  59. * @var array<string, array<string, Collation>>
  60. */
  61. private static $collations = [];
  62. /**
  63. * Loads charset data from the server
  64. *
  65. * @param DatabaseInterface $dbi DatabaseInterface instance
  66. * @param boolean $disableIs Disable use of INFORMATION_SCHEMA
  67. *
  68. * @return void
  69. */
  70. private static function loadCharsets(DatabaseInterface $dbi, bool $disableIs): void
  71. {
  72. /* Data already loaded */
  73. if (count(self::$charsets) > 0) {
  74. return;
  75. }
  76. if ($disableIs) {
  77. $sql = 'SHOW CHARACTER SET';
  78. } else {
  79. $sql = 'SELECT `CHARACTER_SET_NAME` AS `Charset`,'
  80. . ' `DEFAULT_COLLATE_NAME` AS `Default collation`,'
  81. . ' `DESCRIPTION` AS `Description`,'
  82. . ' `MAXLEN` AS `Maxlen`'
  83. . ' FROM `information_schema`.`CHARACTER_SETS`';
  84. }
  85. $res = $dbi->query($sql);
  86. self::$charsets = [];
  87. while ($row = $dbi->fetchAssoc($res)) {
  88. self::$charsets[$row['Charset']] = Charset::fromServer($row);
  89. }
  90. $dbi->freeResult($res);
  91. ksort(self::$charsets, SORT_STRING);
  92. }
  93. /**
  94. * Loads collation data from the server
  95. *
  96. * @param DatabaseInterface $dbi DatabaseInterface instance
  97. * @param boolean $disableIs Disable use of INFORMATION_SCHEMA
  98. *
  99. * @return void
  100. */
  101. private static function loadCollations(DatabaseInterface $dbi, bool $disableIs): void
  102. {
  103. /* Data already loaded */
  104. if (count(self::$collations) > 0) {
  105. return;
  106. }
  107. if ($disableIs) {
  108. $sql = 'SHOW COLLATION';
  109. } else {
  110. $sql = 'SELECT `COLLATION_NAME` AS `Collation`,'
  111. . ' `CHARACTER_SET_NAME` AS `Charset`,'
  112. . ' `ID` AS `Id`,'
  113. . ' `IS_DEFAULT` AS `Default`,'
  114. . ' `IS_COMPILED` AS `Compiled`,'
  115. . ' `SORTLEN` AS `Sortlen`'
  116. . ' FROM `information_schema`.`COLLATIONS`';
  117. }
  118. $res = $dbi->query($sql);
  119. self::$collations = [];
  120. while ($row = $dbi->fetchAssoc($res)) {
  121. self::$collations[$row['Charset']][$row['Collation']] = Collation::fromServer($row);
  122. }
  123. $dbi->freeResult($res);
  124. foreach (array_keys(self::$collations) as $charset) {
  125. ksort(self::$collations[$charset], SORT_STRING);
  126. }
  127. }
  128. /**
  129. * Get current server charset
  130. *
  131. * @param DatabaseInterface $dbi DatabaseInterface instance
  132. * @param boolean $disableIs Disable use of INFORMATION_SCHEMA
  133. *
  134. * @return Charset
  135. */
  136. public static function getServerCharset(DatabaseInterface $dbi, bool $disableIs): Charset
  137. {
  138. if (self::$serverCharset !== null) {
  139. return self::$serverCharset;
  140. }
  141. self::loadCharsets($dbi, $disableIs);
  142. $serverCharset = $dbi->getVariable('character_set_server');
  143. if (! is_string($serverCharset)) {// MySQL 5.7.8 fallback, issue #15614
  144. $serverCharset = $dbi->fetchValue("SELECT @@character_set_server;");
  145. }
  146. self::$serverCharset = self::$charsets[$serverCharset];
  147. return self::$serverCharset;
  148. }
  149. /**
  150. * Get all server charsets
  151. *
  152. * @param DatabaseInterface $dbi DatabaseInterface instance
  153. * @param boolean $disableIs Disable use of INFORMATION_SCHEMA
  154. *
  155. * @return array
  156. */
  157. public static function getCharsets(DatabaseInterface $dbi, bool $disableIs): array
  158. {
  159. self::loadCharsets($dbi, $disableIs);
  160. return self::$charsets;
  161. }
  162. /**
  163. * Get all server collations
  164. *
  165. * @param DatabaseInterface $dbi DatabaseInterface instance
  166. * @param boolean $disableIs Disable use of INFORMATION_SCHEMA
  167. *
  168. * @return array
  169. */
  170. public static function getCollations(DatabaseInterface $dbi, bool $disableIs): array
  171. {
  172. self::loadCollations($dbi, $disableIs);
  173. return self::$collations;
  174. }
  175. /**
  176. * @param DatabaseInterface $dbi DatabaseInterface instance
  177. * @param bool $disableIs Disable use of INFORMATION_SCHEMA
  178. * @param string|null $name Collation name
  179. *
  180. * @return Collation|null
  181. */
  182. public static function findCollationByName(DatabaseInterface $dbi, bool $disableIs, ?string $name): ?Collation
  183. {
  184. $pieces = explode('_', (string) $name);
  185. if ($pieces === false || ! isset($pieces[0])) {
  186. return null;
  187. }
  188. $charset = $pieces[0];
  189. $collations = self::getCollations($dbi, $disableIs);
  190. return $collations[$charset][$name] ?? null;
  191. }
  192. }