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[3];
239 } elsif (length($pat) == 4) {
242 $mon = substr($pat, 0, 2);
243 $day = substr($pat, 2, 2);
245 return (($mon == $date[1]) && ($day == $date[2]));
247 carp "Invalid day pattern '$pat'";
252 # Calcuate date of "n"th last "dayname" of month: second last Tuesday
253 sub last_week_of_month {
257 my $end_dt = DateTime->last_day_of_month(year => $dt->year,
258 month => $dt->month);
260 $day = $daynames{$day};
261 while ($end_dt->day_of_week != $day) {
262 $end_dt->subtract(days => 1);
265 # 99: last week of month, 98: second last, etc.
266 for (my $i = 99 - $week; $i > 0; $i--) {
267 $end_dt->subtract(weeks => 1);
281 return (($dt->month == $month)
282 && (($dt->week_of_month == $weekno)
283 || ($dt->week_of_month == last_day_of_month($dt, $weekno, 'th')->week_of_month)));
287 if ($daynames{$day} != $dt->day_of_week) {
288 # if it's the wrong day of the week, rest doesn't matter
292 if (!defined $month) {
294 return (($dt->weekday_of_month == $weekno)
295 || ($dt->weekday_of_month == last_day_of_month($dt, $weekno, $day)->weekday_of_month));
299 if ($month != $dt->month) {
300 # If it's the wrong month, then we're done
304 # It's the right day of the week
305 # It's the right month
307 if ($weekno == $dt->weekday_of_month) {
308 # If this matches, then we're counting from the beginning
309 # of the month and it matches and we're done.
313 # only case left is that the week number is counting from
314 # the end of the month: eg, second last wednesday
315 return (last_week_of_month($weekno, $day)->weekday_of_month == $dt->weekday_of_month);
321 my $dt = DateTime->new(year => $date[0],
325 if ($pat =~ m/^$weekpat$daypat$/) {
326 # WWdd: 03we = Third Wednesday
327 return check_date($dt, undef, $1, $2);
328 } elsif ($pat =~ m/^$monthpat$weekpat$daypat$/) {
329 # MMWWdd: 0599tu Last Tuesday in May XXX WRITE ME
330 return check_date($dt, $1, $2, $3);
331 } elsif ($pat =~ m/^$monthpat$weekpat$/) {
332 # MMWW: 1204: Fourth week in December XXX WRITE ME
333 return check_date($dt, $1, $2, undef);
335 carp "invalid week pattern '$pat'";
344 return ($pat eq $date[1]);
351 return ($pat eq $date[1]);
366 # We handle enumeration patterns separately. This just
367 # ensures that when we're processing chronological patterns
368 # we don't match an enumeration pattern.
374 'e' => \&match_issue, # not really a "chron" code
376 'm' => \&match_month,
377 's' => \&match_season,
381 sub regularity_match {
386 # we can't match something that doesn't exist.
387 return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{$pubcode};
389 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{$pubcode}}) {
390 my $chroncode= substr($regularity, 0, 1);
391 my @pats = split(/,/, substr($regularity, 1));
393 if (!exists $dispatch{$chroncode}) {
394 carp "Unrecognized chroncode '$chroncode'";
399 foreach my $pat (@pats) {
400 $pat =~ s|/.+||; # If it's a combined date, match the start
401 if ($dispatch{$chroncode}->($pat, @date)) {
414 return $self->regularity_match('o', @date);
421 return $self->regularity_match('p', @date);
428 return $self->regularity_match('c', @date);
431 sub enum_is_combined {
433 my $subfield = shift;
435 my $level = ord($subfield) - ord('a') + 1;
437 return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{c};
439 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{c}}) {
440 next unless $regularity =~ m/^e$level/o;
442 my @pats = split(/,/, substr($regularity, 2));
444 foreach my $pat (@pats) {
445 $pat =~ s|/.+||; # if it's a combined issue, match the start
446 return 1 if ($iss eq $pat);
455 a => {years => 1}, # annual
456 b => {months => 2}, # bimonthly
457 c => {days => 3}, # semiweekly
458 d => {days => 1}, # daily
459 e => {weeks => 2}, # biweekly
460 f => {months => 6}, # semiannual
461 g => {years => 2}, # biennial
462 h => {years => 3}, # triennial
463 i => {days => 2}, # three times / week
464 j => {days => 10}, # three times /month
466 m => {months => 1}, # monthly
467 q => {months => 3}, # quarterly
468 s => {days => 15}, # semimonthly
469 t => {months => 4}, # three times / year
470 w => {weeks => 1}, # weekly
471 # x => completely irregular
478 if (scalar(@new) == 1) {
479 # only a year is specified. Next date is easy
480 $new[0] += $incr->{years} || 1;
481 } elsif (scalar(@new) == 2) {
482 # Year and month or season
485 $new[1] += ($incr->{months}/3) || 1;
489 $new[1] -= 4; # 25 - 4 == 21 == Spring after Winter
493 $new[1] += $incr->{months} || 1;
499 $new[1] = '0' . $new[1] if ($new[1] < 10);
501 } elsif (scalar(@new) == 3) {
502 # Year, Month, Day: now it gets complicated.
504 if ($new[2] =~ /^[0-9]+$/) {
505 # A single number for the day of month, relatively simple
506 my $dt = DateTime->new(year => $new[0],
511 $new[1] = $dt->month;
514 $new[1] = '0' . $new[1] if ($new[1] < 10);
515 $new[2] = '0' . $new[2] if ($new[2] < 10);
517 warn("Don't know how to cope with @new");
523 # Test to see if $m1/$d1 is on or after $m2/$d2
524 # if $d2 is undefined, test is based on just months
526 my ($m1, $d1, $m2, $d2) = @_;
529 || ($m1 == $m2 && ((!defined $d2) || ($d1 >= $d2))));
532 sub calendar_increment {
536 my $cal_change = $self->calendar_change;
542 # A calendar change is defined, need to check if it applies
543 if ((scalar(@new) == 2 && $new[1] > 20) || (scalar(@new) == 1)) {
544 carp "Can't calculate date change for ", $self->as_string;
548 foreach my $change (@{$cal_change}) {
551 if (length($change) == 2) {
553 } elsif (length($change) == 4) {
554 ($month, $day) = unpack("a2a2", $change);
557 if ($cur->[0] == $new[0]) {
558 # Same year, so a 'simple' month/day comparison will be fine
559 $incr = (!on_or_after($cur->[1], $cur->[2], $month, $day)
560 && on_or_after($new[1], $new[2], $month, $day));
562 # @cur is in the year before @new. There are
563 # two possible cases for the calendar change date that
564 # indicate that it's time to change the volume:
565 # (1) the change date is AFTER @cur in the year, or
566 # (2) the change date is BEFORE @new in the year.
568 # -------|------|------X------|------|
569 # @cur (1) Jan 1 (2) @new
571 $incr = (on_or_after($new[1], $new[2], $month, $day)
572 || !on_or_after($cur->[1], $cur->[2], $month, $day));
574 return $incr if $incr;
587 my $reg = $self->{_mfhdc_REGULARITY};
588 my $pattern = $self->{_mfhdc_PATTERN};
589 my $freq = $pattern->{w};
591 foreach my $i (0..$#keys) {
592 $new[$i] = $cur[$i] = $next->{$keys[$i]} if exists $next->{$keys[$i]};
595 # If the current issue has a combined date (eg, May/June)
596 # get rid of the first date and base the calculation
597 # on the final date in the combined issue.
598 $new[-1] =~ s|^[^/]+/||;
600 # If $frequency is not one of the standard codes defined in %increments
601 # then there has to be a $yp publication regularity pattern that
602 # lists the dates of publication. Use that that list to find the next
603 # date following the current one.
604 # XXX: the code doesn't handle this case yet.
605 if (!defined($freq)) {
606 carp "Undefined frequency in next_date!";
607 } elsif (!exists $increments{$freq}) {
608 carp "Don't know how to deal with frequency '$freq'!";
611 # One of the standard defined issue frequencies
613 @new = incr_date($increments{$freq}, @new);
615 while ($self->is_omitted(@new)) {
616 @new = incr_date($increments{$freq}, @new);
619 if ($self->is_combined(@new)) {
620 my @second_date = incr_date($increments{$freq}, @new);
622 # I am cheating: This code assumes that only the smallest
623 # time increment is combined. So, no "Apr 15/May 1" allowed.
624 $new[-1] = $new[-1] . '/' . $second_date[-1];
628 for my $i (0..$#new) {
629 $next->{$keys[$i]} = $new[$i];
632 # Figure out if we need to adust volume number
633 # right now just use the $carry that was passed in.
634 # in long run, need to base this on ($carry or date_change)
636 # if $carry is set, the date doesn't matter: we're not
637 # going to increment the v. number twice at year-change.
638 $next->{a} += $carry;
639 } elsif (defined $self->{_mfhdc_PATTERN}->{x}) {
640 $next->{a} += $self->calendar_increment(\@cur, @new);
648 # First handle any "alternative enumeration", since they're
649 # a lot simpler, and don't depend on the the calendar
650 foreach my $key ('h', 'g') {
651 next if !exists $next->{$key};
652 if (!$self->capstr($key)) {
653 warn "Holding data exists for $key, but no caption specified";
658 my $cap = $self->capfield($key);
659 if ($cap->{RESTART} && $cap->{COUNT}
660 && ($next->{$key} == $cap->{COUNT})) {
674 # $carry keeps track of whether we need to carry into the next
675 # higher level of enumeration. It's not actually necessary except
676 # for when the loop ends: if we need to carry from $b into $a
677 # then $carry will be set when the loop ends.
679 # We need to keep track of this because there are two different
680 # reasons why we might increment the highest level of enumeration ($a)
681 # 1) we hit the correct number of items in $b (ie, 5th iss of quarterly)
682 # 2) it's the right time of the year.
685 foreach my $key (reverse('b'..'f')) {
686 next if !exists $next->{$key};
688 if (!$self->capstr($key)) {
689 # Just assume that it increments continuously and give up
690 warn "Holding data exists for $key, but no caption specified";
696 # If the current issue has a combined issue number (eg, 2/3)
697 # get rid of the first issue number and base the calculation
698 # on the final issue number in the combined issue.
699 if ($next->{$key} =~ m|/|) {
700 $next->{$key} =~ s|^[^/]+/||;
703 my $cap = $self->capfield($key);
704 if ($cap->{RESTART} && $cap->{COUNT}
705 && ($next->{$key} eq $cap->{COUNT})) {
709 # If I don't need to "carry" beyond here, then I just increment
710 # this level of the enumeration and stop looping, since the
711 # "next" hash has been initialized with the current values
717 # You can't have a combined issue that spans two volumes: no.12/1
719 if ($self->enum_is_combined($key, $next->{$key})) {
720 $next->{$key} .= '/' . ($next->{$key} + 1);
726 # The easy part is done. There are two things left to do:
727 # 1) Calculate the date of the next issue, if necessary
728 # 2) Increment the highest level of enumeration (either by date
729 # or because $carry is set because of the above loop
731 if (!$self->subfield('i')) {
732 # The simple case: if there is no chronology specified
733 # then just check $carry and return
734 $next->{'a'} += $carry;
736 # Figure out date of next issue, then decide if we need
737 # to adjust top level enumeration based on that
738 $self->next_date($next, $carry, ('i'..'m'));
747 # Initialize $next with current enumeration & chronology, then
748 # we can just operate on $next, based on the contents of the caption
750 if ($self->enumeration_is_chronology) {
751 foreach my $key ('a' .. 'h') {
752 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}
753 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
755 $self->next_date($next, 0, ('a' .. 'h'));
760 foreach my $key ('a' .. 'h') {
761 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}->{HOLDINGS}
762 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
765 foreach my $key ('i'..'m') {
766 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}
767 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
770 if (exists $next->{'h'}) {
771 $self->next_alt_enum($next);
774 $self->next_enum($next);