2010033101.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Conditional upgrade for UTF8 as described in https://github.com/elgg/elgg/issues/1928
  4. */
  5. // get_version() returns the code version.
  6. // we want the DB version.
  7. $dbversion = (int) datalist_get('version');
  8. // 2009100701 was the utf8 upgrade for 1.7.
  9. // if we've already upgraded, don't try again.
  10. if ($dbversion < 2009100701) {
  11. // if the default client connection is utf8 there is no reason
  12. // to run this upgrade because the strings are already stored correctly.
  13. // start a new link to the DB to see what its defaults are.
  14. $link = mysql_connect($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpass, TRUE);
  15. mysql_select_db($CONFIG->dbname, $link);
  16. $q = "SHOW VARIABLES LIKE 'character_set_client'";
  17. $r = mysql_query($q);
  18. $client = mysql_fetch_assoc($r);
  19. $q = "SHOW VARIABLES LIKE 'character_set_connection'";
  20. $r = mysql_query($q);
  21. $connection = mysql_fetch_assoc($r);
  22. // only run upgrade if not already talking utf8.
  23. if ($client['Value'] != 'utf8' && $connection['Value'] != 'utf8') {
  24. $qs = array();
  25. $qs[] = "SET NAMES utf8";
  26. $qs[] = "ALTER TABLE {$CONFIG->dbprefix}metastrings DISABLE KEYS";
  27. $qs[] = "REPLACE INTO {$CONFIG->dbprefix}metastrings (id, string)
  28. SELECT id, unhex(hex(convert(string using latin1)))
  29. FROM {$CONFIG->dbprefix}metastrings";
  30. $qs[] = "ALTER TABLE {$CONFIG->dbprefix}metastrings ENABLE KEYS";
  31. $qs[] = "ALTER TABLE {$CONFIG->dbprefix}groups_entity DISABLE KEYS";
  32. $qs[] = "REPLACE INTO {$CONFIG->dbprefix}groups_entity (guid, name, description)
  33. SELECT guid, unhex(hex(convert(name using latin1))),
  34. unhex(hex(convert(description using latin1)))
  35. FROM {$CONFIG->dbprefix}groups_entity";
  36. $qs[] = "ALTER TABLE {$CONFIG->dbprefix}groups_entity ENABLE KEYS";
  37. $qs[] = "ALTER TABLE {$CONFIG->dbprefix}objects_entity DISABLE KEYS";
  38. $qs[] = "REPLACE INTO {$CONFIG->dbprefix}objects_entity (guid, title, description)
  39. SELECT guid, unhex(hex(convert(title using latin1))),
  40. unhex(hex(convert(description using latin1)))
  41. FROM {$CONFIG->dbprefix}objects_entity";
  42. $qs[] = "ALTER TABLE {$CONFIG->dbprefix}objects_entity ENABLE KEYS";
  43. $qs[] = "ALTER TABLE {$CONFIG->dbprefix}users_entity DISABLE KEYS";
  44. $qs[] = "REPLACE INTO {$CONFIG->dbprefix}users_entity
  45. (guid, name, username, password, salt, email, language, code,
  46. banned, last_action, prev_last_action, last_login, prev_last_login)
  47. SELECT guid, unhex(hex(convert(name using latin1))),
  48. username, password, salt, email, language, code,
  49. banned, last_action, prev_last_action, last_login, prev_last_login
  50. FROM {$CONFIG->dbprefix}users_entity";
  51. $qs[] = "ALTER TABLE {$CONFIG->dbprefix}users_entity ENABLE KEYS";
  52. foreach ($qs as $q) {
  53. if (!update_data($q)) {
  54. throw new \Exception('Couldn\'t execute upgrade query: ' . $q);
  55. }
  56. }
  57. }
  58. }