start.php ➔ garbagecollector_orphaned_metastrings()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 0
dl 0
loc 48
rs 8.5123
c 0
b 0
f 0
1
<?php
2
/**
3
 * Elgg garbage collector.
4
 *
5
 * @package ElggGarbageCollector
6
 */
7
8
elgg_register_event_handler('init', 'system', 'garbagecollector_init');
9
10
function garbagecollector_init() {
11
	$period = elgg_get_plugin_setting('period', 'garbagecollector');
12
	switch ($period) {
13
		case 'weekly':
14
		case 'monthly':
15
		case 'yearly':
16
			break;
17
		default:
18
			$period = 'monthly';
19
	}
20
21
	// Register cron hook
22
	elgg_register_plugin_hook_handler('cron', $period, 'garbagecollector_cron');
23
	
24
	elgg_register_plugin_hook_handler('gc', 'system', 'garbagecollector_orphaned_metastrings');
25
	elgg_register_plugin_hook_handler('gc', 'system', 'garbagecollector_entities');
26
}
27
28
/**
29
 * Cron job
30
 */
31
function garbagecollector_cron($hook, $entity_type, $returnvalue, $params) {
32
33
	echo elgg_echo('garbagecollector') . "\n";
34
35
	// Now, because we are nice, trigger a plugin hook to let other plugins do some GC
36
	$rv = true;
37
	$period = elgg_get_plugin_setting('period','garbagecollector');
38
	elgg_trigger_plugin_hook('gc', 'system', array('period' => $period));
39
40
	// Now we optimize all tables
41
//	$tables = get_db_tables();
42
//	foreach ($tables as $table) {
43
//		echo elgg_echo('garbagecollector:optimize', array($table));
44
//
45
//		if (optimize_table($table) !== false) {
46
//			echo elgg_echo('garbagecollector:ok');
47
//		} else {
48
//			echo elgg_echo('garbagecollector:error');
49
//		}
50
//
51
//		echo "\n";
52
//	}
53
54
	echo elgg_echo('garbagecollector:done');
55
}
56
57
/**
58
 * Get array of table names
59
 *
60
 * @return array
61
 */
62
function garbagecollector_get_tables() {
63
	static $tables;
64
65
	if (isset($tables)) {
66
		return $tables;
67
	}
68
69
	$table_prefix = elgg_get_config('dbprefix');
70
	$result = get_data("SHOW TABLES LIKE '$table_prefix%'");
71
72
	$tables = array();
73 View Code Duplication
	if (is_array($result) && !empty($result)) {
74
		foreach ($result as $row) {
75
			$row = (array) $row;
76
			if (is_array($row) && !empty($row)) {
77
				foreach ($row as $element) {
78
					$tables[] = $element;
79
				}
80
			}
81
		}
82
	}
83
84
	return $tables;
85
}
86
87
/**
88
 * Optimize a table
89
 *
90
 * @param string $table Database table name
91
 * @return bool
92
 */
93
function garbagecollector_optimize_table($table) {
94
	$table = sanitise_string($table);
95
	return update_data("OPTIMIZE TABLE $table");
96
}
97
98
/**
99
 * Garbage collect stub and fragments from any broken delete/create calls
100
 *
101
 * @return void
102
 */
103
function garbagecollector_entities() {
104
	$dbprefix = elgg_get_config('dbprefix');
105
106
	$tables = array(
107
		'site' => 'sites_entity',
108
		'object' => 'objects_entity',
109
		'group' => 'groups_entity',
110
		'user' => 'users_entity',
111
	);
112
113
	foreach ($tables as $type => $table) {
114
		delete_data("DELETE FROM {$dbprefix}{$table}
115
		WHERE guid NOT IN (SELECT guid FROM {$dbprefix}entities)");
116
		delete_data("DELETE FROM {$dbprefix}entities
117
		WHERE type = '$type' AND guid NOT IN (SELECT guid FROM {$dbprefix}{$table})");
118
	}
119
}
120
121
/**
122
 * Delete any orphaned entries in metastrings.
123
 *
124
 * @return void
125
 */
126
function garbagecollector_orphaned_metastrings() {
127
	$dbprefix = elgg_get_config('dbprefix');
128
	
129
	// Garbage collect metastrings
130
	echo elgg_echo('garbagecollector:gc:metastrings');
131
132
	// If memcache is enabled then we need to flush it of deleted values
133
	if (is_memcache_available()) {
134
		$select_query = "
135
		SELECT * FROM {$dbprefix}metastrings WHERE
136
		(
137
			(id NOT IN (SELECT name_id FROM {$dbprefix}metadata)) AND
138
			(id NOT IN (SELECT value_id FROM {$dbprefix}metadata)) AND
139
			(id NOT IN (SELECT name_id FROM {$dbprefix}annotations)) AND
140
			(id NOT IN (SELECT value_id FROM {$dbprefix}annotations))
141
		)";
142
143
		$dead = get_data($select_query);
144
		if ($dead) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dead of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
145
			static $metastrings_memcache;
146
			
147
			if (!$metastrings_memcache) {
148
				$metastrings_memcache = new \ElggMemcache('metastrings_memcache');
149
			}
150
151
			foreach ($dead as $d) {
152
				$metastrings_memcache->delete($d->string);
153
			}
154
		}
155
	}
156
157
	$query = "
158
		DELETE FROM {$dbprefix}metastrings WHERE
159
		(
160
			(id NOT IN (SELECT name_id FROM {$dbprefix}metadata)) AND
161
			(id NOT IN (SELECT value_id FROM {$dbprefix}metadata)) AND
162
			(id NOT IN (SELECT name_id FROM {$dbprefix}annotations)) AND
163
			(id NOT IN (SELECT value_id FROM {$dbprefix}annotations))
164
		)";
165
166
	$result = delete_data($query);
167
	
168
	if ($result !== false) {
169
		echo elgg_echo('garbagecollector:ok');
170
	} else {
171
		echo elgg_echo('garbagecollector:error');
172
	}
173
}
174