| 1 | package Net::Z3950::FOLIO::PostProcess::MARC; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use MARC::Record; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Net::Z3950::FOLIO::PostProcess::Transform qw(transform); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 16 | ▶ | sub postProcessMARCRecord { | ||||||||||||||||||||||||||
|
Subroutine
postProcessMARCRecord: called CC=12 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 16 | my($cfg, $marc) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 16 | ▶ | return $marc if !$cfg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 16 | my $newMarc = new MARC::Record(); | |||||||||||||||||||||||||||
| 16 | 16 | $newMarc->leader($marc->leader()); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 16 | my @fields = gatherMarcFields($marc, $cfg); | |||||||||||||||||||||||||||
| 19 | 16 | my %fieldCountByTag = (); | |||||||||||||||||||||||||||
| 20 | 16 | foreach my $field (@fields) { | |||||||||||||||||||||||||||
| 21 | 68 | my $tag = $field->tag(); | |||||||||||||||||||||||||||
| 22 | 68 | ▶ | my $fieldCount = $fieldCountByTag{$tag} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$fieldCountByTag{$tag} || 0
MC/DC100%1/1
$fieldCountByTag{$tag} || 0
$fieldCountByTag{$tag}
Truth table100%2/2
A
A= $fieldCountByTag{$tag}
| |||||||||||||||||||||||||||||
| 23 | 68 | $fieldCountByTag{$tag} = $fieldCount+1; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 19 | ▶ | my $getFieldFromRecord = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 26 | 19 | my($fieldname) = @_; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Use the $fieldCount'th instance of field $tag only if substituting from the same field | ||||||||||||||||||||||||||||
| 29 | 19 | ▶ | return (marcFieldOrSubfield($newMarc, $fieldname, $fieldCount, $tag) || | ||||||||||||||||||||||||||
|
Condition100%5/5
marcFieldOrSubfield($newMarc, $fieldname, $fieldCount, $tag) || marcFieldOrSubfield($marc, $fieldname, $fieldCount, $tag) || ''
marcFieldOrSubfield($newMarc, $fieldname, $fieldCount, $tag) or marcFieldOrSubfield($marc, $fieldname, $fieldCount, $tag)
MC/DC100%2/2
marcFieldOrSubfield($newMarc, $fieldname, $fieldCount, $tag) || marcFieldOrSubfield($marc, $fieldname, $fieldCount, $tag) || ''
marcFieldOrSubfield($newMarc, $fieldname, $fieldCount, $tag)
marcFieldOrSubfield($marc, $fieldname, $fieldCount, $tag)
Truth table100%3/3
A or B
A= marcFieldOrSubfield($newMarc, $fieldname, $fieldCount, $tag)B= marcFieldOrSubfield($marc, $fieldname, $fieldCount, $tag)
| |||||||||||||||||||||||||||||
| 30 | marcFieldOrSubfield($marc, $fieldname, $fieldCount, $tag) || | ||||||||||||||||||||||||||||
| 31 | ''); | ||||||||||||||||||||||||||||
| 32 | 68 | }; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 68 | my $newField; | |||||||||||||||||||||||||||
| 35 | 68 | ▶ | if ($field->is_control_field()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 18 | my $value = $field->data(); | |||||||||||||||||||||||||||
| 37 | 18 | my $rules = $cfg->{$tag}; | |||||||||||||||||||||||||||
| 38 | 18 | ▶ | $value = transform($rules, $value, $getFieldFromRecord) if $rules; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 18 | ▶ | $newField = new MARC::Field($tag, $field->indicator(1), $field->indicator(2), $value) if $value ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | } else { | ||||||||||||||||||||||||||||
| 41 | 50 | foreach my $subfield ($field->subfields()) { | |||||||||||||||||||||||||||
| 42 | 123 | my($key, $value) = @$subfield; | |||||||||||||||||||||||||||
| 43 | 123 | my $rules = $cfg->{"$tag\$$key"}; | |||||||||||||||||||||||||||
| 44 | 123 | ▶ | $value = transform($rules, $value, $getFieldFromRecord) if $rules; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 123 | ▶ | next if $value eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 114 | ▶ | if (!$newField) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 49 | $newField = new MARC::Field($tag, $field->indicator(1), $field->indicator(2), $key, $value); | |||||||||||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||||||||||
| 49 | 65 | $newField->add_subfields($key, $value); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 68 | ▶ | $newMarc->append_fields($newField) if $newField; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 16 | return $newMarc; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # For the new record we're creating, we need all the fields (with | ||||||||||||||||||||||||||||
| 62 | # their subfields) from the old record $marc, but also any that are | ||||||||||||||||||||||||||||
| 63 | # created by the rules in $cfg. It's infuriating how we have to go all | ||||||||||||||||||||||||||||
| 64 | # about the houses to make this happen. | ||||||||||||||||||||||||||||
| 65 | # | ||||||||||||||||||||||||||||
| 66 | 16 | ▶ | sub gatherMarcFields { | ||||||||||||||||||||||||||
|
Subroutine
gatherMarcFields: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 16 | my($marc, $cfg) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 16 | my %register; | |||||||||||||||||||||||||||
| 70 | 16 | foreach my $field ($marc->fields()) { | |||||||||||||||||||||||||||
| 71 | 64 | my $tag = $field->tag(); | |||||||||||||||||||||||||||
| 72 | 64 | $register{$tag} = $field; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 16 | my @fields = $marc->fields(); | |||||||||||||||||||||||||||
| 76 | 16 | foreach my $fieldname (sort keys %$cfg) { | |||||||||||||||||||||||||||
| 77 | 12 | my($tag, $subtag) = ($fieldname =~ /(\d+)\$?(.*)/); | |||||||||||||||||||||||||||
| 78 | 12 | ▶ | if (!$subtag) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 3 | ▶ | if (!$register{$tag}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 2 | push @fields, new MARC::Field($tag, ''); | |||||||||||||||||||||||||||
| 81 | #warn "added control-field $tag"; | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||
| 84 | 9 | my $field = $register{$tag}; | |||||||||||||||||||||||||||
| 85 | 9 | ▶ | if (!$field) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 2 | $register{$tag} = new MARC::Field($tag, ' ', ' ', $subtag, ''); | |||||||||||||||||||||||||||
| 87 | 2 | push @fields, $register{$tag}; | |||||||||||||||||||||||||||
| 88 | #warn "added regular field $tag with empty $subtag"; | ||||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||
| 90 | 7 | ▶ | if (!$field->subfield($subtag)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | $field->add_subfields($subtag, ''); | |||||||||||||||||||||||||||
| 92 | #warn "added empty $subtag to regular field $tag"; | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 16 | return @fields; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 49 | ▶ | sub marcFieldOrSubfield { | ||||||||||||||||||||||||||
|
Subroutine
marcFieldOrSubfield: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 49 | my($marc, $fieldname, $index, $useIndexIfmatchTag) = @_; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 49 | my($tag, $subtag) = ($fieldname =~ /(\d+)\$?(.*)/); | |||||||||||||||||||||||||||
| 106 | 49 | ▶ | $index = 0 if defined $useIndexIfmatchTag && $tag ne $useIndexIfmatchTag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $useIndexIfmatchTag and $tag ne $useIndexIfmatchTag
MC/DC100%2/2
defined $useIndexIfmatchTag and $tag ne $useIndexIfmatchTag
defined $useIndexIfmatchTag
$tag ne $useIndexIfmatchTag
Truth table100%3/3
A and B
A= defined $useIndexIfmatchTagB= $tag ne $useIndexIfmatchTag
| |||||||||||||||||||||||||||||
| 107 | 49 | my @fields = $marc->field($tag); | |||||||||||||||||||||||||||
| 108 | 49 | ▶ | my $field = $fields[$index || 0]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$index || 0
MC/DC100%1/1
$index || 0
$index
Truth table100%2/2
A
A= $index
| |||||||||||||||||||||||||||||
| 109 | 49 | ▶ | return undef if !$field; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 35 | ▶ | return $subtag ? $field->subfield($subtag) : $field->data(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 1 | our @EXPORT_OK = qw(postProcessMARCRecord marcFieldOrSubfield); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | 1; | |||||||||||||||||||||||||||