From bf47c4ef15a470f799faa1ddb6989bf75ebdcf31 Mon Sep 17 00:00:00 2001 From: cash Date: Fri, 17 May 2013 14:32:29 -0400 Subject: Fixes #5490 log table deletion works now --- mod/logrotate/start.php | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'mod/logrotate/start.php') diff --git a/mod/logrotate/start.php b/mod/logrotate/start.php index 313cf1fd5..f67e419bc 100644 --- a/mod/logrotate/start.php +++ b/mod/logrotate/start.php @@ -21,7 +21,7 @@ function logrotate_init() { // Register cron hook for archival of logs elgg_register_plugin_hook_handler('cron', $period, 'logrotate_archive_cron'); - + if ($delete != 'never') { // Register cron hook for deletion of selected archived logs elgg_register_plugin_hook_handler('cron', $delete, 'logrotate_delete_cron'); @@ -91,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; } -- cgit v1.2.3