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]);
513 sub subsequent_season {
517 return undef if (($pat < 21) || ($pat > 24));
519 if ($cur->[1] >= $pat) {
520 # current season is on or past pattern season in this year,
521 # advance to next year
524 # Either we've advanced to the next year or the current season
525 # is before the pattern season in the current year. Either way,
526 # all that remains is to set the season properly
540 sub subsequent_year {
552 # We handle enumeration patterns separately. This just
553 # ensures that when we're processing chronological patterns
554 # we don't match an enumeration pattern.
558 sub subsequent_issue {
562 # Issue generation is handled separately
568 'e' => \&match_issue, # not really a "chron" code
570 'm' => \&match_month,
571 's' => \&match_season,
575 sub regularity_match {
580 # we can't match something that doesn't exist.
581 return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{$pubcode};
583 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{$pubcode}}) {
584 my $chroncode= substr($regularity, 0, 1);
585 my @pats = split(/,/, substr($regularity, 1));
587 if (!exists $dispatch{$chroncode}) {
588 carp "Unrecognized chroncode '$chroncode'";
593 foreach my $pat (@pats) {
594 $pat =~ s|/.+||; # If it's a combined date, match the start
595 if ($dispatch{$chroncode}->($pat, @date)) {
608 return $self->regularity_match('o', @date);
615 return $self->regularity_match('p', @date);
622 return $self->regularity_match('c', @date);
625 sub enum_is_combined {
627 my $subfield = shift;
629 my $level = ord($subfield) - ord('a') + 1;
631 return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{c};
633 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{c}}) {
634 next unless $regularity =~ m/^e$level/o;
636 my @pats = split(/,/, substr($regularity, 2));
638 foreach my $pat (@pats) {
639 $pat =~ s|/.+||; # if it's a combined issue, match the start
640 return 1 if ($iss eq $pat);
649 a => {years => 1}, # annual
650 b => {months => 2}, # bimonthly
651 c => {days => 3}, # semiweekly
652 d => {days => 1}, # daily
653 e => {weeks => 2}, # biweekly
654 f => {months => 6}, # semiannual
655 g => {years => 2}, # biennial
656 h => {years => 3}, # triennial
657 i => {days => 2}, # three times / week
658 j => {days => 10}, # three times /month
660 m => {months => 1}, # monthly
661 q => {months => 3}, # quarterly
662 s => {days => 15}, # semimonthly
663 t => {months => 4}, # three times / year
664 w => {weeks => 1}, # weekly
665 # x => completely irregular
672 if (scalar(@new) == 1) {
673 # only a year is specified. Next date is easy
674 $new[0] += $incr->{years} || 1;
675 } elsif (scalar(@new) == 2) {
676 # Year and month or season
679 $new[1] += ($incr->{months}/3) || 1;
683 $new[1] -= 4; # 25 - 4 == 21 == Spring after Winter
687 $new[1] += $incr->{months} || 1;
693 $new[1] = '0' . $new[1] if ($new[1] < 10);
695 } elsif (scalar(@new) == 3) {
696 # Year, Month, Day: now it gets complicated.
698 if ($new[2] =~ /^[0-9]+$/) {
699 # A single number for the day of month, relatively simple
700 my $dt = DateTime->new(year => $new[0],
705 $new[1] = $dt->month;
708 $new[1] = '0' . $new[1] if ($new[1] < 10);
709 $new[2] = '0' . $new[2] if ($new[2] < 10);
711 warn("Don't know how to cope with @new");
717 # Test to see if $m1/$d1 is on or after $m2/$d2
718 # if $d2 is undefined, test is based on just months
720 my ($m1, $d1, $m2, $d2) = @_;
723 || ($m1 == $m2 && ((!defined $d2) || ($d1 >= $d2))));
726 sub calendar_increment {
730 my $cal_change = $self->calendar_change;
736 # A calendar change is defined, need to check if it applies
737 if ((scalar(@new) == 2 && $new[1] > 20) || (scalar(@new) == 1)) {
738 carp "Can't calculate date change for ", $self->as_string;
742 foreach my $change (@{$cal_change}) {
745 if (length($change) == 2) {
747 } elsif (length($change) == 4) {
748 ($month, $day) = unpack("a2a2", $change);
751 if ($cur->[0] == $new[0]) {
752 # Same year, so a 'simple' month/day comparison will be fine
753 $incr = (!on_or_after($cur->[1], $cur->[2], $month, $day)
754 && on_or_after($new[1], $new[2], $month, $day));
756 # @cur is in the year before @new. There are
757 # two possible cases for the calendar change date that
758 # indicate that it's time to change the volume:
759 # (1) the change date is AFTER @cur in the year, or
760 # (2) the change date is BEFORE @new in the year.
762 # -------|------|------X------|------|
763 # @cur (1) Jan 1 (2) @new
765 $incr = (on_or_after($new[1], $new[2], $month, $day)
766 || !on_or_after($cur->[1], $cur->[2], $month, $day));
768 return $incr if $incr;
773 'd' => \&subsequent_day,
774 'e' => \&subsequent_issue, # not a chron code
775 'w' => \&subsequent_week,
776 'm' => \&subsequent_month,
777 's' => \&subsequent_season,
778 'y' => \&subsequent_year,
791 my $reg = $self->{_mfhdc_REGULARITY};
792 my $pattern = $self->{_mfhdc_PATTERN};
793 my $freq = $pattern->{w};
795 foreach my $i (0..$#keys) {
796 $new[$i] = $cur[$i] = $next->{$keys[$i]} if exists $next->{$keys[$i]};
799 # If the current issue has a combined date (eg, May/June)
800 # get rid of the first date and base the calculation
801 # on the final date in the combined issue.
802 $new[-1] =~ s|^[^/]+/||;
804 # XXX Insert new date generation code in here that uses publication
808 ### Old code: only works for simple cases
810 # If $frequency is not one of the standard codes defined in %increments
811 # then there has to be a $yp publication regularity pattern that
812 # lists the dates of publication. Use that that list to find the next
813 # date following the current one.
814 # XXX: the code doesn't handle this case yet.
815 if (!defined($freq)) {
816 carp "Undefined frequency in next_date!";
817 } elsif (!exists $increments{$freq}) {
818 carp "Don't know how to deal with frequency '$freq'!";
821 # One of the standard defined issue frequencies
823 @new = incr_date($increments{$freq}, @new);
825 while ($self->is_omitted(@new)) {
826 @new = incr_date($increments{$freq}, @new);
829 if ($self->is_combined(@new)) {
830 my @second_date = incr_date($increments{$freq}, @new);
832 # I am cheating: This code assumes that only the smallest
833 # time increment is combined. So, no "Apr 15/May 1" allowed.
834 $new[-1] = $new[-1] . '/' . $second_date[-1];
838 for my $i (0..$#new) {
839 $next->{$keys[$i]} = $new[$i];
842 # Figure out if we need to adust volume number
843 # right now just use the $carry that was passed in.
844 # in long run, need to base this on ($carry or date_change)
846 # if $carry is set, the date doesn't matter: we're not
847 # going to increment the v. number twice at year-change.
848 $next->{a} += $carry;
849 } elsif (defined $pattern->{x}) {
850 $next->{a} += $self->calendar_increment(\@cur, @new);
858 # First handle any "alternative enumeration", since they're
859 # a lot simpler, and don't depend on the the calendar
860 foreach my $key ('h', 'g') {
861 next if !exists $next->{$key};
862 if (!$self->capstr($key)) {
863 warn "Holding data exists for $key, but no caption specified";
868 my $cap = $self->capfield($key);
869 if ($cap->{RESTART} && $cap->{COUNT}
870 && ($next->{$key} == $cap->{COUNT})) {
884 # $carry keeps track of whether we need to carry into the next
885 # higher level of enumeration. It's not actually necessary except
886 # for when the loop ends: if we need to carry from $b into $a
887 # then $carry will be set when the loop ends.
889 # We need to keep track of this because there are two different
890 # reasons why we might increment the highest level of enumeration ($a)
891 # 1) we hit the correct number of items in $b (ie, 5th iss of quarterly)
892 # 2) it's the right time of the year.
895 foreach my $key (reverse('b'..'f')) {
896 next if !exists $next->{$key};
898 if (!$self->capstr($key)) {
899 # Just assume that it increments continuously and give up
900 warn "Holding data exists for $key, but no caption specified";
906 # If the current issue has a combined issue number (eg, 2/3)
907 # get rid of the first issue number and base the calculation
908 # on the final issue number in the combined issue.
909 if ($next->{$key} =~ m|/|) {
910 $next->{$key} =~ s|^[^/]+/||;
913 my $cap = $self->capfield($key);
914 if ($cap->{RESTART} && $cap->{COUNT}
915 && ($next->{$key} eq $cap->{COUNT})) {
919 # If I don't need to "carry" beyond here, then I just increment
920 # this level of the enumeration and stop looping, since the
921 # "next" hash has been initialized with the current values
927 # You can't have a combined issue that spans two volumes: no.12/1
929 if ($self->enum_is_combined($key, $next->{$key})) {
930 $next->{$key} .= '/' . ($next->{$key} + 1);
936 # The easy part is done. There are two things left to do:
937 # 1) Calculate the date of the next issue, if necessary
938 # 2) Increment the highest level of enumeration (either by date
939 # or because $carry is set because of the above loop
941 if (!$self->subfield('i')) {
942 # The simple case: if there is no chronology specified
943 # then just check $carry and return
944 $next->{'a'} += $carry;
946 # Figure out date of next issue, then decide if we need
947 # to adjust top level enumeration based on that
948 $self->next_date($next, $carry, ('i'..'m'));
957 # Initialize $next with current enumeration & chronology, then
958 # we can just operate on $next, based on the contents of the caption
960 if ($self->enumeration_is_chronology) {
961 foreach my $key ('a' .. 'h') {
962 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}
963 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
965 $self->next_date($next, 0, ('a' .. 'h'));
970 foreach my $key ('a' .. 'h') {
971 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}->{HOLDINGS}
972 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
975 foreach my $key ('i'..'m') {
976 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}
977 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
980 if (exists $next->{'h'}) {
981 $self->next_alt_enum($next);
984 $self->next_enum($next);