aboutsummaryrefslogtreecommitdiff
path: root/mod/logrotate/start.php
diff options
context:
space:
mode:
Diffstat (limited to 'mod/logrotate/start.php')
-rw-r--r--mod/logrotate/start.php51
1 files changed, 26 insertions, 25 deletions
diff --git a/mod/logrotate/start.php b/mod/logrotate/start.php
index 28f14ad14..f67e419bc 100644
--- a/mod/logrotate/start.php
+++ b/mod/logrotate/start.php
@@ -21,8 +21,11 @@ function logrotate_init() {
// Register cron hook for archival of logs
elgg_register_plugin_hook_handler('cron', $period, 'logrotate_archive_cron');
- // Register cron hook for deletion of selected archived logs
- elgg_register_plugin_hook_handler('cron', $delete, 'logrotate_delete_cron');
+
+ if ($delete != 'never') {
+ // Register cron hook for deletion of selected archived logs
+ elgg_register_plugin_hook_handler('cron', $delete, 'logrotate_delete_cron');
+ }
}
/**
@@ -88,34 +91,32 @@ function logrotate_delete_cron($hook, $entity_type, $returnvalue, $params) {
/**
* This function deletes archived copies of the system logs that are older than specified.
*
- * @param int $time_of_delete An offset in seconds from now to delete (useful for log deletion)
+ * @param int $time_of_delete An offset in seconds from now to delete log tables
+ * @return bool Were any log tables deleted
*/
function log_browser_delete_log($time_of_delete) {
global $CONFIG;
- $offset = (int)$time_of_delete;
- $now = time();
-
- $ts = $now - $offset;
-
- $FLAG = 1;
- $result = mysql_query("SHOW TABLES like '{$CONFIG->dbprefix}system_log_%'");
- while ($showtablerow = mysql_fetch_array($result)) {
- //To obtain time of archival
- $log_time = explode("{$CONFIG->dbprefix}system_log_", $showtablerow[0]);
- if ($log_time < $ts) {
- //If the time of archival is before the required offset then delete
- if (!mysql_query("DROP TABLE $showtablerow[0]")) {
- $FLAG = 0;
- }
+ $cutoff = time() - (int)$time_of_delete;
+
+ $deleted_tables = false;
+ $results = get_data("SHOW TABLES like '{$CONFIG->dbprefix}system_log_%'");
+ if ($results) {
+ foreach ($results as $result) {
+ $data = (array)$result;
+ $table_name = array_shift($data);
+ // extract log table rotation time
+ $log_time = str_replace("{$CONFIG->dbprefix}system_log_", '', $table_name);
+ if ($log_time < $cutoff) {
+ if (delete_data("DROP TABLE $table_name") !== false) {
+ // delete_data returns 0 when dropping a table (false for failure)
+ $deleted_tables = true;
+ } else {
+ elgg_log("Failed to delete the log table $table_name", 'ERROR');
+ }
+ }
}
}
- //Check if the appropriate tables have been deleted and return true if yes
- if ($FLAG) {
- return true;
- } else {
- return false;
- }
-
+ return $deleted_tables;
}