Net/Z3950/FOLIO/PostProcess/MARC.pm

filter: Statementstmt 100% 74 / 74 Branchbran 96.6% 29 / 30 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 3 totaltotal 97.0% 131 / 135 CC 25 SCAR 19.7
CC
25
Cov
99%
postProcessMARCRecord
12.0
gatherMarcFields
7.0
marcFieldOrSubfield
6.0
CRAP 7.2
« Net/Z3950/FOLIO/Config.pm ↑ Summary Net/Z3950/FOLIO/TransformXML.pm »
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
BranchTrueFalseTotal
unless $cfg 0 16 16
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
l!l
1652
MC/DC100%1/1
$fieldCountByTag{$tag} || 0
$fieldCountByTag{$tag}
Truth table100%2/2
A
A= $fieldCountByTag{$tag}
Aresult
11
00
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) || ''
l!l
172
marcFieldOrSubfield($newMarc, $fieldname, $fieldCount, $tag) or marcFieldOrSubfield($marc, $fieldname, $fieldCount, $tag)
l!l&&r!l&&!r
5122
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)
ABresult
1X1
011
000
30 marcFieldOrSubfield($marc, $fieldname, $fieldCount, $tag) ||
31 '');
32 68 };
33  
34 68 my $newField;
35 68 ▶ if ($field->is_control_field()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->is_control_field) { } 18 50 68
36 18 my $value = $field->data();
37 18 my $rules = $cfg->{$tag};
38 18 ▶ $value = transform($rules, $value, $getFieldFromRecord) if $rules;
Branch: 1 branch
BranchTrueFalseTotal
if $rules 3 15 18
39 18 ▶ $newField = new MARC::Field($tag, $field->indicator(1), $field->indicator(2), $value) if $value ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $value ne "" 17 1 18
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
BranchTrueFalseTotal
if $rules 12 111 123
45 123 ▶ next if $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 9 114 123
46 114 ▶ if (!$newField) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $newField) { } 49 65 114
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
BranchTrueFalseTotal
if $newField 66 2 68
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
BranchTrueFalseTotal
if (not $subtag) { } 3 9 12
79 3 ▶ if (!$register{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($register{$tag}) 2 1 3
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
BranchTrueFalseTotal
if (not $field) { } 2 7 9
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
BranchTrueFalseTotal
unless ($field->subfield($subtag)) 1 6 7
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
BranchTrueFalseTotal
if defined $useIndexIfmatchTag and $tag ne $useIndexIfmatchTag 11 38 49
Condition100%3/3
defined $useIndexIfmatchTag and $tag ne $useIndexIfmatchTag
!ll&&!rl&&r
162211
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
ABresult
0X0
100
111
107 49 my @fields = $marc->field($tag);
108 49 ▶ my $field = $fields[$index || 0];
Condition100%2/2
$index || 0
l!l
1336
MC/DC100%1/1
$index || 0
$index
Truth table100%2/2
A
A= $index
Aresult
11
00
109 49 ▶ return undef if !$field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 14 35 49
110 35 ▶ return $subtag ? $field->subfield($subtag) : $field->data();
Branch: 1 branch
BranchTrueFalseTotal
$subtag ? : 31 4 35
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;
« Net/Z3950/FOLIO/Config.pm ↑ Summary Net/Z3950/FOLIO/TransformXML.pm »