PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/install/convert_utf8.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 136 lines | 78 code | 24 blank | 34 comment | 13 complexity | cb779d6a2b29167097d49422bd0a437d MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /*
  11. * Thanks to:
  12. * http://www.v-nessa.net/2007/12/06/convert-database-to-utf-8
  13. * http://developer.loftdigital.com/blog/php-utf-8-cheatsheet
  14. * http://www.mysqlperformanceblog.com/2007/12/18/fixing-column-encoding-mess-in-mysql/
  15. */
  16. //die('Comment this line...');
  17. if (php_sapi_name() === 'cli')
  18. {
  19. define('IP_ROOT_PATH', dirname(dirname($argv[0])) . '/');
  20. }
  21. define('IN_ICYPHOENIX', true);
  22. if (!defined('IP_ROOT_PATH')) define('IP_ROOT_PATH', './');
  23. if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
  24. @set_time_limit(0);
  25. @ini_set('memory_limit', '32M');
  26. require(IP_ROOT_PATH . 'config.' . PHP_EXT);
  27. define('SQL_LAYER', 'mysql4');
  28. require(IP_ROOT_PATH . 'includes/db/mysql.' . PHP_EXT);
  29. $db = new sql_db($dbhost, $dbuser, $dbpasswd, $dbname, false);
  30. if(!$db->db_connect_id)
  31. {
  32. trigger_error('Database connection failed', E_USER_ERROR);
  33. }
  34. // HTML HEADER - BEGIN
  35. echo("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
  36. echo("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
  37. echo("<head>\n");
  38. echo("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n");
  39. echo("<meta name=\"author\" content=\"Icy Phoenix Team\" />\n");
  40. echo("<title>Icy Phoenix :: UTF-8 Conversion</title>\n");
  41. echo("</head>\n");
  42. echo("<body>\n");
  43. echo("<div style=\"font-family: 'Lucida Grande', 'Trebuchet MS', Verdana, Helvetica, Arial, sans-serif; font-size: 10px;\">\n");
  44. echo("<b style=\"color: #dd2222;\">DB Conversion to UTF-8 in progress, please do not stop the browser until the whole process is finished...</b><br />\n<br />\n<br />\n");
  45. // HTML HEADER - END
  46. flush();
  47. $sql = "ALTER DATABASE {$db->sql_escape($dbname)}
  48. CHARACTER SET utf8
  49. DEFAULT CHARACTER SET utf8
  50. COLLATE utf8_bin
  51. DEFAULT COLLATE utf8_bin";
  52. $db->sql_query($sql) or die($db->sql_error());
  53. $sql = "SHOW TABLES";
  54. $result = $db->sql_query($sql) or die($db->sql_error());
  55. while ($row = $db->sql_fetchrow($result))
  56. {
  57. // This assignment doesn't work...
  58. //$table = $row[0];
  59. $current_item = each($row);
  60. $table = $current_item['value'];
  61. reset($row);
  62. if (strpos($table, $table_prefix) === 0)
  63. {
  64. $sql = "ALTER TABLE {$db->sql_escape($table)}
  65. DEFAULT CHARACTER SET utf8
  66. COLLATE utf8_bin";
  67. //ENGINE=MYISAM";
  68. $db->sql_query($sql) or die($db->sql_error());
  69. echo("&bull;&nbsp;Table&nbsp;<b style=\"color: #dd2222;\">$table</b> converted to UTF-8<br />\n");
  70. $sql = "SHOW FIELDS FROM {$db->sql_escape($table)}";
  71. $result_fields = $db->sql_query($sql);
  72. while ($row_fields = $db->sql_fetchrow($result_fields))
  73. {
  74. // These assignments don't work...
  75. /*
  76. $field_name = $row_fields[0];
  77. $field_type = $row_fields[1];
  78. $field_null = $row_fields[2];
  79. $field_key = $row_fields[3];
  80. $field_default = $row_fields[4];
  81. $field_extra = $row_fields[5];
  82. */
  83. $field_name = $row_fields['Field'];
  84. $field_type = $row_fields['Type'];
  85. $field_null = $row_fields['Null'];
  86. $field_key = $row_fields['Key'];
  87. $field_default = $row_fields['Default'];
  88. $field_extra = $row_fields['Extra'];
  89. // Let's remove BLOB and BINARY for now...
  90. //if ((strpos(strtolower($field_type), 'char') !== false) || (strpos(strtolower($field_type), 'text') !== false) || (strpos(strtolower($field_type), 'blob') !== false) || (strpos(strtolower($field_type), 'binary') !== false))
  91. if ((strpos(strtolower($field_type), 'char') !== false) || (strpos(strtolower($field_type), 'text') !== false))
  92. {
  93. //$sql_fields = "ALTER TABLE {$db->sql_escape($table)} CHANGE " . $db->sql_escape($field_name) . " " . $db->sql_escape($field_name) . " " . $db->sql_escape($field_type) . " CHARACTER SET utf8 COLLATE utf8_bin";
  94. $sql_fields = "ALTER TABLE {$db->sql_escape($table)} CHANGE " . $db->sql_escape($field_name) . " " . $db->sql_escape($field_name) . " " . $db->sql_escape($field_type) . " CHARACTER SET utf8 COLLATE utf8_bin " . (($field_null != 'YES') ? "NOT " : "") . "NULL DEFAULT " . (($field_default != 'None') ? ((!empty($field_default) || !is_null($field_default)) ? (is_string($field_default) ? ("'" . $db->sql_escape($field_default) . "'") : $field_default) : (($field_null != 'YES') ? "''" : "NULL")) : "''");
  95. $db->sql_query($sql_fields);
  96. echo("\t&nbsp;&nbsp;&raquo;&nbsp;Field&nbsp;<b style=\"color: #4488aa;\">$field_name</b> (in table <b style=\"color: #009900;\">$table</b>) converted to UTF-8<br />\n");
  97. }
  98. }
  99. echo("<br />\n");
  100. flush();
  101. }
  102. }
  103. $db->sql_close();
  104. echo("<br />\n<br />\n<br />\n<b style=\"color: #dd2222;\">Work Complete!!!</b><br />\n");
  105. // HTML FOOTER - BEGIN
  106. echo("</div>\n");
  107. echo("</body>\n");
  108. echo("</html>\n");
  109. // HTML FOOTER - BEGIN
  110. flush();
  111. exit;
  112. ?>