8 use OpenILS::Utils::MFHD::Date;
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};
171 return $self->{_mfhdc_UNIT};
174 sub calendar_change {
177 return $self->{_mfhdc_PATTERN}->{x};
180 # If items are identified by chronology only, with no separate
181 # enumeration (eg, a newspaper issue), then the chronology is
182 # recorded in the enumeration subfields $a - $f. We can tell
183 # that this is the case if there are $a - $f subfields and no
184 # chronology subfields ($i-$k), and none of the $a-$f subfields
185 # have associated $u or $v subfields, but there's a $w and no $x
187 sub enumeration_is_chronology {
190 # There is always a '$a' subfield in well-formed fields.
191 return 0 if exists $self->{_mfhdc_CHRONS}->{i}
192 || exists $self->{_mfhdc_PATTERN}->{x};
194 foreach my $key ('a' .. 'f') {
197 last if !exists $self->{_mfhdc_ENUMS}->{$key};
199 $enum = $self->{_mfhdc_ENUMS}->{$key};
200 return 0 if defined $enum->{COUNT} || defined $enum->{RESTART};
203 return (exists $self->{_mfhdc_PATTERN}->{w});
206 sub regularity_match {
211 # we can't match something that doesn't exist.
212 return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{$pubcode};
214 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{$pubcode}}) {
215 my $chroncode= substr($regularity, 0, 1);
216 my $matchfunc = MFHD::Date::dispatch($chroncode);
217 my @pats = split(/,/, substr($regularity, 1));
219 if (!defined $matchfunc) {
220 carp "Unrecognized chroncode '$chroncode'";
225 foreach my $pat (@pats) {
226 $pat =~ s|/.+||; # If it's a combined date, match the start
227 if ($matchfunc->($pat, @date)) {
240 # printf("# is_omitted: testing date %s: %d\n", join('/', @date),
241 # $self->regularity_match('o', @date));
242 return $self->regularity_match('o', @date);
249 return $self->regularity_match('p', @date);
256 return $self->regularity_match('c', @date);
259 sub enum_is_combined {
261 my $subfield = shift;
263 my $level = ord($subfield) - ord('a') + 1;
265 return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{c};
267 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{c}}) {
268 next unless $regularity =~ m/^e$level/o;
270 my @pats = split(/,/, substr($regularity, 2));
272 foreach my $pat (@pats) {
273 $pat =~ s|/.+||; # if it's a combined issue, match the start
274 return 1 if ($iss eq $pat);
282 # Test to see if $dt1 is on or after $dt2
283 # if length(@{$dt2} == 2, then just month/day are compared
284 # if length(@{$dt2} == 1, then just the months are compared
289 # printf("# on_or_after(%s, %s): ", join('/', @{$dt1}), join('/', @{$dt2}));
291 foreach my $i (0..(scalar(@{$dt2})-1)) {
292 if ($dt1->[$i] > $dt2->[$i]) {
293 # printf("after - pass\n");
294 # $dt1 occurs AFTER $dt2
296 } elsif ($dt1->[$i] < $dt2->[$i]) {
297 # printf("before - fail\n");
298 # $dt1 occurs BEFORE $dt2
301 # both are still equal, keep going
304 # We fell out of the loop with them being equal, so it's 'on'
305 # printf("on - pass\n");
309 sub calendar_increment {
313 my $cal_change = $self->calendar_change;
319 # A calendar change is defined, need to check if it applies
320 if (scalar(@{$new}) == 1) {
321 carp "Can't calculate date change for ", $self->as_string;
325 foreach my $change (@{$cal_change}) {
328 if (length($change) == 2) {
330 } elsif (length($change) == 4) {
331 ($month, $day) = unpack("a2a2", $change);
334 # printf("# calendar_increment('%s', '%s'): change on '%s/%s'\n",
335 # join('/', @{$cur}), join('/', @{$new}),
336 # $month, defined($day) ? $day : 'UNDEF');
338 if ($cur->[0] == $new->[0]) {
339 # Same year, so a 'simple' month/day comparison will be fine
340 $incr = (!on_or_after([$cur->[1], $cur->[2]], [$month, $day])
341 && on_or_after([$new->[1], $new->[2]], [$month, $day]));
343 # @cur is in the year before @new. There are
344 # two possible cases for the calendar change date that
345 # indicate that it's time to change the volume:
346 # (1) the change date is AFTER @cur in the year, or
347 # (2) the change date is BEFORE @new in the year.
349 # -------|------|------X------|------|
350 # @cur (1) Jan 1 (2) @new
352 $incr = (on_or_after([$new->[1], $new->[2]], [$month, $day])
353 || !on_or_after([$cur->[1], $cur->[2]], [$month, $day]));
355 return $incr if $incr;
368 my @newend; # only used for combined issues
371 my $reg = $self->{_mfhdc_REGULARITY};
372 my $pattern = $self->{_mfhdc_PATTERN};
373 my $freq = $pattern->{w};
375 foreach my $i (0..$#keys) {
376 $cur[$i] = $next->{$keys[$i]} if exists $next->{$keys[$i]};
379 # If the current issue has a combined date (eg, May/June)
380 # get rid of the first date and base the calculation
381 # on the final date in the combined issue.
382 $cur[-1] =~ s|^[^/]+/||;
384 if (defined $pattern->{y}->{p}) {
385 # There is a $y publication pattern defined in the record:
386 # use it to calculate the next issue date.
388 foreach my $pubpat (@{$pattern->{y}->{p}}, @{$pattern->{y}->{c}}) {
389 my $chroncode = substr($pubpat, 0, 1);
390 my $genfunc = MFHD::Date::generator($chroncode);
391 my @pats = split(/,/, substr($pubpat, 1));
393 next if $chroncode eq 'e';
395 if (!defined $genfunc) {
396 carp "Unrecognized chroncode '$chroncode'";
400 foreach my $pat (@pats) {
401 my $combined = $pat =~ m|/|;
405 # printf("# next_date: generating with pattern '%s'\n", $pat);
408 ($start, $end) = split('/', $pat, 2);
410 ($start, $end) = (undef, undef);
413 @candidate = $genfunc->($start || $pat, @cur);
415 while ($self->is_omitted(@candidate)) {
416 # printf("# pubpat omitting date '%s'\n",
417 # join('/', @candidate));
418 @candidate = $genfunc->($start || $pat, @candidate);
421 # printf("# testing new candidate '%s' against '%s'\n",
422 # join('/', @candidate), join('/', @new));
424 if (!defined($new[0])
425 || !on_or_after(\@candidate, \@new)) {
426 # first time through the loop
427 # or @candidate is before @new =>
428 # @candidate is the next issue.
431 @newend = $genfunc->($end, @cur);
436 # printf("# selecting candidate date '%s'\n", join('/', @new));
441 if (defined($newend[0])) {
442 # The best match was a combined issue
443 foreach my $i (0..$#new) {
444 # don't combine identical fields
445 next if $new[$i] eq $newend[$i];
446 $new[$i] .= '/' . $newend[$i];
451 if (scalar @new == 0) {
452 # There was no suitable publication pattern defined,
453 # so use the $w frequency to figure out the next date
454 if (!defined($freq)) {
455 carp "Undefined frequency in next_date!";
456 } elsif (!MFHD::Date::can_increment($freq)) {
457 carp "Don't know how to deal with frequency '$freq'!";
460 # One of the standard defined issue frequencies
462 @new = MFHD::Date::incr_date($freq, @cur);
464 while ($self->is_omitted(@new)) {
465 @new = MFHD::Date::incr_date($freq, @new);
468 if ($self->is_combined(@new)) {
469 my @second_date = MFHD::Date::incr_date($freq, @new);
471 # I am cheating: This code assumes that only the smallest
472 # time increment is combined. So, no "Apr 15/May 1" allowed.
473 $new[-1] = $new[-1] . '/' . $second_date[-1];
478 for my $i (0..$#new) {
479 $next->{$keys[$i]} = $new[$i];
481 # Figure out if we need to adust volume number
482 # right now just use the $carry that was passed in.
483 # in long run, need to base this on ($carry or date_change)
485 # if $carry is set, the date doesn't matter: we're not
486 # going to increment the v. number twice at year-change.
487 $next->{a} += $carry;
488 } elsif (defined $pattern->{x}) {
489 $next->{a} += $self->calendar_increment(\@cur, \@new);
497 # First handle any "alternative enumeration", since they're
498 # a lot simpler, and don't depend on the the calendar
499 foreach my $key ('h', 'g') {
500 next if !exists $next->{$key};
501 if (!$self->capstr($key)) {
502 warn "Holding data exists for $key, but no caption specified";
507 my $cap = $self->capfield($key);
508 if ($cap->{RESTART} && $cap->{COUNT}
509 && ($next->{$key} == $cap->{COUNT})) {
518 # Check caption for $ype subfield, specifying that there's a
519 # particular publication pattern for the given level of enumeration
520 # returns the pattern string or undef
525 return undef if !exists $self->{_mfhdc_PATTERN}->{y}->{p};
527 foreach my $reg (@{$self->{_mfhdc_PATTERN}->{y}->{p}}) {
528 if ($reg =~ m/^e$level/o) {
529 return substr($reg, 2);
540 # $carry keeps track of whether we need to carry into the next
541 # higher level of enumeration. It's not actually necessary except
542 # for when the loop ends: if we need to carry from $b into $a
543 # then $carry will be set when the loop ends.
545 # We need to keep track of this because there are two different
546 # reasons why we might increment the highest level of enumeration ($a)
547 # 1) we hit the correct number of items in $b (ie, 5th iss of quarterly)
548 # 2) it's the right time of the year.
551 # If there's a subfield b, then we will go through the loop at
552 # least once. If there's no subfield b, then there's only a single
553 # level of enumeration, so we just add one to it and we're done.
554 if (exists $next->{b}) {
559 foreach my $key (reverse('b'..'f')) {
563 next if !exists $next->{$key};
565 # If the current issue has a combined issue number (eg, 2/3)
566 # get rid of the first issue number and base the calculation
567 # on the final issue number in the combined issue.
568 if ($next->{$key} =~ m|/|) {
569 $next->{$key} =~ s|^[^/]+/||;
572 $level = ord($key) - ord('a') + 1; # enumeration level
574 $pubpat = $self->enum_pubpat($level);
577 # printf("# next_enum: found pubpat '%s' for subfield '%s'\n",
579 my @pats = split(/,/, $pubpat);
581 # If we fall out the bottom of the loop, then $carry
582 # will still be 1, and we will reset the current
583 # level to the first value in @pats and increment
584 # then next higher level.
587 foreach my $pat (@pats) {
588 my $combined = $pat =~ m|/|;
591 # printf("# next_enum: checking current '%s' against pat '%s'\n",
592 # $next->{$key}, $pat);
595 ($pat, $end) = split('/', $pat, 2);
600 if ($pat > $next->{$key}) {
602 $next->{$key} = $pat;
603 $next->{$key} .= '/' . $end if $end;
604 # printf("# next_enum: selecting new issue no. %s\n", $next->{$key});
605 last; # We've found the correct next issue number
609 $next->{$key} = $pats[0];
611 last; # exit the top level loop because we're done
615 # No enumeration publication pattern specified for this level,
616 # just keed adding one.
618 if (!$self->capstr($key)) {
619 # Just assume that it increments continuously and give up
620 warn "Holding data exists for $key, but no caption specified";
626 # printf("# next_enum: no publication pattern, using frequency\n");
628 my $cap = $self->capfield($key);
629 if ($cap->{RESTART} && $cap->{COUNT}
630 && ($next->{$key} eq $cap->{COUNT})) {
634 # If I don't need to "carry" beyond here, then I just increment
635 # this level of the enumeration and stop looping, since the
636 # "next" hash has been initialized with the current values
642 # You can't have a combined issue that spans two volumes: no.12/1
644 if ($self->enum_is_combined($key, $next->{$key})) {
645 $next->{$key} .= '/' . ($next->{$key} + 1);
652 # The easy part is done. There are two things left to do:
653 # 1) Calculate the date of the next issue, if necessary
654 # 2) Increment the highest level of enumeration (either by date
655 # or because $carry is set because of the above loop
657 if (!$self->subfield('i')) {
658 # The simple case: if there is no chronology specified
659 # then just check $carry and return
660 $next->{'a'} += $carry;
662 # Figure out date of next issue, then decide if we need
663 # to adjust top level enumeration based on that
664 $self->next_date($next, $carry, ('i'..'m'));
673 # Initialize $next with current enumeration & chronology, then
674 # we can just operate on $next, based on the contents of the caption
676 if ($self->enumeration_is_chronology) {
677 foreach my $key ('a' .. 'h') {
678 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}
679 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
681 $self->next_date($next, 0, ('a' .. 'h'));
686 foreach my $key ('a' .. 'h') {
687 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}->{HOLDINGS}
688 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
691 foreach my $key ('i'..'m') {
692 $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}
693 if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
696 if (exists $next->{'h'}) {
697 $self->next_alt_enum($next);
700 $self->next_enum($next);