aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/upgrades/2010033101.php
blob: 5c8ee036b9884a8d77fbd813a061a4a86b08e7bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/*
 * Conditional upgrade for UTF8 as described in http://trac.elgg.org/ticket/1928
 */

// get_version() returns the code version.
// we want the DB version.
$dbversion = (int) datalist_get('version');

// 2009100701 was the utf8 upgrade for 1.7.
// if we've already upgraded, don't try again.
if ($dbversion < 2009100701) {
	// if the default client connection is utf8 there is no reason
	// to run this upgrade because the strings are already stored correctly.

	// start a new link to the DB to see what its defaults are.
	$link = mysql_connect($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpass, TRUE);
	mysql_select_db($CONFIG->dbname, $link);

	$q = "SHOW VARIABLES LIKE 'character_set_client'";
	$r = mysql_query($q);
	$client = mysql_fetch_assoc($r);

	$q = "SHOW VARIABLES LIKE 'character_set_connection'";
	$r = mysql_query($q);
	$connection = mysql_fetch_assoc($r);

	// only run upgrade if not already talking utf8.
	if ($client['Value'] != 'utf8' && $connection['Value'] != 'utf8') {
		$qs = array();
		$qs[] = "SET NAMES utf8";

		$qs[] = "ALTER TABLE {$CONFIG->dbprefix}metastrings DISABLE KEYS";
		$qs[] = "REPLACE INTO {$CONFIG->dbprefix}metastrings (id, string)
			SELECT id, unhex(hex(convert(string using latin1)))
			FROM {$CONFIG->dbprefix}metastrings";
		$qs[] = "ALTER TABLE {$CONFIG->dbprefix}metastrings ENABLE KEYS";

		$qs[] = "ALTER TABLE {$CONFIG->dbprefix}groups_entity DISABLE KEYS";
		$qs[] = "REPLACE INTO {$CONFIG->dbprefix}groups_entity (guid, name, description)
			SELECT guid, unhex(hex(convert(name using latin1))),
				unhex(hex(convert(description using latin1)))
			FROM {$CONFIG->dbprefix}groups_entity";
		$qs[] = "ALTER TABLE {$CONFIG->dbprefix}groups_entity ENABLE KEYS";

		$qs[] = "ALTER TABLE {$CONFIG->dbprefix}objects_entity DISABLE KEYS";
		$qs[] = "REPLACE INTO {$CONFIG->dbprefix}objects_entity (guid, title, description)
			SELECT guid, unhex(hex(convert(title using latin1))),
				unhex(hex(convert(description using latin1)))
			FROM {$CONFIG->dbprefix}objects_entity";
		$qs[] = "ALTER TABLE {$CONFIG->dbprefix}objects_entity ENABLE KEYS";

		$qs[] = "ALTER TABLE {$CONFIG->dbprefix}users_entity DISABLE KEYS";
		$qs[] = "REPLACE INTO {$CONFIG->dbprefix}users_entity
			(guid, name, username, password, salt, email, language, code,
			banned, last_action, prev_last_action, last_login, prev_last_login)
				SELECT guid, unhex(hex(convert(name using latin1))),
					username, password, salt, email, language, code,
					banned, last_action, prev_last_action, last_login, prev_last_login
				FROM {$CONFIG->dbprefix}users_entity";
		$qs[] = "ALTER TABLE {$CONFIG->dbprefix}users_entity ENABLE KEYS";

		foreach ($qs as $q) {
			if (!update_data($q)) {
				throw new Exception('Couldn\'t execute upgrade query: ' . $q);
			}
		}
	}
}