8 use base 'MARC::Field';
13 my $class = ref($proto) || $proto;
15 my $last_enum = undef;
17 $self->{_mfhdc_ENUMS} = {};
18 $self->{_mfhdc_CHRONS} = {};
19 $self->{_mfhdc_PATTERN} = {};
20 $self->{_mfhdc_COPY} = undef;
21 $self->{_mfhdc_UNIT} = undef;
22 $self->{_mfhdc_COMPRESSIBLE} = 1; # until proven otherwise
24 foreach my $subfield ($self->subfields) {
25 my ($key, $val) = @$subfield;
28 } elsif ($key =~ /[a-h]/) {
29 # Enumeration Captions
30 $self->{_mfhdc_ENUMS}->{$key} = {CAPTION => $val,
38 } elsif ($key =~ /[i-m]/) {
40 $self->{_mfhdc_CHRONS}->{$key} = $val;
41 } elsif ($key eq 'u') {
42 # Bib units per next higher enumeration level
43 carp('$u specified for top-level enumeration')
44 unless defined($last_enum);
45 $self->{_mfhdc_ENUMS}->{$last_enum}->{COUNT} = $val;
46 } elsif ($key eq 'v') {
47 carp '$v specified for top-level enumeration'
48 unless defined($last_enum);
49 $self->{_mfhdc_ENUMS}->{$last_enum}->{RESTART} = ($val eq 'r');
50 } elsif ($key =~ /[npwxz]/) {
51 # Publication Pattern ('o' == type of unit, 'q'..'t' undefined)
52 $self->{_mfhdc_PATTERN}->{$key} = $val;
53 } elsif ($key eq 'y') {
54 # Publication pattern: 'y' is repeatable
55 $self->{_mfhdc_PATTERN}->{y} = [] if (!defined $self->{_mfhdc_PATTERN}->{y});
56 push @{$self->{_mfhdc_PATTERN}->{y}}, $val;
57 } elsif ($key eq 'o') {
59 $self->{_mfhdc_UNIT} = $val;
60 } elsif ($key eq 't') {
61 $self->{_mfhdc_COPY} = $val;
63 carp "Unknown caption subfield '$key'";
67 # subsequent levels of enumeration (primary and alternate)
68 # If an enumeration level doesn't document the number
69 # of "issues" per "volume", or whether numbering of issues
70 # restarts, then we can't compress.
71 foreach my $key ('b', 'c', 'd', 'e', 'f', 'h') {
72 if (exists $self->{_mfhdc_ENUMS}->{$key}) {
73 my $pattern = $self->{_mfhdc_ENUMS}->{$key};
74 if (!$pattern->{RESTART} || !$pattern->{COUNT}
75 || ($pattern->{COUNT} eq 'var')
76 || ($pattern->{COUNT} eq 'und')) {
77 $self->{_mfhdc_COMPRESSIBLE} = 0;
83 # If there's a $x subfield and a $j, then it's compressible
84 if (exists $self->{_mfhdc_PATTERN}->{x} && exists $self->{_mfhdc_CHRONS}->{'j'}) {
85 $self->{_mfhdc_COMPRESSIBLE} = 1;
88 bless ($self, $class);
90 if (exists $self->{_mfhdc_PATTERN}->{y}) {
91 $self->decode_pattern;
99 my $pattern = $self->{_mfhdc_PATTERN}->{y};
107 return $self->{_mfhdc_COMPRESSIBLE};
114 if (exists $self->{_mfhdc_CHRONS}->{$key}) {
115 return $self->{_mfhdc_CHRONS}->{$key};
125 if (exists $self->{_mfhdc_ENUMS}->{$key}) {
126 return $self->{_mfhdc_ENUMS}->{$key};
127 } elsif (exists $self->{_mfhdc_CHRONS}->{$key}) {
128 return $self->{_mfhdc_CHRONS}->{$key};
137 my $val = $self->capfield($key);
140 return $val->{CAPTION};
146 # If items are identified by chronology only, with no separate
147 # enumeration (eg, a newspaper issue), then the chronology is
148 # recorded in the enumeration subfields $a - $f. We can tell
149 # that this is the case if there are $a - $f subfields and no
150 # chronology subfields ($i-$k), and none of the $a-$f subfields
151 # have associated $u or $v subfields, but there are $w and $y
154 sub enumeration_is_chronology {
157 # There is always a '$a' subfield in well-formed fields.
158 return 0 if exists $self->{_mfhdc_CHRONS}->{i};
160 foreach my $key ('a' .. 'f') {
163 last if !exists $self->{_mfhdc_ENUMS}->{$key};
165 $enum = $self->{_mfhdc_ENUMS}->{$key};
166 return 0 if defined $enum->{COUNT} || defined $enum->{RESTART};
169 return (exists $self->{_mfhdc_PATTERN}->{w} && exists $self->{_mfhdc_PATTERN}->{y});
182 my $daypat = '(mo|tu|we|th|fr|sa|su)';
183 my $weekpat = '(99|98|97|00|01|02|03|04|05)';
185 my $monthpat = '(01|02|03|04|05|06|07|08|09|10|11|12)';
186 my $seasonpat = '(21|22|23|24)';
188 # Initialize $weeknopat to be '(01|02|03|...|51|52|53)'
190 foreach my $weekno (1..52) {
191 $weeknopat .= sprintf('%02d|', $weekno);
198 # Translate daynames into day of week for DateTime
199 # also used to check if dayname is valid.
201 if (exists $daynames{$pat}) {
203 # figure out day of week for date and compare
204 my $dt = DateTime->new(year => $date[0],
207 return ($dt->day_of_week == $daynames{$pat});
208 } elsif (length($pat) == 2) {
210 return $pat == $date[3];
211 } elsif (length($pat) == 4) {
214 $mon = substr($pat, 0, 2);
215 $day = substr($pat, 2, 2);
217 return (($mon == $date[1]) && ($day == $date[2]));
219 carp "Invalid day pattern '$pat'";
224 # Calcuate date of "n"th last "dayname" of month: second last Tuesday
225 sub last_week_of_month {
229 my $end_dt = DateTime->last_day_of_month(year => $dt->year,
230 month => $dt->month);
232 $day = $daynames{$day};
233 while ($end_dt->day_of_week != $day) {
234 $end_dt->subtract(days => 1);
237 # 99: last week of month, 98: second last, etc.
238 for (my $i = 99 - $week; $i > 0; $i--) {
239 $end_dt->subtract(weeks => 1);
253 return (($dt->month == $month)
254 && (($dt->week_of_month == $weekno)
255 || ($dt->week_of_month == last_day_of_month($dt, $weekno, 'th')->week_of_month)));
259 if ($daynames{$day} != $dt->day_of_week) {
260 # if it's the wrong day of the week, rest doesn't matter
264 if (!defined $month) {
266 return (($dt->weekday_of_month == $weekno)
267 || ($dt->weekday_of_month == last_day_of_month($dt, $weekno, $day)->weekday_of_month));
271 if ($month != $dt->month) {
272 # If it's the wrong month, then we're done
276 # It's the right day of the week
277 # It's the right month
279 if ($weekno == $dt->weekday_of_month) {
280 # If this matches, then we're counting from the beginning
281 # of the month and it matches and we're done.
285 # only case left is that the week number is counting from
286 # the end of the month: eg, second last wednesday
287 return (last_week_of_month($weekno, $day)->weekday_of_month == $dt->weekday_of_month);
293 my $dt = DateTime->new(year => $date[0],
297 if ($pat =~ m/^$weekpat$daypat$/) {
298 # WWdd: 03we = Third Wednesday
299 return check_date($dt, undef, $1, $2);
300 } elsif ($pat =~ m/^$monthpat$weekpat$daypat$/) {
301 # MMWWdd: 0599tu Last Tuesday in May XXX WRITE ME
302 return check_date($dt, $1, $2, $3);
303 } elsif ($pat =~ m/^$monthpat$weekpat$/) {
304 # MMWW: 1204: Fourth week in December XXX WRITE ME
305 return check_date($dt, $1, $2, undef);
307 carp "invalid week pattern '$pat'";
316 return ($pat eq $date[1]);
323 return ($pat eq $date[1]);
336 'm' => \&match_month,
337 's' => \&match_season,
340 sub regularity_match {
345 foreach my $regularity ($self->{_mfhdc_PATTERN}->{y}) {
346 next unless $regularity =~ m/^$pubcode/;
348 my $chroncode= substr($regularity, 1, 1);
349 my @pats = split(/,/, substr($regularity, 2));
352 foreach my $pat (@pats) {
353 if ($dispatch{$chroncode}->($pat, @date)) {
366 return $self->regularity_match('o', @date);
373 return $self->regularity_match('p', @date);
380 return $self->regularity_match('c', @date);