Move purging of old calendar events to use less API and do more with the DB directly. Hopefully will be a little easier on the DB.

This commit is contained in:
Nathan Gray 2010-06-14 11:45:00 +00:00
parent e02d6c4ea9
commit 61099d4a89
2 changed files with 50 additions and 29 deletions

View File

@ -2219,34 +2219,7 @@ class calendar_boupdate extends calendar_bo
*/
function purge($age)
{
$query = array(
'end' => strtotime("-$age years", time()),
'enum_recuring' => false,
'users' => array_keys($GLOBALS['egw']->accounts->search(array()))
);
$events = $this->search($query);
foreach($events as $event)
{
// Delete single events or recurring events where all ocurrences are old enough
if(!$event['recur_type'] || $event['recur_type'] && $event['recur_enddate'] && $event['recur_enddate'] <= $query['end'])
{
$this->delete($event['id'], 0, true);
}
}
// If preserve history is on, we'll need to do this again to completely remove it
$config = config::read('phpgwapi');
if($config['calendar_delete_history']) {
$query['filter'] = 'deleted';
$events = $this->search($query);
foreach($events as $event)
{
if(!$event['recur_type'] || $event['recur_type'] && $event['recur_enddate'] && $event['recur_enddate'] <= $query['end'])
{
$this->delete($event['id'], 0, true);
}
}
}
$time = strtotime("-$age years", time());
$this->so->purge($time);
}
}

View File

@ -1490,6 +1490,54 @@ ORDER BY cal_user_type, cal_usre_id
}
}
/**
* Delete all events that were before the given date.
*
* Recurring events that finished before the date will be deleted.
* Recurring events that span the date will be ignored. Non-recurring
* events before the date will be deleted.
*
* @param int $date
*/
function purge($date)
{
// Start with egw_cal, it's the easiest
$sql = "DELETE egw_cal.* FROM egw_cal
LEFT JOIN egw_cal_repeats ON
egw_cal_repeats.cal_id = egw_cal.cal_id
WHERE egw_cal_repeats.cal_id IS NULL || (recur_enddate < $date && recur_enddate != 0)";
$this->db->query($sql, __LINE__, __FILE__);
// Get a list of what we just deleted for links
$ids = array();
foreach($this->db->select(
'egw_cal_dates',
array('cal_id'),
array('cal_id NOT IN (SELECT cal_id FROM egw_cal)'),
__LINE__, __FILE__, false
) as $row)
{
$ids[] = $row['cal_id'];
}
// Cascade to other tables
foreach($this->all_tables as $table)
{
if($table == 'egw_cal') continue;
$this->db->delete($table, array('cal_id NOT IN (SELECT cal_id FROM egw_cal)'), __LINE__, __FILE__, 'calendar');
}
// Sync
$sql = 'UPDATE egw_api_content_history
SET sync_deleted=NOW()
WHERE sync_appname = \'calendar\'
AND sync_contentid NOT IN (SELECT cal_id from egw_cal)';
$this->db->query($sql, __LINE__, __FILE__);
// Links
egw_link::unlink('', 'calendar', $ids);
}
/**
* read the alarms of a calendar-event specified by $cal_id
*