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 update_pattern($self, $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}->{p}}))) {
95 carp("Caption::new: publication frequency '$pat->{w}' != publication pattern @{$pat->{y}->{p}}");
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);
112 my $pathash = $self->{_mfhdc_PATTERN}->{y};
113 my ($pubcode, $pat) = unpack("a1a*", $val);
115 $pathash->{$pubcode} = [] unless exists $pathash->{$pubcode};
116 push @{$pathash->{$pubcode}}, $pat;
121 my $pattern = $self->{_mfhdc_PATTERN}->{y};
129 return $self->{_mfhdc_COMPRESSIBLE};
136 if (exists $self->{_mfhdc_CHRONS}->{$key}) {
137 return $self->{_mfhdc_CHRONS}->{$key};
147 if (exists $self->{_mfhdc_ENUMS}->{$key}) {
148 return $self->{_mfhdc_ENUMS}->{$key};
149 } elsif (exists $self->{_mfhdc_CHRONS}->{$key}) {
150 return $self->{_mfhdc_CHRONS}->{$key};
159 my $val = $self->capfield($key);
162 return $val->{CAPTION};
168 sub calendar_change {
171 return $self->{_mfhdc_PATTERN}->{x};
174 # If items are identified by chronology only, with no separate
175 # enumeration (eg, a newspaper issue), then the chronology is
176 # recorded in the enumeration subfields $a - $f. We can tell
177 # that this is the case if there are $a - $f subfields and no
178 # chronology subfields ($i-$k), and none of the $a-$f subfields
179 # have associated $u or $v subfields, but there's a $w and no $x
181 sub enumeration_is_chronology {
184 # There is always a '$a' subfield in well-formed fields.
185 return 0 if exists $self->{_mfhdc_CHRONS}->{i}
186 || exists $self->{_mfhdc_PATTERN}->{x};
188 foreach my $key ('a' .. 'f') {
191 last if !exists $self->{_mfhdc_ENUMS}->{$key};
193 $enum = $self->{_mfhdc_ENUMS}->{$key};
194 return 0 if defined $enum->{COUNT} || defined $enum->{RESTART};
197 return (exists $self->{_mfhdc_PATTERN}->{w});
210 my $daypat = '(mo|tu|we|th|fr|sa|su)';
211 my $weekpat = '(99|98|97|00|01|02|03|04|05)';
213 my $monthpat = '(01|02|03|04|05|06|07|08|09|10|11|12)';
214 my $seasonpat = '(21|22|23|24)';
216 # Initialize $weeknopat to be '(01|02|03|...|51|52|53)'
218 foreach my $weekno (1..52) {
219 $weeknopat .= sprintf('%02d|', $weekno);
226 # Translate daynames into day of week for DateTime
227 # also used to check if dayname is valid.
229 if (exists $daynames{$pat}) {
231 # figure out day of week for date and compare
232 my $dt = DateTime->new(year => $date[0],
235 return ($dt->day_of_week == $daynames{$pat});
236 } elsif (length($pat) == 2) {
238 return $pat == $date[2];
239 } elsif (length($pat) == 4) {
241 my ($mon, $day) = unpack("a2a2", $pat);
243 return (($mon == $date[1]) && ($day == $date[2]));
245 carp "Invalid day pattern '$pat'";
253 my $dt = DateTime->new(year => $cur->[0],
257 if (exists $daynames{$pat}) {
258 # dd: published on the given weekday
259 my $dow = $dt->day_of_week;
260 my $corr = ($dow - $daynames{$pat} + 7) % 7;
262 if ($dow == $daynames{$pat}) {
263 # the next one is one week hence
266 # the next one is later this week,
267 # or it is next week (ie, on or after next Monday)
268 # $corr will take care of it.
269 $dt->add(days => $corr);
271 } elsif (length($pat) == 2) {
272 # DD: published on the give day of every month
273 if ($dt->day >= $pat) {
274 # current date is on or after $pat: next one is next month
275 $dt->set(day => $pat);
276 $dt->add(months => 1);
277 $cur->[0] = $dt->year;
278 $cur->[1] = $dt->month;
279 $cur->[2] = $dt->day;
281 # current date is before $pat: set month to pattern
282 # or we've adjusted the year to next year, now fix the month
284 } elsif (length($pat) == 4) {
285 # MMDD: published on the given day of the given month
286 my ($mon, $day) = unpack("a2a2", $pat);
288 if (on_or_after($mon, $day, $cur->[1], $cur->[2])) {
289 # Current date is on or after pattern; next one is next year
292 # Year is now right. Either it's next year (because of on_or_before)
293 # or it's this year, because the current date is NOT on or after
294 # the pattern. Just fix the month and day
298 carp "Invalid day pattern '$pat'";
306 # Calculate date of 3rd Friday of the month (for example)
307 # 1-5: count from beginning of month
308 # 99-97: count back from end of month
309 sub nth_week_of_month {
313 my ($nth_day, $dow, $day);
315 $day = $daynames{$day};
317 if (0 < $week && $week <= 5) {
318 $nth_day = DateTime->clone($dt)->set(day => 1);
319 } elsif ($week >= 97) {
320 $nth_day = DateTime->last_day_of_month(year => $dt->year,
321 month => $dt->month);
326 $dow = $nth_day->day_of_week();
330 $nth_day->add(days => ($day - $dow + 7) % 7,
334 $nth_day->subtract(days => ($day - $nth_day->day_of_week + 7) % 7);
336 # 99: last week of month, 98: second last, etc.
337 for (my $i = 99 - $week; $i > 0; $i--) {
338 $nth_day->subtract(weeks => 1);
342 # There is no nth "day" in the month!
343 return undef if ($dt->month != $nth_day->month);
349 # Internal utility function to match the various different patterns
350 # of month, week, and day
360 return (($dt->month == $month)
361 && (($dt->week_of_month == $weekno)
363 && ($dt->week_of_month == nth_week_of_month($dt, $weekno, $day)->week_of_month))));
367 if ($daynames{$day} != $dt->day_of_week) {
368 # if it's the wrong day of the week, rest doesn't matter
372 if (!defined $month) {
374 return (($weekno == 0) # Every week
375 || ($dt->weekday_of_month == $weekno) # this week
376 || (($weekno >= 97) && ($dt->weekday_of_month == nth_week_of_month($dt, $weekno, $day)->weekday_of_month)));
380 if ($month != $dt->month) {
381 # If it's the wrong month, then we're done
385 # It's the right day of the week
386 # It's the right month
388 if (($weekno == 0) ||($weekno == $dt->weekday_of_month)) {
389 # If this matches, then we're counting from the beginning
390 # of the month and it matches and we're done.
394 # only case left is that the week number is counting from
395 # the end of the month: eg, second last wednesday
396 return (($weekno >= 97)
397 && (nth_week_of_month($dt, $weekno, $day)->weekday_of_month == $dt->weekday_of_month));
403 my $dt = DateTime->new(year => $date[0],
407 if ($pat =~ m/^$weekpat$daypat$/) {
408 # WWdd: 03we = Third Wednesday
409 return check_date($dt, undef, $1, $2);
410 } elsif ($pat =~ m/^$monthpat$weekpat$daypat$/) {
411 # MMWWdd: 0599tu Last Tuesday in May XXX WRITE ME
412 return check_date($dt, $1, $2, $3);
413 } elsif ($pat =~ m/^$monthpat$weekpat$/) {
414 # MMWW: 1204: Fourth week in December XXX WRITE ME
415 return check_date($dt, $1, $2, undef);
417 carp "invalid week pattern '$pat'";
423 # Use $pat to calcuate the date of the issue following $cur
425 sub subsequent_week {
429 my $dt = DateTime->new(year => $cur->[0],
433 if ($pat =~ m/^$weekpat$daypat$/) {
434 # WWdd: published on given weekday of given week of every month
435 my ($week, $day) = ($1, $2);
439 $candidate = DateTime->clone($dt);
440 if ($dt->day_of_week == $daynames{$day}) {
441 # Current is right day, next one is a week hence
442 $candidate->add(days => 7);
444 $candidate->add(days => ($dt->day_of_week - $daynames{$day} + 7) % 7);
447 # 3rd Friday of the month (eg)
448 $candidate = nth_week_of_month($dt, $week, $day);
451 if ($candidate < $dt) {
452 # If the n'th week of the month happens before the
453 # current issue, then the next issue is published next
454 # month, otherwise, it's published this month.
455 # This will never happen for the "00: every week" pattern
456 $candidate = DateTime->clone($dt)->add(months => 1)->set(day => 1);
457 $candidate = nth_week_of_month($dt, $week, $day);
459 } elsif ($pat =~ m/^$monthpat$weekpat$daypat$/) {
460 # MMWWdd: published on given weekday of given week of given month
461 my ($month, $week, $day) = ($1, $2, $3);
463 $candidate = DateTime->new(year => $dt->year,
466 $candidate = nth_week_of_month($candidate, $week, $day);
467 if ($candidate < $dt) {
468 # We've missed it for this year, next one that matches
470 $candidate->add(years => 1)->set(day => 1);
471 $candidate = nth_week_of_month($candidate, $week, $day);
473 } elsif ($pat =~ m/^$monthpat$weekpat$/) {
474 # MMWW: published during given week of given month
475 my ($month, $week) = ($1, $2);
477 $candidate = nth_week_of_month(DateTime->new(year => $dt->year,
482 if ($candidate < $dt) {
483 # Already past the pattern date this year, move to next year
484 $candidate->add(years => 1)->set(day => 1);
485 $candidate = nth_week_of_month($candidate, $week, 'th');
488 carp "invalid week pattern '$pat'";
492 $cur->[0] = $candidate->year;
493 $cur->[1] = $candidate->month;
494 $cur->[2] = $candidate->day;
503 return ($pat eq $date[1]);
510 return ($pat eq $date[1]);
525 # We handle enumeration patterns separately. This just
526 # ensures that when we're processing chronological patterns
527 # we don't match an enumeration pattern.
533 'e' => \&match_issue, # not really a "chron" code
535 'm' => \&match_month,
536 's' => \&match_season,
540 sub regularity_match {
545 # we can't match something that doesn't exist.
546 return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{$pubcode};
548 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{$pubcode}}) {
549 my $chroncode= substr($regularity, 0, 1);
550 my @pats = split(/,/, substr($regularity, 1));
552 if (!exists $dispatch{$chroncode}) {
553 carp "Unrecognized chroncode '$chroncode'";
558 foreach my $pat (@pats) {
559 $pat =~ s|/.+||; # If it's a combined date, match the start
560 if ($dispatch{$chroncode}->($pat, @date)) {
573 return $self->regularity_match('o', @date);
580 return $self->regularity_match('p', @date);
587 return $self->regularity_match('c', @date);
590 sub enum_is_combined {
592 my $subfield = shift;
594 my $level = ord($subfield) - ord('a') + 1;
596 return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{c};
598 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{c}}) {
599 next unless $regularity =~ m/^e$level/o;
601 my @pats = split(/,/, substr($regularity, 2));
603 foreach my $pat (@pats) {
604 $pat =~ s|/.+||; # if it's a combined issue, match the start
605 return 1 if ($iss eq $pat);
614 a => {years => 1}, # annual
615 b => {months => 2}, # bimonthly
616 c => {days => 3}, # semiweekly
617 d => {days => 1}, # daily
618 e => {weeks => 2}, # biweekly
619 f => {months => 6}, # semiannual
620 g => {years => 2}, # biennial
621 h => {years => 3}, # triennial
622 i => {days => 2}, # three times / week
623 j => {days => 10}, # three times /month
625 m => {months => 1}, # monthly
626 q => {months => 3}, # quarterly
627 s => {days => 15}, # semimonthly
628 t => {months => 4}, # three times / year
629 w => {weeks => 1}, # weekly
630 # x => completely irregular
637 if (scalar(@new) == 1) {
638 # only a year is specified. Next date is easy
639 $new[0] += $incr->{years} || 1;
640 } elsif (scalar(@new) == 2) {
641 # Year and month or season
644 $new[1] += ($incr->{months}/3) || 1;
648 $new[1] -= 4; # 25 - 4 == 21 == Spring after Winter
652 $new[1] += $incr->{months} || 1;
658 $new[1] = '0' . $new[1] if ($new[1] < 10);
660 } elsif (scalar(@new) == 3) {
661 # Year, Month, Day: now it gets complicated.
663 if ($new[2] =~ /^[0-9]+$/) {
664 # A single number for the day of month, relatively simple
665 my $dt = DateTime->new(year => $new[0],
670 $new[1] = $dt->month;
673 $new[1] = '0' . $new[1] if ($new[1] < 10);
674 $new[2] = '0' . $new[2] if ($new[2] < 10);
676 warn("Don't know how to cope with @new");
682 # Test to see if $m1/$d1 is on or after $m2/$d2
683 # if $d2 is undefined, test is based on just months
685 my ($m1, $d1, $m2, $d2) = @_;
688 || ($m1 == $m2 && ((!defined $d2) || ($d1 >= $d2))));
691 sub calendar_increment {
695 my $cal_change = $self->calendar_change;
701 # A calendar change is defined, need to check if it applies
702 if ((scalar(@new) == 2 && $new[1] > 20) || (scalar(@new) == 1)) {
703 carp "Can't calculate date change for ", $self->as_string;
707 foreach my $change (@{$cal_change}) {
710 if (length($change) == 2) {
712 } elsif (length($change) == 4) {
713 ($month, $day) = unpack("a2a2", $change);
716 if ($cur->[0] == $new[0]) {
717 # Same year, so a 'simple' month/day comparison will be fine
718 $incr = (!on_or_after($cur->[1], $cur->[2], $month, $day)
719 && on_or_after($new[1], $new[2], $month, $day));
721 # @cur is in the year before @new. There are
722 # two possible cases for the calendar change date that
723 # indicate that it's time to change the volume:
724 # (1) the change date is AFTER @cur in the year, or
725 # (2) the change date is BEFORE @new in the year.
727 # -------|------|------X------|------|
728 # @cur (1) Jan 1 (2) @new
730 $incr = (on_or_after($new[1], $new[2], $month, $day)
731 || !on_or_after($cur->[1], $cur->[2], $month, $day));
733 return $incr if $incr;
747 my $reg = $self->{_mfhdc_REGULARITY};
748 my $pattern = $self->{_mfhdc_PATTERN};
749 my $freq = $pattern->{w};
751 foreach my $i (0..$#keys) {
752 $new[$i] = $cur[$i] = $next->{$keys[$i]} if exists $next->{$keys[$i]};
755 # If the current issue has a combined date (eg, May/June)
756 # get rid of the first date and base the calculation
757 # on the final date in the combined issue.
758 $new[-1] =~ s|^[^/]+/||;
760 # XXX Insert new date generation code in here that uses publication
764 ### Old code: only works for simple cases
766 # If $frequency is not one of the standard codes defined in %increments
767 # then there has to be a $yp publication regularity pattern that
768 # lists the dates of publication. Use that that list to find the next
769 # date following the current one.
770 # XXX: the code doesn't handle this case yet.
771 if (!defined($freq)) {
772 carp "Undefined frequency in next_date!";
773 } elsif (!exists $increments{$freq}) {
774 carp "Don't know how to deal with frequency '$freq'!";
777 # One of the standard defined issue frequencies
779 @new = incr_date($increments{$freq}, @new);
781 while ($self->is_omitted(@new)) {
782 @new = incr_date($increments{$freq}, @new);
785 if ($self->is_combined(@new)) {
786 my @second_date = incr_date($increments{$freq}, @new);
788 # I am cheating: This code assumes that only the smallest
789 # time increment is combined. So, no "Apr 15/May 1" allowed.
790 $new[-1] = $new[-1] . '/' . $second_date[-1];
794 for my $i (0..$#new) {
795 $next->{$keys[$i]} = $new[$i];
798 # Figure out if we need to adust volume number
799 # right now just use the $carry that was passed in.
800 # in long run, need to base this on ($carry or date_change)
802 # if $carry is set, the date doesn't matter: we're not
803 # going to increment the v. number twice at year-change.
804 $next->{a} += $carry;
805 } elsif (defined $pattern->{x}) {
806 $next->{a} += $self->calendar_increment(\@cur, @new);
814 # First handle any "alternative enumeration", since they're
815 # a lot simpler, and don't depend on the the calendar
816 foreach my $key ('h', 'g') {
817 next if !exists $next->{$key};
818 if (!$self->capstr($key)) {
819 warn "Holding data exists for $key, but no caption specified";
824 my $cap = $self->capfield($key);
825 if ($cap->{RESTART} && $cap->{COUNT}
826 && ($next->{$key} == $cap->{COUNT})) {
840 # $carry keeps track of whether we need to carry into the next
841 # higher level of enumeration. It's not actually necessary except
842 # for when the loop ends: if we need to carry from $b into $a
843 # then $carry will be set when the loop ends.
845 # We need to keep track of this because there are two different
846 # reasons why we might increment the highest level of enumeration ($a)
847 # 1) we hit the correct number of items in $b (ie, 5th iss of quarterly)
848 # 2) it's the right time of the year.
851 foreach my $key (reverse('b'..'f')) {
852 next if !exists $next->{$key};
854 if (!$self->capstr($key)) {
855 # Just assume that it increments continuously and give up
856 warn "Holding data exists for $key, but no caption specified";
862 # If the current issue has a combined issue number (eg, 2/3)
863 # get rid of the first issue number and base the calculation
864 # on the final issue number in the combined issue.
865 if ($next->{$key} =~ m|/|) {
866 $next->{$key} =~ s|^[^/]+/||;
869 my $cap = $self->capfield($key);
870 if ($cap->{RESTART} && $cap->{COUNT}
871 && ($next->{$key} eq $cap->{COUNT})) {
875 # If I don't need to "carry" beyond here, then I just increment
876 # this level of the enumeration and stop looping, since the
877 # "next" hash has been initialized with the current values
883 # You can't have a combined issue that spans two volumes: no.12/1
885 if ($self->enum_is_combined($key, $next->{$key})) {
886 $next->{$key} .= '/' . ($next->{$key} + 1);
892 # The easy part is done. There are two things left to do:
893 # 1) Calculate the date of the next issue, if necessary
894 # 2) Increment the highest level of enumeration (either by date
895 # or because $carry is set because of the above loop
897 if (!$self->subfield('i')) {
898 # The simple case: if there is no chronology specified
899 # then just check $carry and return
900 $next->{'a'} += $carry;
902 # Figure out date of next issue, then decide if we need
903 # to adjust top level enumeration based on that
904 $self->next_date($next, $carry, ('i'..'m'));
913 # Initialize $next with current enumeration & chronology, then
914 # we can just operate on $next, based on the contents of the caption
916 if ($self->enumeration_is_chronology) {
917 foreach my $key ('a' .. 'h') {
918 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}
919 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
921 $self->next_date($next, 0, ('a' .. 'h'));
926 foreach my $key ('a' .. 'h') {
927 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}->{HOLDINGS}
928 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
931 foreach my $key ('i'..'m') {
932 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}
933 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
936 if (exists $next->{'h'}) {
937 $self->next_alt_enum($next);
940 $self->next_enum($next);