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 # 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 if (exists $next->{$keys[$i]}) {
390 $cur[$i] = $next->{$keys[$i]};
391 # If the current issue has a combined date (eg, May/June)
392 # get rid of the first date and base the calculation
393 # on the final date in the combined issue.
394 $cur[$i] =~ s|^[^/]+/||;
398 if (defined $pattern->{y}->{p}) {
399 # There is a $y publication pattern defined in the record:
400 # use it to calculate the next issue date.
402 foreach my $pubpat (@{$pattern->{y}->{p}}, @{$pattern->{y}->{c}}) {
403 my $chroncode = substr($pubpat, 0, 1);
404 my $genfunc = MFHD::Date::generator($chroncode);
405 my @pats = split(/,/, substr($pubpat, 1));
407 next if $chroncode eq 'e';
409 if (!defined $genfunc) {
410 carp "Unrecognized chroncode '$chroncode'";
414 foreach my $pat (@pats) {
415 my $combined = $pat =~ m|/|;
419 # printf("# next_date: generating with pattern '%s'\n", $pat);
422 ($start, $end) = split('/', $pat, 2);
424 ($start, $end) = (undef, undef);
427 @candidate = $genfunc->($start || $pat, \@cur, $self);
429 while ($self->is_omitted(@candidate)) {
430 # printf("# pubpat omitting date '%s'\n",
431 # join('/', @candidate));
432 @candidate = $genfunc->($start || $pat, \@candidate, $self);
435 # printf("# testing new candidate '%s' against '%s'\n",
436 # join('/', @candidate), join('/', @new));
438 if (!defined($new[0]) || !on_or_after(\@candidate, \@new)) {
439 # first time through the loop
440 # or @candidate is before @new =>
441 # @candidate is the next issue.
444 @newend = $genfunc->($end, \@cur, $self);
449 # printf("# selecting candidate date '%s'\n", join('/', @new));
454 $new[1] = 24 if ($new[1] == 20); # restore fake early winter
456 if (defined($newend[0])) {
457 # The best match was a combined issue
458 foreach my $i (0..$#new) {
459 # don't combine identical fields
460 next if $new[$i] eq $newend[$i];
461 $new[$i] .= '/' . $newend[$i];
466 if (scalar @new == 0) {
467 # There was no suitable publication pattern defined,
468 # so use the $w frequency to figure out the next date
469 if (!defined($freq)) {
470 carp "Undefined frequency in next_chron!";
471 } elsif (!MFHD::Date::can_increment($freq)) {
472 carp "Don't know how to deal with frequency '$freq'!";
474 # One of the standard defined issue frequencies
475 @new = MFHD::Date::incr_date($freq, @cur);
477 while ($self->is_omitted(@new)) {
478 @new = MFHD::Date::incr_date($freq, @new);
481 if ($self->is_combined(@new)) {
482 my @second_date = MFHD::Date::incr_date($freq, @new);
483 foreach my $i (0..$#new) {
484 # don't combine identical fields
485 next if $new[$i] eq $second_date[$i];
486 $new[$i] .= '/' . $second_date[$i];
492 for my $i (0..$#new) {
493 $next->{$keys[$i]} = $new[$i];
496 # Figure out if we need to adjust volume number
498 # If we are incrementing based on date, $carry doesn't
499 # matter: we're not going to increment the v. number twice
501 # It is conceivable that a serial could increment based on date for some
502 # volumes and issue numbering for other volumes, but until a real case
503 # comes up, let's assume that defined calendar changes always trump $u
504 if (defined $pattern->{x}) {
505 my $increment = $self->calendar_increment(\@cur, \@new);
506 # if we hit a calendar change, restart dependant restarters
507 # regardless of whether they thought they should
509 $next->{a} += $increment;
510 foreach my $key ('b'..'f') {
511 next if !exists $next->{$key};
512 my $cap = $self->capfield($key);
513 if ($cap->{RESTART}) {
515 if ($self->enum_is_combined($key, $next->{$key})) {
516 $next->{$key} .= '/' . ($next->{$key} + 1);
519 last; # if we find a non-restarting level, stop
524 $next->{a} += $carry;
528 sub winter_starts_year {
531 my $pubpats = $self->{_mfhdc_PATTERN}->{y}->{p};
532 my $freq = $self->{_mfhdc_PATTERN}->{w};
534 if ($freq =~ /^\d$/) {
535 foreach my $pubpat (@$pubpats) {
536 my $chroncode = substr($pubpat, 0, 1);
537 if ($chroncode eq 's' and substr($pubpat, 1, 2) == 24) {
550 # First handle any "alternative enumeration", since they're
551 # a lot simpler, and don't depend on the the calendar
552 foreach my $key ('h', 'g') {
553 next if !exists $next->{$key};
554 if (!$self->capstr($key)) {
555 warn "Holding data exists for $key, but no caption specified";
560 my $cap = $self->capfield($key);
563 && ($next->{$key} == $cap->{COUNT})) {
572 # Check caption for $ype subfield, specifying that there's a
573 # particular publication pattern for the given level of enumeration
574 # returns the pattern string or undef
579 return undef if !exists $self->{_mfhdc_PATTERN}->{y}->{p};
581 foreach my $reg (@{$self->{_mfhdc_PATTERN}->{y}->{p}}) {
582 if ($reg =~ m/^e$level/o) {
583 return substr($reg, 2);
594 # $carry keeps track of whether we need to carry into the next
595 # higher level of enumeration. It's not actually necessary except
596 # for when the loop ends: if we need to carry from $b into $a
597 # then $carry will be set when the loop ends.
599 # We need to keep track of this because there are two different
600 # reasons why we might increment the highest level of enumeration ($a)
601 # 1) we hit the correct number of items in $b (ie, 5th iss of quarterly)
602 # 2) it's the right time of the year.
605 # If there's a subfield b, then we will go through the loop at
606 # least once. If there's no subfield b, then there's only a single
607 # level of enumeration, so we just add one to it and we're done.
608 if (exists $next->{b}) {
613 foreach my $key (reverse('b'..'f')) {
617 next if !exists $next->{$key};
619 # If the current issue has a combined issue number (eg, 2/3)
620 # get rid of the first issue number and base the calculation
621 # on the final issue number in the combined issue.
622 if ($next->{$key} =~ m|/|) {
623 $next->{$key} =~ s|^[^/]+/||;
626 $level = ord($key) - ord('a') + 1; # enumeration level
628 $pubpat = $self->enum_pubpat($level);
631 # printf("# next_enum: found pubpat '%s' for subfield '%s'\n",
633 my @pats = split(/,/, $pubpat);
635 # If we fall out the bottom of the loop, then $carry
636 # will still be 1, and we will reset the current
637 # level to the first value in @pats and increment
638 # then next higher level.
641 foreach my $pat (@pats) {
642 my $combined = $pat =~ m|/|;
645 # printf("# next_enum: checking current '%s' against pat '%s'\n",
646 # $next->{$key}, $pat);
649 ($pat, $end) = split('/', $pat, 2);
654 if ($pat > $next->{$key}) {
656 $next->{$key} = $pat;
657 $next->{$key} .= '/' . $end if $end;
658 # printf("# next_enum: selecting new issue no. %s\n", $next->{$key});
659 last; # We've found the correct next issue number
663 $next->{$key} = $pats[0];
665 last; # exit the top level loop because we're done
669 # No enumeration publication pattern specified for this level,
670 # just keep adding one.
672 if (!$self->capstr($key)) {
673 # Just assume that it increments continuously and give up
674 warn "Holding data exists for $key, but no caption specified";
680 # printf("# next_enum: no publication pattern, using frequency\n");
682 my $cap = $self->capfield($key);
685 && ($next->{$key} eq $cap->{COUNT})) {
688 } elsif ($cap->{COUNT} > 0 and !($next->{$key} % $cap->{COUNT})) {
689 # If we have a non-restarting enum, but we define a count,
690 # we need to carry to the next level when the current value
691 # divides evenly by the count
692 # XXX: this code naively assumes that there has never been an
693 # issue number anomaly of any kind (like an extra issue), but this
694 # limit is inherent in the standard
698 # If I don't need to "carry" beyond here, then I just increment
699 # this level of the enumeration and stop looping, since the
700 # "next" hash has been initialized with the current values
706 # You can't have a combined issue that spans two volumes: no.12/1
708 if ($self->enum_is_combined($key, $next->{$key})) {
709 $next->{$key} .= '/' . ($next->{$key} + 1);
716 # The easy part is done. There are two things left to do:
717 # 1) Calculate the date of the next issue, if necessary
718 # 2) Increment the highest level of enumeration (either by date
719 # or because $carry is set because of the above loop
721 if (!$self->subfield('i') || !$next->{i}) {
722 # The simple case: if there is no chronology specified
723 # then just check $carry and return
724 $next->{'a'} += $carry;
726 # Figure out date of next issue, then decide if we need
727 # to adjust top level enumeration based on that
728 $self->next_chron($next, $carry, ('i'..'m'));
737 # If the holding is compressed and not open ended, base next() on the
738 # closing date. If the holding is open-ended, next() is undefined
740 if ($holding->is_compressed) {
741 return undef if $holding->is_open_ended;
742 # TODO: error on next for open-ended holdings?
748 # Initialize $next with current enumeration & chronology, then
749 # we can just operate on $next, based on the contents of the caption
750 foreach my $key ('a'..'m') {
751 my $holding_values = $holding->field_values($key);
752 $next->{$key} = ${$holding_values}[$index] if defined $holding_values;
755 if ($self->enumeration_is_chronology) {
756 $self->next_chron($next, 0, ('a'..'h'));
760 if (exists $next->{'h'}) {
761 $self->next_alt_enum($next);
764 $self->next_enum($next);
769 # return a simple subfields list
774 foreach my $subfield ($self->subfields) {
775 push(@subfields, $subfield->[0], $subfield->[1]);