8 use OpenILS::Utils::MFHD::Date;
10 use base 'MARC::Field';
14 my $class = ref($proto) || $proto;
16 my $last_enum = undef;
18 $self->{_mfhdc_ENUMS} = {};
19 $self->{_mfhdc_CHRONS} = {};
20 $self->{_mfhdc_PATTERN} = {};
21 $self->{_mfhdc_COPY} = undef;
22 $self->{_mfhdc_UNIT} = undef;
23 $self->{_mfhdc_LINK_ID} = undef;
24 $self->{_mfhdc_COMPRESSIBLE} = 1; # until proven otherwise
26 foreach my $subfield ($self->subfields) {
27 my ($key, $val) = @$subfield;
29 $self->{_mfhdc_LINK_ID} = $val;
30 } elsif ($key =~ /[a-h]/) {
31 # Enumeration Captions
32 $self->{_mfhdc_ENUMS}->{$key} = {
42 } elsif ($key =~ /[i-m]/) {
44 $self->{_mfhdc_CHRONS}->{$key} = $val;
45 } elsif ($key eq 'u') {
46 # Bib units per next higher enumeration level
48 # Some files seem to have "empty" $u subfields,
49 # especially for top level of enumeration. Just drop them
50 next if (!defined($val) || !$val);
52 carp('$u specified for top-level enumeration')
53 unless defined($last_enum);
54 $self->{_mfhdc_ENUMS}->{$last_enum}->{COUNT} = $val;
55 } elsif ($key eq 'v') {
56 # Is this level of enumeration continuous, or does it restart?
58 # Some files seem to have "empty" $v subfields,
59 # especially for top level of enumeration. Just drop them
60 next if (!defined($val) || !$val);
62 carp '$v specified for top-level enumeration'
63 unless defined($last_enum);
64 $self->{_mfhdc_ENUMS}->{$last_enum}->{RESTART} = ($val eq 'r');
65 } elsif ($key =~ /[npwz]/) {
66 # Publication Pattern info ('o' == type of unit, 'q'..'t' undefined)
67 $self->{_mfhdc_PATTERN}->{$key} = $val;
68 } elsif ($key =~ /x/) {
69 # Calendar change can have multiple comma-separated values
70 $self->{_mfhdc_PATTERN}->{x} = [split /,/, $val];
71 } elsif ($key eq 'y') {
72 $self->{_mfhdc_PATTERN}->{y} = {}
73 unless exists $self->{_mfhdc_PATTERN}->{y};
74 update_pattern($self, $val);
75 } elsif ($key eq 'o') {
77 $self->{_mfhdc_UNIT} = $val;
78 } elsif ($key eq 't') {
79 $self->{_mfhdc_COPY} = $val;
81 carp "Unknown caption subfield '$key'";
85 # subsequent levels of enumeration (primary and alternate)
86 # If an enumeration level doesn't document the number
87 # of "issues" per "volume", or whether numbering of issues
88 # restarts, then we can't compress.
89 foreach my $key ('b', 'c', 'd', 'e', 'f', 'h') {
90 if (exists $self->{_mfhdc_ENUMS}->{$key}) {
91 my $pattern = $self->{_mfhdc_ENUMS}->{$key};
92 if ( !$pattern->{RESTART}
94 || ($pattern->{COUNT} eq 'var')
95 || ($pattern->{COUNT} eq 'und')) {
96 $self->{_mfhdc_COMPRESSIBLE} = 0;
102 my $pat = $self->{_mfhdc_PATTERN};
104 # Sanity check publication frequency vs publication pattern:
105 # if the frequency is a number, then the pattern better
106 # have that number of values associated with it.
107 if ( exists($pat->{w})
108 && ($pat->{w} =~ /^\d+$/)
109 && ($pat->{w} != scalar(@{$pat->{y}->{p}}))) {
111 "Caption::new: publication frequency '$pat->{w}' != publication pattern @{$pat->{y}->{p}}"
115 # If there's a $x subfield and a $j, then it's compressible
116 if (exists $pat->{x} && exists $self->{_mfhdc_CHRONS}->{'j'}) {
117 $self->{_mfhdc_COMPRESSIBLE} = 1;
120 bless($self, $class);
128 my $pathash = $self->{_mfhdc_PATTERN}->{y};
129 my ($pubcode, $pat) = unpack("a1a*", $val);
131 $pathash->{$pubcode} = [] unless exists $pathash->{$pubcode};
132 push @{$pathash->{$pubcode}}, $pat;
137 my $pattern = $self->{_mfhdc_PATTERN}->{y};
145 return $self->{_mfhdc_COMPRESSIBLE};
152 if (exists $self->{_mfhdc_CHRONS}->{$key}) {
153 return $self->{_mfhdc_CHRONS}->{$key};
163 if (exists $self->{_mfhdc_ENUMS}->{$key}) {
164 return $self->{_mfhdc_ENUMS}->{$key};
165 } elsif (exists $self->{_mfhdc_CHRONS}->{$key}) {
166 return $self->{_mfhdc_CHRONS}->{$key};
175 my $val = $self->capfield($key);
178 return $val->{CAPTION};
187 return $self->{_mfhdc_UNIT};
193 return $self->{_mfhdc_LINK_ID};
196 sub calendar_change {
199 return $self->{_mfhdc_PATTERN}->{x};
202 # If items are identified by chronology only, with no separate
203 # enumeration (eg, a newspaper issue), then the chronology is
204 # recorded in the enumeration subfields $a - $f. We can tell
205 # that this is the case if there are $a - $f subfields and no
206 # chronology subfields ($i-$k), and none of the $a-$f subfields
207 # have associated $u or $v subfields, but there's a $w and no $x
209 sub enumeration_is_chronology {
212 # There is always a '$a' subfield in well-formed fields.
214 if exists $self->{_mfhdc_CHRONS}->{i}
215 || exists $self->{_mfhdc_PATTERN}->{x};
217 foreach my $key ('a'..'f') {
220 last if !exists $self->{_mfhdc_ENUMS}->{$key};
222 $enum = $self->{_mfhdc_ENUMS}->{$key};
223 return 0 if defined $enum->{COUNT} || defined $enum->{RESTART};
226 return (exists $self->{_mfhdc_PATTERN}->{w});
229 sub regularity_match {
234 # we can't match something that doesn't exist.
235 return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{$pubcode};
237 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{$pubcode}}) {
238 my $chroncode = substr($regularity, 0, 1);
239 my $matchfunc = MFHD::Date::dispatch($chroncode);
240 my @pats = split(/,/, substr($regularity, 1));
242 if (!defined $matchfunc) {
243 carp "Unrecognized chroncode '$chroncode'";
248 foreach my $pat (@pats) {
249 $pat =~ s|/.+||; # If it's a combined date, match the start
250 if ($matchfunc->($pat, @date)) {
263 # printf("# is_omitted: testing date %s: %d\n", join('/', @date),
264 # $self->regularity_match('o', @date));
265 return $self->regularity_match('o', @date);
272 return $self->regularity_match('p', @date);
279 return $self->regularity_match('c', @date);
282 sub enum_is_combined {
284 my $subfield = shift;
286 my $level = ord($subfield) - ord('a') + 1;
288 return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{c};
290 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{c}}) {
291 next unless $regularity =~ m/^e$level/o;
293 my @pats = split(/,/, substr($regularity, 2));
295 foreach my $pat (@pats) {
296 $pat =~ s|/.+||; # if it's a combined issue, match the start
297 return 1 if ($iss eq $pat);
304 # Test to see if $dt1 is on or after $dt2
305 # if length(@{$dt2} == 2, then just month/day are compared
306 # if length(@{$dt2} == 1, then just the months are compared
311 # printf("# on_or_after(%s, %s): ", join('/', @{$dt1}), join('/', @{$dt2}));
313 foreach my $i (0..(scalar(@{$dt2}) - 1)) {
314 if ($dt1->[$i] > $dt2->[$i]) {
315 # printf("after - pass\n");
316 # $dt1 occurs AFTER $dt2
318 } elsif ($dt1->[$i] < $dt2->[$i]) {
319 # printf("before - fail\n");
320 # $dt1 occurs BEFORE $dt2
323 # both are still equal, keep going
326 # We fell out of the loop with them being equal, so it's 'on'
327 # printf("on - pass\n");
331 sub calendar_increment {
335 my $cal_change = $self->calendar_change;
341 # A calendar change is defined, need to check if it applies
342 if (scalar(@{$new}) == 1) {
343 carp "Can't calculate date change for ", $self->as_string;
347 foreach my $change (@{$cal_change}) {
350 if (length($change) == 2) {
352 } elsif (length($change) == 4) {
353 ($month, $day) = unpack("a2a2", $change);
356 # printf("# calendar_increment('%s', '%s'): change on '%s/%s'\n",
357 # join('/', @{$cur}), join('/', @{$new}),
358 # $month, defined($day) ? $day : 'UNDEF');
360 if ($cur->[0] == $new->[0]) {
361 # Same year, so a 'simple' month/day comparison will be fine
363 ( !on_or_after([$cur->[1], $cur->[2]], [$month, $day])
364 && on_or_after([$new->[1], $new->[2]], [$month, $day]));
366 # @cur is in the year before @new. There are
367 # two possible cases for the calendar change date that
368 # indicate that it's time to change the volume:
369 # (1) the change date is AFTER @cur in the year, or
370 # (2) the change date is BEFORE @new in the year.
372 # -------|------|------X------|------|
373 # @cur (1) Jan 1 (2) @new
376 (on_or_after([$new->[1], $new->[2]], [$month, $day])
377 || !on_or_after([$cur->[1], $cur->[2]], [$month, $day]));
379 return $incr if $incr;
392 my @newend; # only used for combined issues
395 my $reg = $self->{_mfhdc_REGULARITY};
396 my $pattern = $self->{_mfhdc_PATTERN};
397 my $freq = $pattern->{w};
399 foreach my $i (0..$#keys) {
400 $cur[$i] = $next->{$keys[$i]} if exists $next->{$keys[$i]};
403 # If the current issue has a combined date (eg, May/June)
404 # get rid of the first date and base the calculation
405 # on the final date in the combined issue.
406 $cur[-1] =~ s|^[^/]+/||;
408 if (defined $pattern->{y}->{p}) {
409 # There is a $y publication pattern defined in the record:
410 # use it to calculate the next issue date.
412 foreach my $pubpat (@{$pattern->{y}->{p}}, @{$pattern->{y}->{c}}) {
413 my $chroncode = substr($pubpat, 0, 1);
414 my $genfunc = MFHD::Date::generator($chroncode);
415 my @pats = split(/,/, substr($pubpat, 1));
417 next if $chroncode eq 'e';
419 if (!defined $genfunc) {
420 carp "Unrecognized chroncode '$chroncode'";
424 foreach my $pat (@pats) {
425 my $combined = $pat =~ m|/|;
429 # printf("# next_date: generating with pattern '%s'\n", $pat);
432 ($start, $end) = split('/', $pat, 2);
434 ($start, $end) = (undef, undef);
437 @candidate = $genfunc->($start || $pat, @cur);
439 while ($self->is_omitted(@candidate)) {
440 # printf("# pubpat omitting date '%s'\n",
441 # join('/', @candidate));
442 @candidate = $genfunc->($start || $pat, @candidate);
445 # printf("# testing new candidate '%s' against '%s'\n",
446 # join('/', @candidate), join('/', @new));
448 if (!defined($new[0]) || !on_or_after(\@candidate, \@new)) {
449 # first time through the loop
450 # or @candidate is before @new =>
451 # @candidate is the next issue.
454 @newend = $genfunc->($end, @cur);
459 # printf("# selecting candidate date '%s'\n", join('/', @new));
464 if (defined($newend[0])) {
465 # The best match was a combined issue
466 foreach my $i (0..$#new) {
467 # don't combine identical fields
468 next if $new[$i] eq $newend[$i];
469 $new[$i] .= '/' . $newend[$i];
474 if (scalar @new == 0) {
475 # There was no suitable publication pattern defined,
476 # so use the $w frequency to figure out the next date
477 if (!defined($freq)) {
478 carp "Undefined frequency in next_chron!";
479 } elsif (!MFHD::Date::can_increment($freq)) {
480 carp "Don't know how to deal with frequency '$freq'!";
482 # One of the standard defined issue frequencies
483 @new = MFHD::Date::incr_date($freq, @cur);
485 while ($self->is_omitted(@new)) {
486 @new = MFHD::Date::incr_date($freq, @new);
489 if ($self->is_combined(@new)) {
490 my @second_date = MFHD::Date::incr_date($freq, @new);
492 # I am cheating: This code assumes that only the smallest
493 # time increment is combined. So, no "Apr 15/May 1" allowed.
494 $new[-1] = $new[-1] . '/' . $second_date[-1];
499 for my $i (0..$#new) {
500 $next->{$keys[$i]} = $new[$i];
502 # Figure out if we need to adjust volume number
503 # right now just use the $carry that was passed in.
504 # in long run, need to base this on ($carry or date_change)
506 # if $carry is set, the date doesn't matter: we're not
507 # going to increment the v. number twice at year-change.
508 $next->{a} += $carry;
509 } elsif (defined $pattern->{x}) {
510 $next->{a} += $self->calendar_increment(\@cur, \@new);
518 # First handle any "alternative enumeration", since they're
519 # a lot simpler, and don't depend on the the calendar
520 foreach my $key ('h', 'g') {
521 next if !exists $next->{$key};
522 if (!$self->capstr($key)) {
523 warn "Holding data exists for $key, but no caption specified";
528 my $cap = $self->capfield($key);
531 && ($next->{$key} == $cap->{COUNT})) {
540 # Check caption for $ype subfield, specifying that there's a
541 # particular publication pattern for the given level of enumeration
542 # returns the pattern string or undef
547 return undef if !exists $self->{_mfhdc_PATTERN}->{y}->{p};
549 foreach my $reg (@{$self->{_mfhdc_PATTERN}->{y}->{p}}) {
550 if ($reg =~ m/^e$level/o) {
551 return substr($reg, 2);
562 # $carry keeps track of whether we need to carry into the next
563 # higher level of enumeration. It's not actually necessary except
564 # for when the loop ends: if we need to carry from $b into $a
565 # then $carry will be set when the loop ends.
567 # We need to keep track of this because there are two different
568 # reasons why we might increment the highest level of enumeration ($a)
569 # 1) we hit the correct number of items in $b (ie, 5th iss of quarterly)
570 # 2) it's the right time of the year.
573 # If there's a subfield b, then we will go through the loop at
574 # least once. If there's no subfield b, then there's only a single
575 # level of enumeration, so we just add one to it and we're done.
576 if (exists $next->{b}) {
581 foreach my $key (reverse('b'..'f')) {
585 next if !exists $next->{$key};
587 # If the current issue has a combined issue number (eg, 2/3)
588 # get rid of the first issue number and base the calculation
589 # on the final issue number in the combined issue.
590 if ($next->{$key} =~ m|/|) {
591 $next->{$key} =~ s|^[^/]+/||;
594 $level = ord($key) - ord('a') + 1; # enumeration level
596 $pubpat = $self->enum_pubpat($level);
599 # printf("# next_enum: found pubpat '%s' for subfield '%s'\n",
601 my @pats = split(/,/, $pubpat);
603 # If we fall out the bottom of the loop, then $carry
604 # will still be 1, and we will reset the current
605 # level to the first value in @pats and increment
606 # then next higher level.
609 foreach my $pat (@pats) {
610 my $combined = $pat =~ m|/|;
613 # printf("# next_enum: checking current '%s' against pat '%s'\n",
614 # $next->{$key}, $pat);
617 ($pat, $end) = split('/', $pat, 2);
622 if ($pat > $next->{$key}) {
624 $next->{$key} = $pat;
625 $next->{$key} .= '/' . $end if $end;
626 # printf("# next_enum: selecting new issue no. %s\n", $next->{$key});
627 last; # We've found the correct next issue number
631 $next->{$key} = $pats[0];
633 last; # exit the top level loop because we're done
637 # No enumeration publication pattern specified for this level,
638 # just keep adding one.
640 if (!$self->capstr($key)) {
641 # Just assume that it increments continuously and give up
642 warn "Holding data exists for $key, but no caption specified";
648 # printf("# next_enum: no publication pattern, using frequency\n");
650 my $cap = $self->capfield($key);
653 && ($next->{$key} eq $cap->{COUNT})) {
657 # If I don't need to "carry" beyond here, then I just increment
658 # this level of the enumeration and stop looping, since the
659 # "next" hash has been initialized with the current values
665 # You can't have a combined issue that spans two volumes: no.12/1
667 if ($self->enum_is_combined($key, $next->{$key})) {
668 $next->{$key} .= '/' . ($next->{$key} + 1);
675 # The easy part is done. There are two things left to do:
676 # 1) Calculate the date of the next issue, if necessary
677 # 2) Increment the highest level of enumeration (either by date
678 # or because $carry is set because of the above loop
680 if (!$self->subfield('i') || !$next->{i}) {
681 # The simple case: if there is no chronology specified
682 # then just check $carry and return
683 $next->{'a'} += $carry;
685 # Figure out date of next issue, then decide if we need
686 # to adjust top level enumeration based on that
687 $self->next_chron($next, $carry, ('i'..'m'));
696 # If the holding is compressed and not open ended, base next() on the
697 # closing date. If the holding is open-ended, next() is undefined
699 if ($holding->is_compressed) {
700 return undef if $holding->is_open_ended;
701 # TODO: error on next for open-ended holdings?
707 # Initialize $next with current enumeration & chronology, then
708 # we can just operate on $next, based on the contents of the caption
709 foreach my $key ('a'..'m') {
710 my $holding_values = $holding->field_values($key);
711 $next->{$key} = ${$holding_values}[$index] if defined $holding_values;
714 if ($self->enumeration_is_chronology) {
715 $self->next_chron($next, 0, ('a'..'h'));
719 if (exists $next->{'h'}) {
720 $self->next_alt_enum($next);
723 $self->next_enum($next);
728 # return a simple subfields list
733 foreach my $subfield ($self->subfields) {
734 push(@subfields, $subfield->[0], $subfield->[1]);