aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/upgrades/2011052801.php
blob: d68e0118ef40f37646c09663fcbd50dba1333dda (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
<?php
/**
 * Make sure all users have the relationship member_of_site
 */
global $DB_QUERY_CACHE, $ENTITY_CACHE;
$db_prefix = get_config('dbprefix');

$limit = 100;

$q = "SELECT e.* FROM {$db_prefix}entities e
	WHERE e.type = 'user' AND e.guid NOT IN (
		SELECT guid_one FROM {$db_prefix}entity_relationships
			WHERE guid_two = 1 AND relationship = 'member_of_site'
		)
	LIMIT $limit";

$users = get_data($q);

while ($users) {
	$DB_QUERY_CACHE = $ENTITY_CACHE = array();

	// do manually to not trigger any events because these aren't new users.
	foreach ($users as $user) {
		$rel_q = "INSERT INTO {$db_prefix}entity_relationships VALUES (
			'',
			'$user->guid',
			'member_of_site',
			'$user->site_guid',
			'$user->time_created'
		)";

		insert_data($rel_q);
	}

	// every time we run this query we've just reduced the rows it returns by $limit
	// so don't pass an offset.
	$q = "SELECT e.* FROM {$db_prefix}entities e
		WHERE e.type = 'user' AND e.guid NOT IN (
			SELECT guid_one FROM {$db_prefix}entity_relationships
				WHERE guid_two = 1 AND relationship = 'member_of_site'
			)
		LIMIT $limit";

	$users = get_data($q);
}