1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#!/usr/bin/env php
<?php
/**
* Script to refresh the pending alarm times for the next alarm instance.
*
* @package davical
* @subpackage alarms
* @author Andrew McMillan <andrew@morphoss.com>
* @copyright Morphoss Ltd
* @license http://gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$script_file = __FILE__;
chdir(str_replace('/scripts/refresh-alarms.php','/htdocs',$script_file));
$_SERVER['SERVER_NAME'] = 'localhost';
/**
* Call with something like e.g.:
*
* scripts/refresh_alarms.php -p P1800D -f P1D
*
*/
$args = (object) array();
$args->debug = false;
$args->set_last = false;
$args->slow_query_threshold = false; // Won't set higher threshold by default
$args->future = 'P400D';
$args->near_past = 'P1D';
$debugging = null;
function parse_arguments() {
global $args;
$opts = getopt( 'f:p:q:s:d:lh' );
foreach( $opts AS $k => $v ) {
switch( $k ) {
case 'f': $args->future = $v; break;
case 'p': $args->near_past = $v; break;
case 'q': $args->slow_query_threshold = $v; break;
case 's': $_SERVER['SERVER_NAME'] = $v; break;
case 'd': $args->debug = true; $debugging = explode(',',$v); break;
case 'l': $args->set_last = true; break;
case 'h': usage(); break;
default: $args->{$k} = $v;
}
}
}
function usage() {
echo <<<USAGE
Usage:
refresh-alarms.php [-s server.domain.tld] [other options]
-s <server> The servername to be used to identify the DAViCal configuration file.
-p <duration> Near past period to review for finding recently last instances: default 1 days ('P1D')
-f <duration> Future period to consider for finding future alarms: default ~5 years ('P2000D')
-l Try to set the 'last' alarm date in historical alarms
-q <secs> Warn about slow queries which take longer than this many seconds (use AWL default).
-d xxx Enable debugging where 'xxx' is a comma-separated list of debug subsystems
USAGE;
exit(0);
}
parse_arguments();
if ( $args->debug && is_array($debugging )) {
foreach( $debugging AS $v ) {
$c->dbg[$v] = 1;
}
}
$args->near_past = '-' . $args->near_past;
require_once("./always.php");
if ( $args->slow_query_threshold !== false ) {
$c->default_query_warning_threshold = $args->slow_query_threshold;
}
require_once('AwlQuery.php');
require_once('RRule.php');
require_once('vCalendar.php');
/**
* Essentially what we are doing is:
*
UPDATE calendar_alarm
SET next_trigger = (SELECT rrule_event_instances_range(
dtstart + icalendar_interval_to_SQL(replace(trigger,'TRIGGER:','')),
rrule,
current_timestamp, current_timestamp + '2 days'::interval,
1)
LIMIT 1)
FROM calendar_item
WHERE calendar_alarm.dav_id = calendar_item.dav_id
AND next_trigger is null
AND rrule IS NOT NULL
*/
$expand_range_start = new RepeatRuleDateTime(gmdate('Ymd\THis\Z'));
$expand_range_end = new RepeatRuleDateTime(gmdate('Ymd\THis\Z'));
$expand_range_end->modify( $args->future );
$earliest = clone($expand_range_start);
$earliest->modify( $args->near_past );
if ( $args->debug ) printf( "Looking for event instances between '%s' and '%s'\n", $earliest->UTC(), $expand_range_end->UTC() );
$sql = 'SELECT * FROM calendar_alarm JOIN calendar_item USING (dav_id) JOIN caldav_data USING (dav_id) WHERE rrule IS NOT NULL AND next_trigger IS NULL';
if ( $args->debug ) printf( "%s\n", $sql );
$qry = new AwlQuery( $sql );
if ( $qry->Exec() && $qry->rows() ) {
while( $alarm = $qry->Fetch() ) {
if ( $args->debug ) printf( "refresh: Processing alarm for '%s' based on '%s','%s', '%s'\n",
$alarm->dav_name, $alarm->dtstart, $alarm->rrule, $alarm->trigger );
$ic = new vComponent( $alarm->caldav_data );
$expanded = expand_event_instances( $ic, $earliest, $expand_range_end );
$expanded->MaskComponents( array( 'VEVENT'=>1, 'VTODO'=>1, 'VJOURNAL'=>1 ) );
$instances = $expanded->GetComponents();
$null = null;
$trigger = new vProperty(null, $null, $alarm->trigger );
$related = $trigger->GetParameterValue('RELATED');
$first = new RepeatRuleDateTime($alarm->dtstart);
$first->modify( $trigger->Value() );
$next = null;
$last = null;
foreach( $instances AS $k => $component ) {
$when = new RepeatRuleDateTime( $component->GetPValue('DTSTART') ); // a UTC value
if ( $args->debug ) printf( "refresh: Looking at event instance on '%s'\n", $when->UTC() );
if ( $related == 'END' ) {
$when->modify( $component->GetPValue('DURATION') );
}
$when->modify( $trigger->Value() );
if ( $when > $expand_range_start && $when < $expand_range_end && (!isset($next) || $when < $next) ) {
$next = clone($when);
}
if ( $args->set_last && (!isset($last) || $when > $last) ) {
$last = clone($when);
}
}
$trigger_type = $trigger->GetParameterValue('VALUE');
if ( $trigger_type == 'DATE' || $trigger_type == 'DATE-TIME' || preg_match('{^\d{8}T\d{6}Z?$}', $trigger->Value()) ) {
$first = new RepeatRuleDateTime($trigger);
if ( $first > $expand_range_start && (empty($next) || $first < $next ) )
$next = $first;
else if ( empty($next) ) {
if ( $args->set_last && (empty($last) || $first > $last) )
$last = $first;
}
}
if ( $args->set_last && !isset($last) && (!isset($next) || $next < $expand_range_start) ) {
$vc = new vCalendar( $alarm->caldav_data );
$range = getVCalendarRange($vc);
if ( isset($range->until) && $range->until < $earliest ) $last = $range->until;
}
if ( isset($next) && $next < $expand_range_end ) {
if ( $args->debug ) printf( "refresh: Found next alarm instance on '%s'\n", $next->UTC() );
$sql = 'UPDATE calendar_alarm SET next_trigger = :next WHERE dav_id = :id AND count = :count AND component = :component';
$update = new AwlQuery( $sql, array( ':next' => $next->UTC(), ':id' => $alarm->dav_id, ':count' => $alarm->count, ':component' => $alarm->component ) );
$update->Exec('refresh-alarms', __LINE__, __FILE__ );
}
else if ( $args->set_last && isset($last) && $last < $earliest ) {
if ( $args->debug ) printf( "refresh: Found past final alarm instance on '%s'\n", $last->UTC() );
$sql = 'UPDATE calendar_alarm SET next_trigger = :last WHERE dav_id = :id AND count = :count AND component = :component';
$update = new AwlQuery( $sql, array( ':last' => $last->UTC(), ':id' => $alarm->dav_id, ':count' => $alarm->count, ':component' => $alarm->component ) );
$update->Exec('refresh-alarms', __LINE__, __FILE__ );
}
else if ( $args->debug && isset($next) && $next < $expand_range_end ) {
printf( "refresh: Found next alarm instance on '%s' after '%s'\n", $next->UTC(), $expand_range_end->UTC() );
}
}
}
|