Completed
Push — sgdt-style-layout ( f4ca52...97ec33 )
by Ilia
10:18 queued 10s
created

functions.php ➔ initialize_list()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 3
nop 1
dl 0
loc 21
ccs 0
cts 16
cp 0
crap 6
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
// Base on cp_notification
4
function initialize_list( $cutoff_timestamp ){
5
	$dbprefix = elgg_get_config('dbprefix');
6
	$message_subtype = get_subtype_id( 'object', 'messages' );
7
	$fromId = elgg_get_metastring_id('fromId'); 
8
9
	# get user guid list of old notifications and insert them all into a temp table
10
	$query_table = "CREATE TABLE tmp_notify_delete_list ( `guid` bigint(20) unsigned NOT NULL, PRIMARY KEY (`guid`) )";
11
	$query = "INSERT INTO tmp_notify_delete_list (guid) SELECT e.guid FROM {$dbprefix}entities e 
12
LEFT JOIN {$dbprefix}metadata msg_fromId on e.guid = msg_fromId.entity_guid 
13
LEFT JOIN {$dbprefix}metastrings msvfrom ON msg_fromId.value_id = msvfrom.id 
14
LEFT JOIN {$dbprefix}entities efrom ON msvfrom.string = efrom.guid 
15
WHERE e.subtype=$message_subtype AND e.time_created < {$cutoff_timestamp} 
16
AND msg_fromId.name_id='{$fromId}' AND efrom.type <> 'user'";
17
18
	try{
19
		$result_table = insert_data($query_table);
20
		$result = insert_data($query);
21
	} catch (Exception $e) {/* let mysql take care of duplicate insert attempts and trivialize leader election*/ echo $e; return 1; }
22
	
23
	return 0;
24
}
25
26
27
28
function delete_notifications(){
29
	$dbprefix = elgg_get_config('dbprefix');
30
31
	$delete_entities = "DELETE FROM {$dbprefix}entities WHERE guid IN (SELECT guid FROM tmp_notify_delete_list)";
32
	$delete_objects = "DELETE FROM {$dbprefix}objects_entity WHERE guid IN (SELECT guid FROM tmp_notify_delete_list)";
33
	$delete_metadata = "DELETE FROM {$dbprefix}metadata WHERE entity_guid IN (SELECT guid FROM tmp_notify_delete_list)";
34
	$cleanup_tmp = "DROP TABLE tmp_notify_delete_list";
35
36
37
	delete_data($delete_entities);
38
	delete_data($delete_objects);
39
	delete_data($delete_metadata);
40
	delete_data($cleanup_tmp);
41
}