Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

mod/garbagecollector/start.php (1 issue)

1
<?php
2
/**
3
 * Elgg garbage collector.
4
 *
5
 * @package ElggGarbageCollector
6
 */
7
8
/**
9
 * Garbage collection init
10
 *
11
 * @return void
12
 */
13
function garbagecollector_init() {
14 31
	$period = elgg_get_plugin_setting('period', 'garbagecollector');
15 31
	switch ($period) {
16 31
		case 'weekly':
17 31
		case 'monthly':
18 31
		case 'yearly':
19
			break;
20
		default:
21 31
			$period = 'monthly';
22
	}
23
24
	// Register cron hook
25 31
	elgg_register_plugin_hook_handler('cron', $period, 'garbagecollector_cron');
26 31
}
27
28
/**
29
 * Cron job
30
 *
31
 * @param string $hook        'cron'
32
 * @param string $type        interval
33
 * @param mixed  $returnvalue current return value
34
 * @param array  $params      supplied params
35
 *
36
 * @return void
37
 */
38
function garbagecollector_cron($hook, $type, $returnvalue, $params) {
1 ignored issue
show
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

38
function garbagecollector_cron($hook, /** @scrutinizer ignore-unused */ $type, $returnvalue, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
40
	echo elgg_echo('garbagecollector') . "\n";
41
42
	// Now, because we are nice, trigger a plugin hook to let other plugins do some GC
43
	$rv = true;
44
	$period = elgg_get_plugin_setting('period', 'garbagecollector');
45
	elgg_trigger_plugin_hook('gc', 'system', ['period' => $period]);
46
47
	// Now we optimize all tables
48
	$tables = garbagecollector_get_tables();
49
	foreach ($tables as $table) {
50
		echo elgg_echo('garbagecollector:optimize', [$table]);
51
52
		if (garbagecollector_optimize_table($table) !== false) {
53
			echo elgg_echo('ok');
54
		} else {
55
			echo elgg_echo('error');
56
		}
57
58
		echo "\n";
59
	}
60
61
	echo elgg_echo('garbagecollector:done');
62
}
63
64
/**
65
 * Get array of table names
66
 *
67
 * @return array
68
 */
69
function garbagecollector_get_tables() {
70
	static $tables;
71
72
	if (isset($tables)) {
73
		return $tables;
74
	}
75
76
	$table_prefix = elgg_get_config('dbprefix');
77
	$result = get_data("SHOW TABLES LIKE '$table_prefix%'");
78
79
	$tables = [];
80
	if (is_array($result) && !empty($result)) {
81
		foreach ($result as $row) {
82
			$row = (array) $row;
83
			if (is_array($row) && !empty($row)) {
84
				foreach ($row as $element) {
85
					$tables[] = $element;
86
				}
87
			}
88
		}
89
	}
90
91
	return $tables;
92
}
93
94
/**
95
 * Optimize a table
96
 *
97
 * @param string $table Database table name
98
 * @return bool
99
 */
100
function garbagecollector_optimize_table($table) {
101
	$table = sanitise_string($table);
102
	return update_data("OPTIMIZE TABLE $table");
103
}
104
105
return function() {
106 18
	elgg_register_event_handler('init', 'system', 'garbagecollector_init');
107
};
108