blib/lib/iCal/Parser.pm

filter: Statementstmt 98.6% 216 / 219 Branchbran 84.5% 71 / 84 Conditioncond 67.7% 42 / 62 MC/DCmcdc 27.0% 10 / 37 Subroutinesub 96.5% 28 / 29 Podpod 23.5% 4 / 17 totaltotal 82.8% 371 / 448 SCAR 43.6
CC
73
Cov
90%
VEVENT
25.4
convert_value
14.8
new
7.2
CRAP 78.1
↑ Summary
1 # $Id$
2 package iCal::Parser;
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 our $VERSION='1.20';
6  
7 4 our @ISA = qw (Exporter);
8  
9 4 ▶use DateTime::Format::ICal;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Text::vFile::asData;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # mapping of ical entries to datatypes
17 4 our %TYPES=(dates=>{DTSTAMP=>1,DTSTART=>1,DTEND=>1,COMPLETED=>1,
18 'RECURRENCE-ID'=>1,EXDATE=>1,DUE=>1,
19 'LAST-MODIFIED'=>1,
20 },
21 durations=>{DURATION=>1},
22 arrays=>{EXDATE=>1,ATTENDEE=>1},
23 hash=>{'ATTENDEE'=>1, ORGANIZER=>1},
24 );
25  
26 4 our %defaults=(debug=>0,span=>undef,start=>undef,end=>undef,months=>60,tz=>'local');
27  
28 4 our $dfmt=DateTime::Format::ICal->new;
29 4 our $parser=Text::vFile::asData->new;
30 sub new {
31 31 ▶ my ($class, %params) = @_;
Subroutine new: called CC=7 SCAR=19.8
Pod documented
32  
33 31 my $self=bless {%defaults, %params,
34 ical=>{cals=>[],events=>{},todos=>[]},
35 _today=>DateTime->now,_calid=>0,
36 }, $class;
37 #set range, allow passed in dates as DateTimes or strings
38 31 ▶ my $start=$params{start}||DateTime->now->truncate(to=>'year');
Condition66%2/3
$params{'start'} || 'DateTime'->now->truncate('to', 'year')
l!l&&r!l&&!r
2920
MC/DC0%0/2
$params{'start'} || 'DateTime'->now->truncate('to', 'year')
$params{'start'} 'DateTime'->now->truncate('to', 'year')
Truth table66%2/3
A || B
A= $params{'start'}B= 'DateTime'->now->truncate('to', 'year')
ABresult
1X1
011
000
39 31 ▶ $start=$dfmt->parse_datetime($start) unless ref $start;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $start 27 4 31
40 31 ▶ my $end=$params{end}||$start->clone->add(months=>$self->{months});
Condition66%2/3
$params{'end'} || $start->clone->add('months', $self->{'months'})
l!l&&r!l&&!r
2290
MC/DC0%0/2
$params{'end'} || $start->clone->add('months', $self->{'months'})
$params{'end'} $start->clone->add('months', $self->{'months'})
Truth table66%2/3
A || B
A= $params{'end'}B= $start->clone->add('months', $self->{'months'})
ABresult
1X1
011
000
41 31 ▶ $end=$dfmt->parse_datetime($end) unless ref $end;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $end 1 30 31
42 31 ▶ $self->{span}||=DateTime::Span->new(start=>$start, end=>$end);
Condition66%2/3
$self->{"span"} ||= "DateTime::Span"->new("start", $start, "end", $end)
l!l&&r!l&&!r
1300
MC/DC0%0/2
$self->{"span"} ||= "DateTime::Span"->new("start", $start, "end", $end)
$self->{"span"} "DateTime::Span"->new("start", $start, "end", $end)
Truth table66%2/3
A ||= B
A= $self->{"span"}B= "DateTime::Span"->new("start", $start, "end", $end)
ABresult
1X1
011
000
43  
44 $self->{tz}=DateTime::TimeZone->new(name=>$self->{tz})
45 31 ▶ unless ref $self->{tz};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"tz"} 31 0 31
46  
47 29 return ($self);
48 }
49 sub parse {
50 29 ▶ my $self=shift;
Subroutine parse: called CC=4 SCAR=13.9
Pod documented
51  
52 29 foreach my $file (@_) {
53 32 ▶ my $fh=ref $file ? $file
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 2 30 32
Condition50%1/2
"IO::File"->new($file, "r") or die "Can't open $file, $!"
l!l
300
MC/DC0%0/1
"IO::File"->new($file, "r") or die "Can't open $file, $!"
"IO::File"->new($file, "r")
Truth table50%1/2
A
A= "IO::File"->new($file, "r")
Aresult
11
00
54 : IO::File->new($file,'r') || die "Can\'t open $file, $!";
55 32 my $data=$parser->parse($fh);
56 32 undef $fh;
57  
58 32 $self->VCALENDAR($data->{objects}[0],$file);
59 32 $self->add_objects($data->{objects}[0]);
60 32 $self->update_recurrences;
61 }
62 29 return $self->{ical};
63 }
64 sub parse_files {
65 1 ▶ return parse(@_);
Subroutine parse_files: called CC=1 SCAR=0.0
Pod documented
66 }
67 sub parse_strings {
68 1 ▶ my $self=shift;
Subroutine parse_strings: called CC=1 SCAR=0.0
Pod documented
69 1 return $self->parse((map { IO::String->new($_) } @_));
70 }
71 sub calendar {
72 0 ▶ return shift->{ical};
Subroutine calendar: not called CC=1 SCAR=6.9
Pod undocumented
73 }
74 sub VCALENDAR {
75 32 ▶ my($self,$cal,$file)=@_;
Subroutine VCALENDAR: called CC=5 SCAR=16.2
Pod undocumented
76  
77 32 my %props=();
78 32 $self->{recurrences}=[];
79 32 $self->map_properties(\%props,$cal);
80 32 ▶ $props{'X-WR-TIMEZONE'}||=$self->{tz}->name;
Condition66%2/3
$props{"X-WR-TIMEZONE"} ||= $self->{"tz"}->name
l!l&&r!l&&!r
3290
MC/DC0%0/2
$props{"X-WR-TIMEZONE"} ||= $self->{"tz"}->name
$props{"X-WR-TIMEZONE"} $self->{"tz"}->name
Truth table66%2/3
A ||= B
A= $props{"X-WR-TIMEZONE"}B= $self->{"tz"}->name
ABresult
1X1
011
000
81 32 $props{index}=++$self->{_calid};
82 32 ▶ $props{'X-WR-RELCALID'}||=$self->{_calid};
Condition66%2/3
$props{"X-WR-RELCALID"} ||= $self->{"_calid"}
l!l&&r!l&&!r
3290
MC/DC0%0/2
$props{"X-WR-RELCALID"} ||= $self->{"_calid"}
$props{"X-WR-RELCALID"} $self->{"_calid"}
Truth table66%2/3
A ||= B
A= $props{"X-WR-RELCALID"}B= $self->{"_calid"}
ABresult
1X1
011
000
83 32 ▶ $props{'X-WR-CALNAME'}||= ref $file
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 2 27 29
Condition66%2/3
$props{"X-WR-CALNAME"} ||= ref $file ? qq[Calendar $self->{"_calid"}] : fileparse($file, qr/\.\w+/)
l!l&&r!l&&!r
3290
MC/DC0%0/2
$props{"X-WR-CALNAME"} ||= ref $file ? qq[Calendar $self->{"_calid"}] : fileparse($file, qr/\.\w+/)
$props{"X-WR-CALNAME"} ref $file ? qq[Calendar $self->{"_calid"}] : fileparse($file, qr/\.\w+/)
Truth table66%2/3
A ||= B
A= $props{"X-WR-CALNAME"}B= ref $file ? qq[Calendar $self->{"_calid"}] : fileparse($file, qr/\.\w+/)
ABresult
1X1
011
000
84 ? "Calendar $self->{_calid}" : fileparse($file,qr{\.\w+});
85  
86 32 push @{$self->{ical}{cals}},\%props;
87 }
88 sub VTODO {
89 10 ▶ my($self,$todo)=@_;
Subroutine VTODO: called CC=3 SCAR=11.0
Pod undocumented
90 10 ▶ return if $self->{no_todos};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_todos"} 0 10 10
91  
92 10 my $t={idref=>$self->_cur_calid};
93 10 $self->map_properties($t,$todo);
94 10 ▶ $t->{PRIORITY}||=99;
Condition100%2/2
$t->{"PRIORITY"} ||= 99
l!l
73
MC/DC100%1/1
$t->{"PRIORITY"} ||= 99
$t->{"PRIORITY"}
Truth table100%2/2
A
A= $t->{"PRIORITY"}
Aresult
11
00
95  
96 10 $self->add_objects($todo,$t);
97 10 push @{ $self->{ical}{todos} }, $t;
98 }
99 sub VEVENT {
100 87 ▶ my($self,$event)=@_;
Subroutine VEVENT: called CC=25 SCAR=32.3
Pod undocumented
101 87 ▶ return if $self->{no_events};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_events"} 0 87 87
102  
103 87 my %e=(idref=>$self->_cur_calid);
104  
105 87 $self->map_properties(\%e,$event);
106 87 $self->add_objects($event,\%e);
107  
108 87 my $start=$e{DTSTART};
109 87 ▶ return if $start > $self->{span}->end;
Branch: 1 branch
BranchTrueFalseTotal
if $start > $self->{"span"}->end 4 83 87
110  
111 warn "Event: @e{qw(UID DTSTART SUMMARY)}\n"
112 83 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 83 83
113  
114 # stolen from Text::vFile::asData example
115 83 ▶ $e{allday}=1 if _param($event,'DTSTART','VALUE')||'' eq 'DATE';
Branch: 1 branch
BranchTrueFalseTotal
if 34 49 83
Condition100%2/2
_param($event, "DTSTART", "VALUE") or
l!l
3449
MC/DC100%1/1
_param($event, "DTSTART", "VALUE") or
_param($event, "DTSTART", "VALUE")
Truth table100%2/2
A
A= _param($event, "DTSTART", "VALUE")
Aresult
11
00
116  
117 #is it a rule that an event must contain either a duration or end?
118 # answer: no, it's not (cpan bug #25232)
119 83 my $end=$e{DTEND};
120 83 ▶ my $duration=$end ? $end-$start : delete $e{DURATION};
Branch: 1 branch
BranchTrueFalseTotal
$end ? : 55 28 83
121 83 ▶ $duration ||= DateTime::Duration->new(days=> $e{allday} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$e{'allday'} ? : 1 1 2
Condition66%2/3
$duration ||= "DateTime::Duration"->new("days", $e{"allday"} ? 1 : 0)
l!l&&r!l&&!r
8120
MC/DC0%0/2
$duration ||= "DateTime::Duration"->new("days", $e{"allday"} ? 1 : 0)
$duration "DateTime::Duration"->new("days", $e{"allday"} ? 1 : 0)
Truth table66%2/3
A ||= B
A= $durationB= "DateTime::Duration"->new("days", $e{"allday"} ? 1 : 0)
ABresult
1X1
011
000
122 83 ▶ $e{DTEND}||=$start+$duration;
Condition66%2/3
$e{"DTEND"} ||= $start + $duration
l!l&&r!l&&!r
55280
MC/DC0%0/2
$e{"DTEND"} ||= $start + $duration
$e{"DTEND"} $start + $duration
Truth table66%2/3
A ||= B
A= $e{"DTEND"}B= $start + $duration
ABresult
1X1
011
000
123 83 ▶ $e{hours}=_hours($duration) unless $e{allday};
Branch: 1 branch
BranchTrueFalseTotal
unless $e{"allday"} 49 34 83
124  
125 #build recurrence sets
126 83 my $set;
127 83 ▶ if (my $rid=$e{'RECURRENCE-ID'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rid = $e{"RECURRENCE-ID"}) 10 73 83
128 10 ▶ return if $start < $self->{span}->start;
Branch: 1 branch
BranchTrueFalseTotal
if $start < $self->{"span"}->start 0 10 10
129 10 push @{ $self->{recurrences} }, \%e;
130 10 return;
131 }
132 73 ▶ if (my $recur=delete $e{RRULE}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $recur = delete $e{'RRULE'}) { } 19 54 73
elsif ($end) { } 42 12 54
133 $set=$dfmt->parse_recurrence(recurrence=>$recur, dtstart=>$start,
134 #cap infinite repeats
135 19 until =>$self->{span}->end);
136 } elsif ($end) {
137 # non-rrule event possibly spanning multiple days,
138 # expand into multiple events
139 42 my $diff=$end-$start;
140 42 ▶ if (!$e{allday} && $end->day > $start->day) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $e{"allday"} and $end->day > $start->day) 7 35 42
Condition100%3/3
not $e{"allday"} and $end->day > $start->day
!ll&&!rl&&r
3147
MC/DC100%2/2
not $e{"allday"} and $end->day > $start->day
not $e{"allday"} $end->day > $start->day
Truth table100%3/3
not(A) and B
A= not $e{"allday"}B= $end->day > $start->day
ABresult
0X0
100
111
141 7 $self->add_span(\%e);
142 7 return;
143 }
144 35 ▶ if ($diff->delta_days > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff->delta_days > 1) 5 30 35
145 # note recurrence includes last date, and allday events
146 # end at 00 on the last (non-inclusive) day, so remove it
147 # from set
148 $set=DateTime::Set->from_recurrence
149 (start=>$start,end=>$end->clone->subtract(days=>1),
150 recurrence=>sub {
151 48 ▶ return $_[0]->truncate(to=>'day')->add(days=>1)
Subroutine __ANON__: called CC=1 SCAR=0.0
152 5 });
153 # reset duration to "allday" event
154 5 $duration=DateTime::Duration->new(days=>1);
155 }
156 }
157 66 ▶ $set||=DateTime::Set->from_datetimes(dates=>[$start]);
Condition66%2/3
$set ||= "DateTime::Set"->from_datetimes("dates", [$start])
l!l&&r!l&&!r
24420
MC/DC0%0/2
$set ||= "DateTime::Set"->from_datetimes("dates", [$start])
$set "DateTime::Set"->from_datetimes("dates", [$start])
Truth table66%2/3
A ||= B
A= $setB= "DateTime::Set"->from_datetimes("dates", [$start])
ABresult
1X1
011
000
158  
159 # fix bug w/ recurrence containing no entries
160 # note that count returns "undef" for infinitely large sets.
161 66 ▶ return if defined $set->count && $set->count==0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $set->count and $set->count == 0 1 65 66
Condition66%2/3
defined $set->count and $set->count == 0
!ll&&!rl&&r
0651
MC/DC50%1/2
defined $set->count and $set->count == 0
defined $set->count $set->count == 0
Truth table66%2/3
A and B
A= defined $set->countB= $set->count == 0
ABresult
0X0
100
111
162  
163 65 ▶ if (my $dates=delete $e{'EXDATE'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dates = delete $e{"EXDATE"}) 4 61 65
164 #mozilla/sunbird set exdate to T00..., so, get first start date
165 #and set times on exdates
166 4 my $d=$set->min;
167 my $exset=DateTime::Set->from_datetimes
168 (dates=>[
169 4 map {$_->set(hour=>$d->hour,minute=>$d->minute,
170 second=>$d->second)
171 } @$dates]);
172 4 $set=$set
173 ->complement(DateTime::Set->from_datetimes(dates=>$dates));
174 }
175 65 ▶ $set=$set->intersection($self->{span}) if $self->{span};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"span"} 65 0 65
176 65 my $iter=$set->iterator;
177 65 ▶ while (my $dt=$iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dt = $iter->next) 215 65 280
178 #bug found by D. Sweet. Fix alarms on entries
179 #other than first
180 215 my $new_event={%e,DTSTART=>$dt,DTEND=>$dt+$duration};
181 $new_event->{VALARM}=_fix_alarms($new_event, $e{DTSTART})
182 215 ▶ if $new_event->{VALARM};
Branch: 1 branch
BranchTrueFalseTotal
if $new_event->{"VALARM"} 84 131 215
183 215 $self->add_event($new_event);
184 }
185 }
186 sub VALARM {
187 20 ▶ my($self,$alarm,$e)=@_;
Subroutine VALARM: called CC=3 SCAR=11.0
Pod undocumented
188  
189 20 my %a=();
190 #handle "RELATED attribute
191 20 ▶ my $which=$alarm->{properties}{TRIGGER}[0]{param}{RELATED}||'START';
Condition100%2/2
$alarm->{'properties'}{'TRIGGER'}[0]{'param'}{'RELATED'} || 'START'
l!l
119
MC/DC100%1/1
$alarm->{'properties'}{'TRIGGER'}[0]{'param'}{'RELATED'} || 'START'
$alarm->{'properties'}{'TRIGGER'}[0]{'param'}{'RELATED'}
Truth table100%2/2
A
A= $alarm->{'properties'}{'TRIGGER'}[0]{'param'}{'RELATED'}
Aresult
11
00
192  
193 20 $self->map_properties(\%a,$alarm);
194 $a{when}=ref $a{TRIGGER} eq 'DateTime::Duration'
195 ? $e->{"DT$which"}+delete $a{TRIGGER}
196 20 ▶ : delete $a{TRIGGER};
Branch: 1 branch
BranchTrueFalseTotal
ref $a{'TRIGGER'} eq 'DateTime::Duration' ? : 19 1 20
197  
198 20 push @{$e->{VALARM}},\%a;
199 }
200 sub _fix_alarms {
201 84 ▶ my $e=shift;
Subroutine _fix_alarms: called CC=2 SCAR=6.9
202 84 my $orig_start=shift;
203  
204 # trigger already remove, generate diff
205 84 my $diff=$e->{DTSTART}-$orig_start;
206 84 my @alarms=();
207 84 foreach my $old (@{ $e->{VALARM} }) {
208 104 my %a=%$old;
209 104 $a{when}=$a{when}->clone->add_duration($diff);
210 104 push @alarms, \%a;
211 }
212 84 return \@alarms;
213 }
214 sub add_objects {
215 129 ▶ my $self=shift;
Subroutine add_objects: called CC=4 SCAR=13.9
Pod undocumented
216 129 my $event=shift;
217  
218 129 ▶ return unless $event->{objects};
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"objects"} 85 44 129
219 44 foreach my $o (@{ $event->{objects} }) {
220 117 my $t=$o->{type};
221 117 ▶ $self->$t($o,@_) if $self->can($t);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($t) 117 0 117
222 }
223 }
224 sub _hours {
225 65 ▶ my $duration=shift;
Subroutine _hours: called CC=4 SCAR=13.9
226  
227 65 my($days,$hours,$minutes)=@{$duration}{qw(days hours minutes)};
228 65 ▶ $days||=0; $hours||=0; $minutes||=0;
Condition83%5/6
$days ||= 0
l!l
659
$hours ||= 0
l!l
065
$minutes ||= 0
l!l
605
MC/DC100%1/1
$days ||= 0
$days
MC/DC0%0/1
$hours ||= 0
$hours
MC/DC100%1/1
$minutes ||= 0
$minutes
Truth table100%2/2
A
A= $days
Aresult
11
00
Truth table50%1/2
A
A= $hours
Aresult
11
00
Truth table100%2/2
A
A= $minutes
Aresult
11
00
229 65 return sprintf "%.2f",($days*24*60+$hours*60+$minutes)/60.0;
230 }
231 sub convert_value {
232 805 ▶ my($self,$type,$hash)=@_;
Subroutine convert_value: called CC=13 SCAR=26.9
Pod undocumented
233  
234 805 my $value=$hash->{value};
235 805 ▶ return $value unless $value; #should protect from invalid datetimes
Branch: 1 branch
BranchTrueFalseTotal
unless $value 7 798 805
236  
237 798 ▶ if ($type eq 'TRIGGER') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "TRIGGER") 20 778 798
238 #can be date or duration!
239 20 ▶ return $dfmt->parse_duration($value) if $value =~/^[-+]?P/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^[-+]?P/ 19 1 20
240 1 return $dfmt->parse_datetime($value)->set_time_zone($self->{tz});
241 }
242 778 ▶ if ($TYPES{hash}{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iCal::Parser::TYPES{"hash"}{$type}) 25 753 778
243 25 my %h=(value=>$value);
244 25 map { $h{$_}=$hash->{param}{$_} } keys %{ $hash->{param} };
245 25 return \%h;
246 }
247 753 ▶ return $dfmt->parse_duration($value) if $TYPES{durations}{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $iCal::Parser::TYPES{"durations"}{$type} 26 727 753
248 727 ▶ return $value unless $TYPES{dates}{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $iCal::Parser::TYPES{"dates"}{$type} 448 279 727
249  
250 #mozilla calendar bug: negative dates on todos!
251 279 ▶ return undef if $value =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^-/ 2 277 279
252  
253 #handle dates which can be arrays (EXDATE)
254 277 my @dates=();
255 277 foreach my $s (split ',', $value) {
256 # I have a sample calendar "Employer Tax calendar"
257 # which has an allday event ending on 20040332!
258 # so, handle the exception
259 278 my $date;
260 278 eval {
261 278 $date=$dfmt->parse_datetime($s)->set_time_zone($self->{tz});
262 };
263 278 ▶ push @dates, $date and next unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 278 0 278
Condition40%2/5
$@ || push(@dates, $date) && (next)
l!l&&r!l&&!r
02780
push @dates, $date and next
!ll
0278
MC/DC0%0/2
$@ || push(@dates, $date) && (next)
$@ push @dates, $date
Truth table33%1/3
A || B
A= $@B= push @dates, $date
ABresult
1X1
011
000
264 0 ▶ die $@ if $@ && $type ne 'DTEND';
Branch: 1 branch
BranchTrueFalseTotal
if $@ and $type ne "DTEND" 0 0 0
Condition0%0/3
$@ and $type ne "DTEND"
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and $type ne "DTEND"
$@ $type ne "DTEND"
Truth table0%0/3
A and B
A= $@B= $type ne "DTEND"
ABresult
0X0
100
111
265 push @dates,
266 0 $dfmt->parse_datetime(--$value)->set_time_zone($self->{tz});
267 }
268 277 return @dates;
269 }
270 sub get_value {
271 795 ▶ my($self,$props,$key)=@_;
Subroutine get_value: called CC=2 SCAR=7.0
Pod undocumented
272  
273 795 my @a=map {$self->convert_value($key,$_)} @{ $props->{$key} };
274 795 ▶ return wantarray ? @a : $a[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 795 0 795
275 }
276 sub _param {
277 83 ▶ my($event,$key,$param)=@_;
Subroutine _param: called CC=1 SCAR=0.0
278 83 return $event->{properties}{$key}[0]{param}{$param};
279 }
280 #set $a from $b
281 sub map_properties {
282 149 ▶ my($self,$e,$event)=@_;
Subroutine map_properties: called CC=4 SCAR=13.9
Pod undocumented
283  
284 149 my $props=$event->{properties};
285 149 foreach (keys %$props) {
286 795 my @a=$self->get_value($props,$_);
287 795 ▶ delete $e->{$_}, next unless defined $a[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a[0] 2 793 795
288 793 ▶ $e->{$_}=$TYPES{arrays}{$_} ? \@a : $a[0];
Branch: 1 branch
BranchTrueFalseTotal
$iCal::Parser::TYPES{'arrays'}{$_} ? : 15 778 793
289 }
290 ;
291 149 delete $e->{SEQUENCE};
292 }
293 sub _cur_calid {
294 97 ▶ my $self=shift;
Subroutine _cur_calid: called CC=1 SCAR=0.0
295 97 return $self->{ical}{cals}[-1]{'X-WR-RELCALID'};
296 }
297 sub find_day {
298 251 ▶ my($self,$d)=@_;
Subroutine find_day: called CC=3 SCAR=11.0
Pod undocumented
299  
300 251 my $h=$self->{ical}{events};
301 #warn sprintf "find %4d-%02d-%02d\n",$d->year,$d->month,$d->day
302 #if $self->{debug};
303 251 foreach my $i ($d->year,$d->month,$d->day) {
304 753 ▶ $h->{$i}||={};
Condition100%2/2
$h->{$i} ||= {}
l!l
448305
MC/DC100%1/1
$h->{$i} ||= {}
$h->{$i}
Truth table100%2/2
A
A= $h->{$i}
Aresult
11
00
305 753 $h=$h->{$i};
306 }
307 251 return $h;
308 }
309 sub add_event {
310 241 ▶ my($self,$event)=@_;
Subroutine add_event: called CC=1 SCAR=0.0
Pod undocumented
311  
312 241 $self->find_day($event->{DTSTART})->{$event->{UID}}=$event;
313 }
314 sub update_recurrences {
315 32 ▶ my $self=shift;
Subroutine update_recurrences: called CC=3 SCAR=11.0
Pod undocumented
316 32 foreach my $event (@{ $self->{recurrences} }) {
317 10 my $day=$self->find_day(delete $event->{'RECURRENCE-ID'});
318 10 ▶ my $old=delete $day->{$event->{UID}}||{};
Condition100%2/2
delete $$day{$event->{'UID'}} || {}
l!l
82
MC/DC100%1/1
delete $$day{$event->{'UID'}} || {}
delete $$day{$event->{'UID'}}
Truth table100%2/2
A
A= delete $$day{$event->{'UID'}}
Aresult
11
00
319 10 $self->add_event({%$old,%$event});
320 }
321 }
322 sub add_span {
323 7 ▶ my($self,$event)=@_;
Subroutine add_span: called CC=5 SCAR=16.2
Pod undocumented
324 7 my %last=%$event;
325  
326 #when event spans days, only alarm on first entry
327 7 delete $last{VALARM};
328  
329 7 $last{DTSTART}=$event->{DTEND}->clone->truncate(to=>'day');
330 7 $last{DTEND}=$event->{DTEND};
331 7 $event->{DTEND}=$event->{DTSTART}->clone->truncate(to=>'day')
332 ->add(days=>1);
333 7 $last{hours}=_hours($last{DTEND}-$last{DTSTART});
334 7 $event->{hours}=_hours($event->{DTEND}-$event->{DTSTART});
335 7 my @a=();
336 7 my $min=$self->{span}->start;
337 7 my $max=$self->{span}->end;
338 7 ▶ for (my $d=$event->{DTEND}->clone;
Branch: 1 branch
BranchTrueFalseTotal
if ($d < $last{"DTSTART"}) 2 7 9
339 $d < $last{DTSTART}; $d->add(days=>1)) {
340 2 ▶ if ($d >= $min && $d <= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d >= $min and $d <= $max) 2 0 2
Condition33%1/3
$d >= $min and $d <= $max
!ll&&!rl&&r
002
MC/DC0%0/2
$d >= $min and $d <= $max
$d >= $min $d <= $max
Truth table33%1/3
A and B
A= $d >= $minB= $d <= $max
ABresult
0X0
100
111
341 2 my %t=%last;
342 2 $t{DTSTART}=$d->clone;
343 2 $t{DTEND}=$d->clone->add(days=>1);
344 2 $t{hours}=_hours($t{DTEND}-$t{DTSTART});
345 2 push @a,\%t;
346 }
347 }
348 7 my($start,$end)=($self->{span}->start,$self->{span}->end);
349 16 map {$self->add_event($_)} grep {
350 7 ▶ $_->{DTSTART} >= $start && $_->{DTEND} <= $end
Branch: 1 branch
BranchTrueFalseTotal
$_->{"DTSTART"} >= $start and $_->{"DTEND"} <= $end 16 0 16
351 } $event,@a,\%last;
352 }
353 4 1;
354 __END__
355  
356 =head1 NAME
357  
358 iCal::Parser - Parse iCalendar files into a data structure
359  
360 =head1 SYNOPSIS
361  
362 use iCal::Parser
363  
364 my $parser=iCal::Parser->new();
365 my $hash=$parser->parse($file);
366  
367 $parser->parse($another_file);
368 my $combined=$parser->calendar;
369  
370 my $combined=iCal::Parser->new->parse(@files);
371 my $combined=iCal::Parser->new->parse_files(@files);
372 my $combined=iCal::Parser->new->parse_strings(@strings);
373  
374 =head1 DESCRIPTION
375  
376 This module processes iCalendar (vCalendar 2.0) files as specified in RFC 2445
377 into a data structure.
378 It handles recurrences (C<RRULE>s), exclusions (C<EXDATE>s), event updates
379 (events with a C<RECURRENCE-ID>), and nested data structures (C<ATTENDEES> and
380 C<VALARM>s). It currently ignores the C<VTIMEZONE>, C<VJOURNAL> and
381 C<VFREEBUSY> entry types.
382  
383 The data structure returned is a hash like the following:
384  
385 {
386 calendars=>[\%cal, ...],
387 events=>{yyyy=>{mm=>{dd}=>{UID=>\%event}}
388 todos=>[\%todo, ...]
389 }
390  
391 That is, it contains an array of calendar hashes, a hash of events key by
392 C<year=E<gt>month=E<gt>day=E<gt>eventUID>, and an array of todos.
393  
394 Calendars, events and todos are "rolled up" version os the hashes returned from
395 L<Text::vFile::asData>, with dates replaced by C<DateTime> objects.
396  
397 During parsing, events in the input calendar are expanded out into multiple
398 events, one per day covered by the event, as follows:
399  
400 =over 4
401  
402 =item *
403  
404 If the event is a one day "all day" event (in ical, the event is 24hrs long,
405 starts at midnight on the day and ends a midnight of the next day),
406 it contains no C<hour> field and the C<allday> field is set to C<1>.
407  
408 =item *
409  
410 If the event is a recurrence (C<RRULE>), one event per day is created as
411 per the C<RRULE> specification.
412  
413 =item *
414  
415 If the event spans more than one day (the start and end dates are on different
416 days, but does not contain an C<RRULE>),
417 it is expanded into multiple events, the first events end time is set
418 to midnight, subsequent events are set to start at midnight and end at
419 midnight the following day (same as an "allday" event, but the C<allday> field
420 is not set), and the last days event is set to run from midnight to the
421 end time of the original multi-day event.
422  
423 =item *
424  
425 If the event is an update (it contains a C<RECURRENCE-ID>), the original
426 event is updated. If the referenced event does not exist (e.g., it was
427 deleted after the update), then the event is added as a new event.
428  
429 =back
430  
431  
432 An example of each hash is below.
433  
434 =head2 Calendar Hash
435  
436 {
437 'X-WR-CALNAME' => 'Test',
438 'index' => 1,
439 'X-WR-RELCALID' => '7CCE8555-3516-11D9-8A43-000D93C45D90',
440 'PRODID' => '-//Apple Computer\\, Inc//iCal 1.5//EN',
441 'CALSCALE' => 'GREGORIAN',
442 'X-WR-TIMEZONE' => 'America/New_York',
443 'X-WR-CALDESC' => 'My Test Calendar',
444 'VERSION' => '2.0'
445 }
446  
447 =head2 Event Hash
448  
449 Note that C<hours> and C<allday> are mutually exclusive in the actual data.
450 The C<idref> field contains the C<id> of the calendar the event
451 came from, which is useful if the hash was created from multiple calendars.
452  
453 {
454 'SUMMARY' => 'overnight',
455 'hours' => '15.00',
456 'allday' => 1,
457 'UID' => '95CCBF98-3685-11D9-8CA5-000D93C45D90',
458 'idref' => '7CCE8555-3516-11D9-8A43-000D93C45D90',
459 'DTSTAMP' => \%DateTime,
460 'DTEND' => \%DateTime,
461 'DTSTART' => \%DateTime
462 'ATTENDEE' => [
463 {
464 'CN' => 'Jay',
465 'value' => 'mailto:jayl@my.server'
466 },
467 ],
468 'VALARM' => [
469 {
470 'when' => \%DateTime,
471 'SUMMARY' => 'Alarm notification',
472 'ACTION' => 'EMAIL',
473 'DESCRIPTION' => 'This is an event reminder',
474 'ATTENDEE' => [
475 {
476 'value' => 'mailto:cpan@my.server'
477 }
478 ]
479 }
480 ],
481 }
482  
483 =head2 Todo Hash
484  
485 {
486 'URL' => 'mailto:me',
487 'SUMMARY' => 'todo 1',
488 'UID' => 'B78E68F2-35E7-11D9-9E64-000D93C45D90',
489 'idref' => '7CCE8555-3516-11D9-8A43-000D93C45D90',
490 'STATUS' => 'COMPLETED',
491 'COMPLETED' => \%DateTime,
492 'DTSTAMP' => \%DateTime,
493 'PRIORITY' => '9',
494 'DTSTART' => \%DateTime,
495 'DUE' => \%DateTime,
496 'DESCRIPTION' => 'not much',
497 'VALARM' => [
498 {
499 'when' => \%DateTime,
500 'ATTACH' => 'file://localhost/my-file',
501 'ACTION' => 'PROCEDURE'
502 }
503 ],
504 },
505  
506 =head1 Methods
507  
508 =head2 new(%opt_args)
509  
510 =head3 Optional Arguments
511  
512 =over 4
513  
514 =item start {yyymmdd|DateTime}
515  
516 Only include events on or after C<yyymmdd>. Defaults to Jan of this year.
517  
518 =item end {yyyymmdd|DateTime}
519  
520 Only include events before C<yyymmdd>.
521  
522 =item no_events
523  
524 Don't include events in the output (todos only).
525  
526 =item no_todos
527  
528 Don't include todos in the output (events only).
529  
530 =item months n
531  
532 L<DateTime::Set>s (used for calculating recurrences) are limited to
533 approximately 200 entries. If an C<end> date is not specified, the
534 C<to> date is set to the C<start> date plus this many months.
535 The default is 60.
536  
537 =item tz (string|DateTime::TimeZone)
538  
539 Use tz as timezone for date values.
540 The default is 'local', which will adjust the parsed dates to the current timezone.
541  
542 =item debug
543  
544 Set to non-zero for some debugging output during processing.
545  
546 =back
547  
548 =head2 parse({file|file_handle}+)
549  
550 Parse the input files or opened file handles and return the generated hash.
551  
552 This function can be called mutitple times and the calendars will be
553 merge into the hash, each event tagged with the unique id of its calendar.
554  
555 =head2 parse_files({file|file_handle}+)
556  
557 Alias for C<parse()>
558  
559 =head2 parse_strings(string+)
560  
561 Parse the input strings (each assumed to be a valid iCalendar) and return
562 the generated hash.
563  
564 =head1 AUTHOR
565  
566 Rick Frankel, cpan@rickster.com
567  
568 =head1 COPYRIGHT
569  
570 This program is free software; you can redistribute
571 it and/or modify it under the same terms as Perl itself.
572  
573 The full text of the license can be found in the
574 LICENSE file included with this module.
575  
576  
577 =head1 SEE ALSO
578  
579 L<Text::vFile::asData>, L<DateTime::Set>, L<DateTime::Span>,
580 L<iCal::Parser::SAX>
↑ Summary