Guest User

Untitled

a guest
Jan 4th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. define('EOL', PHP_EOL);
  5.  
  6. $usage = 'Usage: DBUSER DBPASS DBNAME [DBHOST]' . EOL;
  7.  
  8. $host = $argv[4] ?? 'localhost';
  9. $user = $argv[1] ?? '';
  10. $pass = $argv[2] ?? '';
  11. $dbname = $argv[3] ?? '';
  12.  
  13. if (empty($user) || empty($pass) || empty($dbname)) {
  14. die($usage);
  15. }
  16.  
  17. $converter = new Converter($host, $user, $pass, $dbname);
  18.  
  19. $converter->alterDb();
  20. $converter->alterTables();
  21. $converter->alterColumns();
  22.  
  23. $converter->latin1ToUtf8();
  24.  
  25. $converter->renameTables();
  26. $converter->changeEngine();
  27.  
  28. $converter->closeDb();
  29.  
  30.  
  31. class Converter
  32. {
  33. protected $db = null;
  34. public $dbname = null;
  35.  
  36. public function __construct($host, $user, $pass, $dbname)
  37. {
  38. $db = new mysqli($host, $user, $pass, $dbname);
  39.  
  40. if ($db->connect_error) {
  41. die('Error : ('. $db->connect_errno .') '. $db->connect_error . EOL);
  42. }
  43. $this->dbname = $dbname;
  44. $this->db = $db;
  45. }
  46.  
  47. public function alterDb()
  48. {
  49. $this->db->query('ALTER DATABASE '. $this->dbname .' CHARACTER SET utf8 COLLATE utf8_general_ci');
  50. return true;
  51. }
  52.  
  53. public function alterTables()
  54. {
  55. $tables = $this->getTables();
  56.  
  57. foreach($tables as $table) {
  58. $this->db->query('ALTER TABLE '.$table.' CHARSET=utf8, COLLATE=utf8_general_ci');
  59. }
  60. return true;
  61. }
  62.  
  63. public function alterColumns()
  64. {
  65. $tables = $this->getTables();
  66.  
  67. foreach($tables as $table) {
  68. $cols = $this->getColumns($table);
  69. foreach($cols as $col){
  70. $col['Null'] = ($col['Null'] == 'NO') ? 'NOT NULL' : 'NULL';
  71. $col['Default'] = is_null($col['Default']) ? '' : ' DEFAULT "'. $col['Default'] .'"';
  72. $this->db->query('ALTER TABLE '.$table.' CHANGE '.$col['Field'].' '.$col['Field'].' '.$col['Type'].' CHARSET utf8 COLLATE utf8_general_ci'.$col['Default'].' '. $c
  73. ol['Null']);
  74. }
  75. }
  76. return true;
  77. }
  78.  
  79. public function latin1ToUtf8()
  80. {
  81. $tables = $this->getTables();
  82.  
  83. foreach($tables as $table) {
  84. $cols = $this->getColumns($table);
  85. $ary = [];
  86. foreach($cols as $col){
  87. $ary[] = $col['Field'] .' = CONVERT(CAST(CONVERT('.$col['Field'].' USING latin1) AS BINARY) USING utf8)';
  88. }
  89.  
  90. $this->db->query('UPDATE '.$table.' SET ' . implode(', ', $ary));
  91. }
  92. return true;
  93. }
  94.  
  95. public function getColumns($table)
  96. {
  97. $rows = [];
  98. $result = $this->db->query('SHOW COLUMNS FROM '. $table .' WHERE `Type` LIKE "%char%" OR `Type` LIKE "%text"');
  99. while($row = $result->fetch_assoc()) {
  100. $rows[] = $row;
  101. }
  102. $result->free();
  103. return $rows;
  104. }
  105.  
  106. public function getTables()
  107. {
  108. $rows = [];
  109. $result = $this->db->query('SHOW TABLES');
  110. while($row = $result->fetch_assoc()) {
  111. $rows[] = $row['Tables_in_' . $this->dbname];
  112. }
  113. $result->free();
  114. return $rows;
  115. }
  116.  
  117. public function renameTables()
  118. {
  119. $tables = $this->getTables();
  120. foreach($tables as $table) {
  121. $this->db->query('RENAME TABLE '. $table .' TO '. str_replace('pega_', '', $table));
  122. }
  123. return true;
  124. }
  125.  
  126. public function changeEngine()
  127. {
  128. $tables = $this->getTables();
  129.  
  130. foreach($tables as $table) {
  131. $this->db->query('ALTER TABLE '. $table .' ENGINE=INNODB');
  132. }
  133. return true;
  134. }
  135.  
  136. public function closeDb()
  137. {
  138. $this->db->close();
  139. }
  140. }
Add Comment
Please, Sign In to add comment