DAViCal
instance_range.php
1 <?php
2 
3 require_once("RRule.php");
4 require_once("vCalendar.php");
5 
10 function update_instance_ranges($collection_dav_name) {
11  // This can take a while, since vCalendar parsing is very slow, and we're
12  // going to parse every event in the collection!
13  //
14  // Since we might be doing this during a caldav request which we'd rather not
15  // have fail, we increase the execution time limit to prevent timeouts
16  set_time_limit(120);
17 
18  $qry = new AwlQuery();
19 
20  $in_transaction = ($qry->TransactionState() == 1);
21  if ( ! $in_transaction ) $qry->Begin();
22 
23  $qry->QDo(
24  "SELECT d.dav_id, c.collection_id, d.caldav_data, c.timezone, i.first_instance_start, i.last_instance_end
25  FROM caldav_data d
26  INNER JOIN collection c ON d.collection_id = c.collection_id
27  INNER JOIN calendar_item i ON d.collection_id = i.collection_id AND d.dav_id = i.dav_id
28  WHERE c.dav_name = :dav_name",
29  [":dav_name" => $collection_dav_name]
30  );
31 
32  while( $row = $qry->Fetch() ) {
33  try {
34  $range = getVCalendarRange(new vCalendar($row->caldav_data), $row->timezone);
35  } catch (Exception $e) {
36  dbg_error_log('instance_range','Error parsing calendar item, skipping update');
37  continue;
38  }
39 
40  $new_start = isset($range->from) ? $range->from->UTC() : null;
41  $new_end = isset($range->until) ? $range->until->UTC() : null;
42 
43  if ($new_start != $row->first_instance_start || $new_end != $row->last_instance_end) {
44  $inner_qry = new AwlQuery(
45  "UPDATE calendar_item
46  SET first_instance_start = :first_instance_start,
47  last_instance_end = :last_instance_end
48  WHERE collection_id = :collection_id AND dav_id = :dav_id",
49  [
50  ":dav_id" => $row->dav_id,
51  ":collection_id" => $row->collection_id,
52 
53  ":first_instance_start" => $new_start,
54  ":last_instance_end" => $new_end
55  ]
56  );
57  $inner_qry->Exec('UpdateInstanceRange',__LINE__,__FILE__);
58  }
59  }
60 
61  if ( ! $in_transaction ) $qry->Commit();
62 }
63 
64 ?>