2010061501.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * utf8 database conversion and file merging for usernames with multibyte chars
  4. *
  5. */
  6. // check that we need to do the utf8 conversion
  7. // C&P logic from 2010033101
  8. $dbversion = (int) datalist_get('version');
  9. if ($dbversion < 2009100701) {
  10. // start a new link to the DB to see what its defaults are.
  11. $link = mysql_connect($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpass, TRUE);
  12. mysql_select_db($CONFIG->dbname, $link);
  13. $q = "SHOW VARIABLES LIKE 'character_set_client'";
  14. $r = mysql_query($q);
  15. $client = mysql_fetch_assoc($r);
  16. $q = "SHOW VARIABLES LIKE 'character_set_connection'";
  17. $r = mysql_query($q);
  18. $connection = mysql_fetch_assoc($r);
  19. // only run upgrade if not already talking utf8
  20. if ($client['Value'] != 'utf8' && $connection['Value'] != 'utf8') {
  21. $qs = array();
  22. $qs[] = "SET NAMES utf8";
  23. $qs[] = "ALTER TABLE {$CONFIG->dbprefix}users_entity DISABLE KEYS";
  24. $qs[] = "REPLACE INTO {$CONFIG->dbprefix}users_entity
  25. (guid, name, username, password, salt, email, language, code,
  26. banned, admin, last_action, prev_last_action, last_login, prev_last_login)
  27. SELECT guid, name, unhex(hex(convert(username using latin1))),
  28. password, salt, email, language, code,
  29. banned, admin, last_action, prev_last_action, last_login, prev_last_login
  30. FROM {$CONFIG->dbprefix}users_entity";
  31. $qs[] = "ALTER TABLE {$CONFIG->dbprefix}users_entity ENABLE KEYS";
  32. foreach ($qs as $q) {
  33. if (!update_data($q)) {
  34. throw new \Exception('Couldn\'t execute upgrade query: ' . $q);
  35. }
  36. }
  37. global $ENTITY_CACHE;
  38. /**
  39. Upgrade file locations
  40. */
  41. // new connection to force into utf8 mode to get the old name
  42. $link = mysql_connect($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpass, TRUE);
  43. mysql_select_db($CONFIG->dbname, $link);
  44. // must be the first command
  45. mysql_query("SET NAMES utf8");
  46. $users = mysql_query("SELECT guid, username FROM {$CONFIG->dbprefix}users_entity
  47. WHERE username != ''", $link);
  48. while ($user = mysql_fetch_object($users)) {
  49. $ENTITY_CACHE = array();
  50. $to = $CONFIG->dataroot . user_file_matrix($user->guid);
  51. foreach (array('1_0', '1_1', '1_6') as $version) {
  52. $function = "file_matrix_$version";
  53. $from = $CONFIG->dataroot . $function($user->username);
  54. merge_directories($from, $to, $move = TRUE, $preference = 'from');
  55. }
  56. }
  57. }
  58. }