10 use base 'MARC::Field';
15 my $class = ref($proto) || $proto;
17 my $last_enum = undef;
19 $self->{_mfhdc_ENUMS} = {};
20 $self->{_mfhdc_CHRONS} = {};
21 $self->{_mfhdc_PATTERN} = {};
22 $self->{_mfhdc_COPY} = undef;
23 $self->{_mfhdc_UNIT} = undef;
24 $self->{_mfhdc_COMPRESSIBLE} = 1; # until proven otherwise
26 foreach my $subfield ($self->subfields) {
27 my ($key, $val) = @$subfield;
30 } elsif ($key =~ /[a-h]/) {
31 # Enumeration Captions
32 $self->{_mfhdc_ENUMS}->{$key} = {CAPTION => $val,
40 } elsif ($key =~ /[i-m]/) {
42 $self->{_mfhdc_CHRONS}->{$key} = $val;
43 } elsif ($key eq 'u') {
44 # Bib units per next higher enumeration level
45 carp('$u specified for top-level enumeration')
46 unless defined($last_enum);
47 $self->{_mfhdc_ENUMS}->{$last_enum}->{COUNT} = $val;
48 } elsif ($key eq 'v') {
49 carp '$v specified for top-level enumeration'
50 unless defined($last_enum);
51 $self->{_mfhdc_ENUMS}->{$last_enum}->{RESTART} = ($val eq 'r');
52 } elsif ($key =~ /[npwz]/) {
53 # Publication Pattern info ('o' == type of unit, 'q'..'t' undefined)
54 $self->{_mfhdc_PATTERN}->{$key} = $val;
55 } elsif ($key =~ /x/) {
56 # Calendar change can have multiple comma-separated values
57 $self->{_mfhdc_PATTERN}->{x} = [split /,/, $val];
58 } elsif ($key eq 'y') {
59 $self->{_mfhdc_PATTERN}->{y} = []
60 unless exists $self->{_mfhdc_PATTERN}->{y};
61 push @{$self->{_mfhdc_PATTERN}->{y}}, $val;
62 } elsif ($key eq 'o') {
64 $self->{_mfhdc_UNIT} = $val;
65 } elsif ($key eq 't') {
66 $self->{_mfhdc_COPY} = $val;
68 carp "Unknown caption subfield '$key'";
72 # subsequent levels of enumeration (primary and alternate)
73 # If an enumeration level doesn't document the number
74 # of "issues" per "volume", or whether numbering of issues
75 # restarts, then we can't compress.
76 foreach my $key ('b', 'c', 'd', 'e', 'f', 'h') {
77 if (exists $self->{_mfhdc_ENUMS}->{$key}) {
78 my $pattern = $self->{_mfhdc_ENUMS}->{$key};
79 if (!$pattern->{RESTART} || !$pattern->{COUNT}
80 || ($pattern->{COUNT} eq 'var')
81 || ($pattern->{COUNT} eq 'und')) {
82 $self->{_mfhdc_COMPRESSIBLE} = 0;
88 my $pat = $self->{_mfhdc_PATTERN};
90 # Sanity check publication frequency vs publication pattern:
91 # if the frequency is a number, then the pattern better
92 # have that number of values associated with it.
93 if (exists($pat->{w}) && ($pat->{w} =~ /^\d+$/)
94 && ($pat->{w} != scalar(@{$pat->{y}}))) {
95 carp("Caption::new: publication frequency '$pat->{w}' != publication pattern @{$pat->{y}}");
99 # If there's a $x subfield and a $j, then it's compressible
100 if (exists $pat->{x} && exists $self->{_mfhdc_CHRONS}->{'j'}) {
101 $self->{_mfhdc_COMPRESSIBLE} = 1;
104 bless ($self, $class);
106 if (exists $pat->{y}) {
107 $self->decode_pattern;
115 my $pattern = $self->{_mfhdc_PATTERN}->{y};
123 return $self->{_mfhdc_COMPRESSIBLE};
130 if (exists $self->{_mfhdc_CHRONS}->{$key}) {
131 return $self->{_mfhdc_CHRONS}->{$key};
141 if (exists $self->{_mfhdc_ENUMS}->{$key}) {
142 return $self->{_mfhdc_ENUMS}->{$key};
143 } elsif (exists $self->{_mfhdc_CHRONS}->{$key}) {
144 return $self->{_mfhdc_CHRONS}->{$key};
153 my $val = $self->capfield($key);
156 return $val->{CAPTION};
162 sub calendar_change {
165 return $self->{_mfhdc_PATTERN}->{x};
168 # If items are identified by chronology only, with no separate
169 # enumeration (eg, a newspaper issue), then the chronology is
170 # recorded in the enumeration subfields $a - $f. We can tell
171 # that this is the case if there are $a - $f subfields and no
172 # chronology subfields ($i-$k), and none of the $a-$f subfields
173 # have associated $u or $v subfields, but there's a $w and no $x
175 sub enumeration_is_chronology {
178 # There is always a '$a' subfield in well-formed fields.
179 return 0 if exists $self->{_mfhdc_CHRONS}->{i}
180 || exists $self->{_mfhdc_PATTERN}->{x};
182 foreach my $key ('a' .. 'f') {
185 last if !exists $self->{_mfhdc_ENUMS}->{$key};
187 $enum = $self->{_mfhdc_ENUMS}->{$key};
188 return 0 if defined $enum->{COUNT} || defined $enum->{RESTART};
191 return (exists $self->{_mfhdc_PATTERN}->{w});
204 my $daypat = '(mo|tu|we|th|fr|sa|su)';
205 my $weekpat = '(99|98|97|00|01|02|03|04|05)';
207 my $monthpat = '(01|02|03|04|05|06|07|08|09|10|11|12)';
208 my $seasonpat = '(21|22|23|24)';
210 # Initialize $weeknopat to be '(01|02|03|...|51|52|53)'
212 foreach my $weekno (1..52) {
213 $weeknopat .= sprintf('%02d|', $weekno);
220 # Translate daynames into day of week for DateTime
221 # also used to check if dayname is valid.
223 if (exists $daynames{$pat}) {
225 # figure out day of week for date and compare
226 my $dt = DateTime->new(year => $date[0],
229 return ($dt->day_of_week == $daynames{$pat});
230 } elsif (length($pat) == 2) {
232 return $pat == $date[3];
233 } elsif (length($pat) == 4) {
236 $mon = substr($pat, 0, 2);
237 $day = substr($pat, 2, 2);
239 return (($mon == $date[1]) && ($day == $date[2]));
241 carp "Invalid day pattern '$pat'";
246 # Calcuate date of "n"th last "dayname" of month: second last Tuesday
247 sub last_week_of_month {
251 my $end_dt = DateTime->last_day_of_month(year => $dt->year,
252 month => $dt->month);
254 $day = $daynames{$day};
255 while ($end_dt->day_of_week != $day) {
256 $end_dt->subtract(days => 1);
259 # 99: last week of month, 98: second last, etc.
260 for (my $i = 99 - $week; $i > 0; $i--) {
261 $end_dt->subtract(weeks => 1);
275 return (($dt->month == $month)
276 && (($dt->week_of_month == $weekno)
277 || ($dt->week_of_month == last_day_of_month($dt, $weekno, 'th')->week_of_month)));
281 if ($daynames{$day} != $dt->day_of_week) {
282 # if it's the wrong day of the week, rest doesn't matter
286 if (!defined $month) {
288 return (($dt->weekday_of_month == $weekno)
289 || ($dt->weekday_of_month == last_day_of_month($dt, $weekno, $day)->weekday_of_month));
293 if ($month != $dt->month) {
294 # If it's the wrong month, then we're done
298 # It's the right day of the week
299 # It's the right month
301 if ($weekno == $dt->weekday_of_month) {
302 # If this matches, then we're counting from the beginning
303 # of the month and it matches and we're done.
307 # only case left is that the week number is counting from
308 # the end of the month: eg, second last wednesday
309 return (last_week_of_month($weekno, $day)->weekday_of_month == $dt->weekday_of_month);
315 my $dt = DateTime->new(year => $date[0],
319 if ($pat =~ m/^$weekpat$daypat$/) {
320 # WWdd: 03we = Third Wednesday
321 return check_date($dt, undef, $1, $2);
322 } elsif ($pat =~ m/^$monthpat$weekpat$daypat$/) {
323 # MMWWdd: 0599tu Last Tuesday in May XXX WRITE ME
324 return check_date($dt, $1, $2, $3);
325 } elsif ($pat =~ m/^$monthpat$weekpat$/) {
326 # MMWW: 1204: Fourth week in December XXX WRITE ME
327 return check_date($dt, $1, $2, undef);
329 carp "invalid week pattern '$pat'";
338 return ($pat eq $date[1]);
345 return ($pat eq $date[1]);
360 # We handle enumeration patterns separately. This just
361 # ensures that when we're processing chronological patterns
362 # we don't match an enumeration pattern.
368 'e' => \&match_issue, # not really a "chron" code
370 'm' => \&match_month,
371 's' => \&match_season,
375 sub regularity_match {
380 # we can't match something that doesn't exist.
381 return 0 if !exists $self->{_mfhdc_PATTERN}->{y};
383 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}}) {
384 next unless $regularity =~ m/^$pubcode/;
386 my $chroncode= substr($regularity, 1, 1);
387 my @pats = split(/,/, substr($regularity, 2));
389 if (!exists $dispatch{$chroncode}) {
390 carp "Unrecognized chroncode '$chroncode'";
395 foreach my $pat (@pats) {
396 $pat =~ s|/.+||; # If it's a combined date, match the start
397 if ($dispatch{$chroncode}->($pat, @date)) {
410 return $self->regularity_match('o', @date);
417 return $self->regularity_match('p', @date);
424 return $self->regularity_match('c', @date);
427 sub enum_is_combined {
429 my $subfield = shift;
431 my $level = ord($subfield) - ord('a') + 1;
433 return 0 if !exists $self->{_mfhdc_PATTERN}->{y};
435 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}}) {
436 next unless $regularity =~ m/^ce$level/o;
438 my @pats = split(/,/, substr($regularity, 3));
440 foreach my $pat (@pats) {
441 $pat =~ s|/.+||; # if it's a combined issue, match the start
442 return 1 if ($iss eq $pat);
451 a => {years => 1}, # annual
452 b => {months => 2}, # bimonthly
453 c => {days => 3}, # semiweekly
454 d => {days => 1}, # daily
455 e => {weeks => 2}, # biweekly
456 f => {months => 6}, # semiannual
457 g => {years => 2}, # biennial
458 h => {years => 3}, # triennial
459 i => {days => 2}, # three times / week
460 j => {days => 10}, # three times /month
462 m => {months => 1}, # monthly
463 q => {months => 3}, # quarterly
464 s => {days => 15}, # semimonthly
465 t => {months => 4}, # three times / year
466 w => {weeks => 1}, # weekly
467 # x => completely irregular
474 if (scalar(@new) == 1) {
475 # only a year is specified. Next date is easy
476 $new[0] += $incr->{years} || 1;
477 } elsif (scalar(@new) == 2) {
478 # Year and month or season
481 $new[1] += ($incr->{months}/3) || 1;
485 $new[1] -= 4; # 25 - 4 == 21 == Spring after Winter
489 $new[1] += $incr->{months} || 1;
495 $new[1] = '0' . $new[1] if ($new[1] < 10);
497 } elsif (scalar(@new) == 3) {
498 # Year, Month, Day: now it gets complicated.
500 if ($new[2] =~ /^[0-9]+$/) {
501 # A single number for the day of month, relatively simple
502 my $dt = DateTime->new(year => $new[0],
507 $new[1] = $dt->month;
510 $new[1] = '0' . $new[1] if ($new[1] < 10);
511 $new[2] = '0' . $new[2] if ($new[2] < 10);
513 warn("Don't know how to cope with @new");
519 # Test to see if $m1/$d1 is on or after $m2/$d2
520 # if $d2 is undefined, test is based on just months
522 my ($m1, $d1, $m2, $d2) = @_;
525 || ($m1 == $m2 && ((!defined $d2) || ($d1 >= $d2))));
528 sub calendar_increment {
532 my $cal_change = $self->calendar_change;
538 # A calendar change is defined, need to check if it applies
539 if ((scalar(@new) == 2 && $new[1] > 20) || (scalar(@new) == 1)) {
540 carp "Can't calculate date change for ", $self->as_string;
544 foreach my $change (@{$cal_change}) {
547 if (length($change) == 2) {
549 } elsif (length($change) == 4) {
550 ($month, $day) = unpack("a2a2", $change);
553 if ($cur->[0] == $new[0]) {
554 # Same year, so a 'simple' month/day comparison will be fine
555 $incr = (!on_or_after($cur->[1], $cur->[2], $month, $day)
556 && on_or_after($new[1], $new[2], $month, $day));
558 # @cur is in the year before @new. There are
559 # two possible cases for the calendar change date that
560 # indicate that it's time to change the volume:
561 # (1) the change date is AFTER @cur in the year, or
562 # (2) the change date is BEFORE @new in the year.
564 # -------|------|------X------|------|
565 # @cur (1) Jan 1 (2) @new
567 $incr = (on_or_after($new[1], $new[2], $month, $day)
568 || !on_or_after($cur->[1], $cur->[2], $month, $day));
570 return $incr if $incr;
583 my $reg = $self->{_mfhdc_REGULARITY};
584 my $pattern = $self->{_mfhdc_PATTERN};
585 my $freq = $pattern->{w};
587 foreach my $i (0..$#keys) {
588 $new[$i] = $cur[$i] = $next->{$keys[$i]} if exists $next->{$keys[$i]};
591 # If the current issue has a combined date (eg, May/June)
592 # get rid of the first date and base the calculation
593 # on the final date in the combined issue.
594 $new[-1] =~ s|^[^/]+/||;
596 # If $frequency is not one of the standard codes defined in %increments
597 # then there has to be a $yp publication regularity pattern that
598 # lists the dates of publication. Use that that list to find the next
599 # date following the current one.
600 # XXX: the code doesn't handle this case yet.
601 if (!defined($freq)) {
602 carp "Undefined frequency in next_date!";
603 } elsif (!exists $increments{$freq}) {
604 carp "Don't know how to deal with frequency '$freq'!";
607 # One of the standard defined issue frequencies
609 @new = incr_date($increments{$freq}, @new);
611 while ($self->is_omitted(@new)) {
612 @new = incr_date($increments{$freq}, @new);
615 if ($self->is_combined(@new)) {
616 my @second_date = incr_date($increments{$freq}, @new);
618 # I am cheating: This code assumes that only the smallest
619 # time increment is combined. So, no "Apr 15/May 1" allowed.
620 $new[-1] = $new[-1] . '/' . $second_date[-1];
624 for my $i (0..$#new) {
625 $next->{$keys[$i]} = $new[$i];
628 # Figure out if we need to adust volume number
629 # right now just use the $carry that was passed in.
630 # in long run, need to base this on ($carry or date_change)
632 # if $carry is set, the date doesn't matter: we're not
633 # going to increment the v. number twice at year-change.
634 $next->{a} += $carry;
635 } elsif (defined $self->{_mfhdc_PATTERN}->{x}) {
636 $next->{a} += $self->calendar_increment(\@cur, @new);
644 # First handle any "alternative enumeration", since they're
645 # a lot simpler, and don't depend on the the calendar
646 foreach my $key ('h', 'g') {
647 next if !exists $next->{$key};
648 if (!$self->capstr($key)) {
649 warn "Holding data exists for $key, but no caption specified";
654 my $cap = $self->capfield($key);
655 if ($cap->{RESTART} && $cap->{COUNT}
656 && ($next->{$key} == $cap->{COUNT})) {
670 # $carry keeps track of whether we need to carry into the next
671 # higher level of enumeration. It's not actually necessary except
672 # for when the loop ends: if we need to carry from $b into $a
673 # then $carry will be set when the loop ends.
675 # We need to keep track of this because there are two different
676 # reasons why we might increment the highest level of enumeration ($a)
677 # 1) we hit the correct number of items in $b (ie, 5th iss of quarterly)
678 # 2) it's the right time of the year.
681 foreach my $key (reverse('b'..'f')) {
682 next if !exists $next->{$key};
684 if (!$self->capstr($key)) {
685 # Just assume that it increments continuously and give up
686 warn "Holding data exists for $key, but no caption specified";
692 # If the current issue has a combined issue number (eg, 2/3)
693 # get rid of the first issue number and base the calculation
694 # on the final issue number in the combined issue.
695 if ($next->{$key} =~ m|/|) {
696 $next->{$key} =~ s|^[^/]+/||;
699 my $cap = $self->capfield($key);
700 if ($cap->{RESTART} && $cap->{COUNT}
701 && ($next->{$key} eq $cap->{COUNT})) {
705 # If I don't need to "carry" beyond here, then I just increment
706 # this level of the enumeration and stop looping, since the
707 # "next" hash has been initialized with the current values
713 # You can't have a combined issue that spans two volumes: no.12/1
715 if ($self->enum_is_combined($key, $next->{$key})) {
716 $next->{$key} .= '/' . ($next->{$key} + 1);
722 # The easy part is done. There are two things left to do:
723 # 1) Calculate the date of the next issue, if necessary
724 # 2) Increment the highest level of enumeration (either by date
725 # or because $carry is set because of the above loop
727 if (!$self->subfield('i')) {
728 # The simple case: if there is no chronology specified
729 # then just check $carry and return
730 $next->{'a'} += $carry;
732 # Figure out date of next issue, then decide if we need
733 # to adjust top level enumeration based on that
734 $self->next_date($next, $carry, ('i'..'m'));
743 # Initialize $next with current enumeration & chronology, then
744 # we can just operate on $next, based on the contents of the caption
746 if ($self->enumeration_is_chronology) {
747 foreach my $key ('a' .. 'h') {
748 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}
749 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
751 $self->next_date($next, 0, ('a' .. 'h'));
756 foreach my $key ('a' .. 'h') {
757 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}->{HOLDINGS}
758 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
761 foreach my $key ('i'..'m') {
762 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}
763 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
766 if (exists $next->{'h'}) {
767 $self->next_alt_enum($next);
770 $self->next_enum($next);