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
47 carp('$u specified for top-level enumeration')
48 unless defined($last_enum);
49 $self->{_mfhdc_ENUMS}->{$last_enum}->{COUNT} = $val;
50 } elsif ($key eq 'v') {
51 carp '$v specified for top-level enumeration'
52 unless defined($last_enum);
53 $self->{_mfhdc_ENUMS}->{$last_enum}->{RESTART} = ($val eq 'r');
54 } elsif ($key =~ /[npwz]/) {
55 # Publication Pattern info ('o' == type of unit, 'q'..'t' undefined)
56 $self->{_mfhdc_PATTERN}->{$key} = $val;
57 } elsif ($key =~ /x/) {
58 # Calendar change can have multiple comma-separated values
59 $self->{_mfhdc_PATTERN}->{x} = [split /,/, $val];
60 } elsif ($key eq 'y') {
61 $self->{_mfhdc_PATTERN}->{y} = {}
62 unless exists $self->{_mfhdc_PATTERN}->{y};
63 update_pattern($self, $val);
64 } elsif ($key eq 'o') {
66 $self->{_mfhdc_UNIT} = $val;
67 } elsif ($key eq 't') {
68 $self->{_mfhdc_COPY} = $val;
70 carp "Unknown caption subfield '$key'";
74 # subsequent levels of enumeration (primary and alternate)
75 # If an enumeration level doesn't document the number
76 # of "issues" per "volume", or whether numbering of issues
77 # restarts, then we can't compress.
78 foreach my $key ('b', 'c', 'd', 'e', 'f', 'h') {
79 if (exists $self->{_mfhdc_ENUMS}->{$key}) {
80 my $pattern = $self->{_mfhdc_ENUMS}->{$key};
81 if ( !$pattern->{RESTART}
83 || ($pattern->{COUNT} eq 'var')
84 || ($pattern->{COUNT} eq 'und')) {
85 $self->{_mfhdc_COMPRESSIBLE} = 0;
91 my $pat = $self->{_mfhdc_PATTERN};
93 # Sanity check publication frequency vs publication pattern:
94 # if the frequency is a number, then the pattern better
95 # have that number of values associated with it.
96 if ( exists($pat->{w})
97 && ($pat->{w} =~ /^\d+$/)
98 && ($pat->{w} != scalar(@{$pat->{y}->{p}}))) {
100 "Caption::new: publication frequency '$pat->{w}' != publication pattern @{$pat->{y}->{p}}"
104 # If there's a $x subfield and a $j, then it's compressible
105 if (exists $pat->{x} && exists $self->{_mfhdc_CHRONS}->{'j'}) {
106 $self->{_mfhdc_COMPRESSIBLE} = 1;
109 bless($self, $class);
117 my $pathash = $self->{_mfhdc_PATTERN}->{y};
118 my ($pubcode, $pat) = unpack("a1a*", $val);
120 $pathash->{$pubcode} = [] unless exists $pathash->{$pubcode};
121 push @{$pathash->{$pubcode}}, $pat;
126 my $pattern = $self->{_mfhdc_PATTERN}->{y};
134 return $self->{_mfhdc_COMPRESSIBLE};
141 if (exists $self->{_mfhdc_CHRONS}->{$key}) {
142 return $self->{_mfhdc_CHRONS}->{$key};
152 if (exists $self->{_mfhdc_ENUMS}->{$key}) {
153 return $self->{_mfhdc_ENUMS}->{$key};
154 } elsif (exists $self->{_mfhdc_CHRONS}->{$key}) {
155 return $self->{_mfhdc_CHRONS}->{$key};
164 my $val = $self->capfield($key);
167 return $val->{CAPTION};
176 return $self->{_mfhdc_UNIT};
182 return $self->{_mfhdc_LINK_ID};
185 sub calendar_change {
188 return $self->{_mfhdc_PATTERN}->{x};
191 # If items are identified by chronology only, with no separate
192 # enumeration (eg, a newspaper issue), then the chronology is
193 # recorded in the enumeration subfields $a - $f. We can tell
194 # that this is the case if there are $a - $f subfields and no
195 # chronology subfields ($i-$k), and none of the $a-$f subfields
196 # have associated $u or $v subfields, but there's a $w and no $x
198 sub enumeration_is_chronology {
201 # There is always a '$a' subfield in well-formed fields.
203 if exists $self->{_mfhdc_CHRONS}->{i}
204 || exists $self->{_mfhdc_PATTERN}->{x};
206 foreach my $key ('a'..'f') {
209 last if !exists $self->{_mfhdc_ENUMS}->{$key};
211 $enum = $self->{_mfhdc_ENUMS}->{$key};
212 return 0 if defined $enum->{COUNT} || defined $enum->{RESTART};
215 return (exists $self->{_mfhdc_PATTERN}->{w});
218 sub regularity_match {
223 # we can't match something that doesn't exist.
224 return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{$pubcode};
226 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{$pubcode}}) {
227 my $chroncode = substr($regularity, 0, 1);
228 my $matchfunc = MFHD::Date::dispatch($chroncode);
229 my @pats = split(/,/, substr($regularity, 1));
231 if (!defined $matchfunc) {
232 carp "Unrecognized chroncode '$chroncode'";
237 foreach my $pat (@pats) {
238 $pat =~ s|/.+||; # If it's a combined date, match the start
239 if ($matchfunc->($pat, @date)) {
252 # printf("# is_omitted: testing date %s: %d\n", join('/', @date),
253 # $self->regularity_match('o', @date));
254 return $self->regularity_match('o', @date);
261 return $self->regularity_match('p', @date);
268 return $self->regularity_match('c', @date);
271 sub enum_is_combined {
273 my $subfield = shift;
275 my $level = ord($subfield) - ord('a') + 1;
277 return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{c};
279 foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{c}}) {
280 next unless $regularity =~ m/^e$level/o;
282 my @pats = split(/,/, substr($regularity, 2));
284 foreach my $pat (@pats) {
285 $pat =~ s|/.+||; # if it's a combined issue, match the start
286 return 1 if ($iss eq $pat);
293 # Test to see if $dt1 is on or after $dt2
294 # if length(@{$dt2} == 2, then just month/day are compared
295 # if length(@{$dt2} == 1, then just the months are compared
300 # printf("# on_or_after(%s, %s): ", join('/', @{$dt1}), join('/', @{$dt2}));
302 foreach my $i (0..(scalar(@{$dt2}) - 1)) {
303 if ($dt1->[$i] > $dt2->[$i]) {
304 # printf("after - pass\n");
305 # $dt1 occurs AFTER $dt2
307 } elsif ($dt1->[$i] < $dt2->[$i]) {
308 # printf("before - fail\n");
309 # $dt1 occurs BEFORE $dt2
312 # both are still equal, keep going
315 # We fell out of the loop with them being equal, so it's 'on'
316 # printf("on - pass\n");
320 sub calendar_increment {
324 my $cal_change = $self->calendar_change;
330 # A calendar change is defined, need to check if it applies
331 if (scalar(@{$new}) == 1) {
332 carp "Can't calculate date change for ", $self->as_string;
336 foreach my $change (@{$cal_change}) {
339 if (length($change) == 2) {
341 } elsif (length($change) == 4) {
342 ($month, $day) = unpack("a2a2", $change);
345 # printf("# calendar_increment('%s', '%s'): change on '%s/%s'\n",
346 # join('/', @{$cur}), join('/', @{$new}),
347 # $month, defined($day) ? $day : 'UNDEF');
349 if ($cur->[0] == $new->[0]) {
350 # Same year, so a 'simple' month/day comparison will be fine
352 ( !on_or_after([$cur->[1], $cur->[2]], [$month, $day])
353 && on_or_after([$new->[1], $new->[2]], [$month, $day]));
355 # @cur is in the year before @new. There are
356 # two possible cases for the calendar change date that
357 # indicate that it's time to change the volume:
358 # (1) the change date is AFTER @cur in the year, or
359 # (2) the change date is BEFORE @new in the year.
361 # -------|------|------X------|------|
362 # @cur (1) Jan 1 (2) @new
365 (on_or_after([$new->[1], $new->[2]], [$month, $day])
366 || !on_or_after([$cur->[1], $cur->[2]], [$month, $day]));
368 return $incr if $incr;
381 my @newend; # only used for combined issues
384 my $reg = $self->{_mfhdc_REGULARITY};
385 my $pattern = $self->{_mfhdc_PATTERN};
386 my $freq = $pattern->{w};
388 foreach my $i (0..$#keys) {
389 $cur[$i] = $next->{$keys[$i]} if exists $next->{$keys[$i]};
392 # If the current issue has a combined date (eg, May/June)
393 # get rid of the first date and base the calculation
394 # on the final date in the combined issue.
395 $cur[-1] =~ s|^[^/]+/||;
397 if (defined $pattern->{y}->{p}) {
398 # There is a $y publication pattern defined in the record:
399 # use it to calculate the next issue date.
401 foreach my $pubpat (@{$pattern->{y}->{p}}, @{$pattern->{y}->{c}}) {
402 my $chroncode = substr($pubpat, 0, 1);
403 my $genfunc = MFHD::Date::generator($chroncode);
404 my @pats = split(/,/, substr($pubpat, 1));
406 next if $chroncode eq 'e';
408 if (!defined $genfunc) {
409 carp "Unrecognized chroncode '$chroncode'";
413 foreach my $pat (@pats) {
414 my $combined = $pat =~ m|/|;
418 # printf("# next_date: generating with pattern '%s'\n", $pat);
421 ($start, $end) = split('/', $pat, 2);
423 ($start, $end) = (undef, undef);
426 @candidate = $genfunc->($start || $pat, @cur);
428 while ($self->is_omitted(@candidate)) {
429 # printf("# pubpat omitting date '%s'\n",
430 # join('/', @candidate));
431 @candidate = $genfunc->($start || $pat, @candidate);
434 # printf("# testing new candidate '%s' against '%s'\n",
435 # join('/', @candidate), join('/', @new));
437 if (!defined($new[0]) || !on_or_after(\@candidate, \@new)) {
438 # first time through the loop
439 # or @candidate is before @new =>
440 # @candidate is the next issue.
443 @newend = $genfunc->($end, @cur);
448 # printf("# selecting candidate date '%s'\n", join('/', @new));
453 if (defined($newend[0])) {
454 # The best match was a combined issue
455 foreach my $i (0..$#new) {
456 # don't combine identical fields
457 next if $new[$i] eq $newend[$i];
458 $new[$i] .= '/' . $newend[$i];
463 if (scalar @new == 0) {
464 # There was no suitable publication pattern defined,
465 # so use the $w frequency to figure out the next date
466 if (!defined($freq)) {
467 carp "Undefined frequency in next_chron!";
468 } elsif (!MFHD::Date::can_increment($freq)) {
469 carp "Don't know how to deal with frequency '$freq'!";
471 # One of the standard defined issue frequencies
472 @new = MFHD::Date::incr_date($freq, @cur);
474 while ($self->is_omitted(@new)) {
475 @new = MFHD::Date::incr_date($freq, @new);
478 if ($self->is_combined(@new)) {
479 my @second_date = MFHD::Date::incr_date($freq, @new);
481 # I am cheating: This code assumes that only the smallest
482 # time increment is combined. So, no "Apr 15/May 1" allowed.
483 $new[-1] = $new[-1] . '/' . $second_date[-1];
488 for my $i (0..$#new) {
489 $next->{$keys[$i]} = $new[$i];
491 # Figure out if we need to adjust volume number
492 # right now just use the $carry that was passed in.
493 # in long run, need to base this on ($carry or date_change)
495 # if $carry is set, the date doesn't matter: we're not
496 # going to increment the v. number twice at year-change.
497 $next->{a} += $carry;
498 } elsif (defined $pattern->{x}) {
499 $next->{a} += $self->calendar_increment(\@cur, \@new);
507 # First handle any "alternative enumeration", since they're
508 # a lot simpler, and don't depend on the the calendar
509 foreach my $key ('h', 'g') {
510 next if !exists $next->{$key};
511 if (!$self->capstr($key)) {
512 warn "Holding data exists for $key, but no caption specified";
517 my $cap = $self->capfield($key);
520 && ($next->{$key} == $cap->{COUNT})) {
529 # Check caption for $ype subfield, specifying that there's a
530 # particular publication pattern for the given level of enumeration
531 # returns the pattern string or undef
536 return undef if !exists $self->{_mfhdc_PATTERN}->{y}->{p};
538 foreach my $reg (@{$self->{_mfhdc_PATTERN}->{y}->{p}}) {
539 if ($reg =~ m/^e$level/o) {
540 return substr($reg, 2);
551 # $carry keeps track of whether we need to carry into the next
552 # higher level of enumeration. It's not actually necessary except
553 # for when the loop ends: if we need to carry from $b into $a
554 # then $carry will be set when the loop ends.
556 # We need to keep track of this because there are two different
557 # reasons why we might increment the highest level of enumeration ($a)
558 # 1) we hit the correct number of items in $b (ie, 5th iss of quarterly)
559 # 2) it's the right time of the year.
562 # If there's a subfield b, then we will go through the loop at
563 # least once. If there's no subfield b, then there's only a single
564 # level of enumeration, so we just add one to it and we're done.
565 if (exists $next->{b}) {
570 foreach my $key (reverse('b'..'f')) {
574 next if !exists $next->{$key};
576 # If the current issue has a combined issue number (eg, 2/3)
577 # get rid of the first issue number and base the calculation
578 # on the final issue number in the combined issue.
579 if ($next->{$key} =~ m|/|) {
580 $next->{$key} =~ s|^[^/]+/||;
583 $level = ord($key) - ord('a') + 1; # enumeration level
585 $pubpat = $self->enum_pubpat($level);
588 # printf("# next_enum: found pubpat '%s' for subfield '%s'\n",
590 my @pats = split(/,/, $pubpat);
592 # If we fall out the bottom of the loop, then $carry
593 # will still be 1, and we will reset the current
594 # level to the first value in @pats and increment
595 # then next higher level.
598 foreach my $pat (@pats) {
599 my $combined = $pat =~ m|/|;
602 # printf("# next_enum: checking current '%s' against pat '%s'\n",
603 # $next->{$key}, $pat);
606 ($pat, $end) = split('/', $pat, 2);
611 if ($pat > $next->{$key}) {
613 $next->{$key} = $pat;
614 $next->{$key} .= '/' . $end if $end;
615 # printf("# next_enum: selecting new issue no. %s\n", $next->{$key});
616 last; # We've found the correct next issue number
620 $next->{$key} = $pats[0];
622 last; # exit the top level loop because we're done
626 # No enumeration publication pattern specified for this level,
627 # just keep adding one.
629 if (!$self->capstr($key)) {
630 # Just assume that it increments continuously and give up
631 warn "Holding data exists for $key, but no caption specified";
637 # printf("# next_enum: no publication pattern, using frequency\n");
639 my $cap = $self->capfield($key);
642 && ($next->{$key} eq $cap->{COUNT})) {
646 # If I don't need to "carry" beyond here, then I just increment
647 # this level of the enumeration and stop looping, since the
648 # "next" hash has been initialized with the current values
654 # You can't have a combined issue that spans two volumes: no.12/1
656 if ($self->enum_is_combined($key, $next->{$key})) {
657 $next->{$key} .= '/' . ($next->{$key} + 1);
664 # The easy part is done. There are two things left to do:
665 # 1) Calculate the date of the next issue, if necessary
666 # 2) Increment the highest level of enumeration (either by date
667 # or because $carry is set because of the above loop
669 if (!$self->subfield('i')) {
670 # The simple case: if there is no chronology specified
671 # then just check $carry and return
672 $next->{'a'} += $carry;
674 # Figure out date of next issue, then decide if we need
675 # to adjust top level enumeration based on that
676 $self->next_chron($next, $carry, ('i'..'m'));
685 # If the holding is compressed and not open ended, base next() on the
686 # closing date. If the holding is open-ended, next() is undefined
688 if ($holding->is_compressed) {
689 return undef if $holding->is_open_ended;
690 # TODO: error on next for open-ended holdings?
696 # Initialize $next with current enumeration & chronology, then
697 # we can just operate on $next, based on the contents of the caption
698 foreach my $key ('a'..'m') {
699 my $holding_values = $holding->field_values($key);
700 $next->{$key} = ${$holding_values}[$index] if defined $holding_values;
703 if ($self->enumeration_is_chronology) {
704 $self->next_chron($next, 0, ('a'..'h'));
708 if (exists $next->{'h'}) {
709 $self->next_alt_enum($next);
712 $self->next_enum($next);