| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Catmandu::Fix::marc_copy; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
305751
|
use Catmandu::Sane; |
|
|
1
|
|
|
|
|
9
|
|
|
|
1
|
|
|
|
|
6
|
|
|
4
|
1
|
|
|
1
|
|
658
|
use Catmandu::MARC; |
|
|
1
|
|
|
|
|
5
|
|
|
|
1
|
|
|
|
|
54
|
|
|
5
|
1
|
|
|
1
|
|
11
|
use Moo; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
6
|
|
|
6
|
1
|
|
|
1
|
|
995
|
use Catmandu::Fix::Has; |
|
|
1
|
|
|
|
|
1211
|
|
|
|
1
|
|
|
|
|
10
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
with 'Catmandu::Fix::Base'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '1.13'; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has marc_path => (fix_arg => 1); |
|
13
|
|
|
|
|
|
|
has path => (fix_arg => 1); |
|
14
|
|
|
|
|
|
|
has equals => (fix_opt => 1); |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub emit { |
|
17
|
0
|
|
|
0
|
0
|
|
my ($self,$fixer) = @_; |
|
18
|
0
|
|
|
|
|
|
my $path = $fixer->split_path($self->path); |
|
19
|
0
|
|
|
|
|
|
my $key = $path->[-1]; |
|
20
|
0
|
|
|
|
|
|
my $marc_obj = Catmandu::MARC->instance; |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Precompile the marc_path to gain some speed |
|
23
|
0
|
|
|
|
|
|
my $marc_context = $marc_obj->compile_marc_path($self->marc_path, subfield_wildcard => 0); |
|
24
|
0
|
|
|
|
|
|
my $marc = $fixer->capture($marc_obj); |
|
25
|
0
|
|
|
|
|
|
my $marc_path = $fixer->capture($marc_context); |
|
26
|
0
|
|
|
|
|
|
my $equals = $fixer->capture($self->equals); |
|
27
|
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
my $var = $fixer->var; |
|
29
|
0
|
|
|
|
|
|
my $result = $fixer->generate_var; |
|
30
|
0
|
|
|
|
|
|
my $current_value = $fixer->generate_var; |
|
31
|
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
my $perl = ""; |
|
33
|
0
|
|
|
|
|
|
$perl .= $fixer->emit_declare_vars($current_value, "[]"); |
|
34
|
0
|
|
|
|
|
|
$perl .=<<EOF; |
|
35
|
|
|
|
|
|
|
if (my ${result} = ${marc}->marc_copy( |
|
36
|
|
|
|
|
|
|
${var}, |
|
37
|
|
|
|
|
|
|
${marc_path}, |
|
38
|
|
|
|
|
|
|
${equals}) ) { |
|
39
|
|
|
|
|
|
|
${result} = ref(${result}) ? ${result} : [${result}]; |
|
40
|
|
|
|
|
|
|
for ${current_value} (\@{${result}}) { |
|
41
|
|
|
|
|
|
|
EOF |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$perl .= $fixer->emit_create_path( |
|
44
|
|
|
|
|
|
|
$var, |
|
45
|
|
|
|
|
|
|
$path, |
|
46
|
|
|
|
|
|
|
sub { |
|
47
|
0
|
|
|
0
|
|
|
my $var2 = shift; |
|
48
|
0
|
|
|
|
|
|
"${var2} = ${current_value}" |
|
49
|
|
|
|
|
|
|
} |
|
50
|
0
|
|
|
|
|
|
); |
|
51
|
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
$perl .=<<EOF; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
EOF |
|
56
|
0
|
|
|
|
|
|
$perl; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
1; |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
__END__ |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 NAME |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Catmandu::Fix::marc_copy - copy marc data in a structured way to a new field |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# fixed field |
|
70
|
|
|
|
|
|
|
marc_copy(001, fixed001) |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Can result in: |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
fixed001 : [ |
|
75
|
|
|
|
|
|
|
{ |
|
76
|
|
|
|
|
|
|
"tag": "001", |
|
77
|
|
|
|
|
|
|
"ind1": null, |
|
78
|
|
|
|
|
|
|
"ind2": null, |
|
79
|
|
|
|
|
|
|
"content": "fol05882032 " |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
] |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
And |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# variable field |
|
86
|
|
|
|
|
|
|
marc_copy(650, subjects) |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Can result in: |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
subjects:[ |
|
91
|
|
|
|
|
|
|
{ |
|
92
|
|
|
|
|
|
|
"subfields" : [ |
|
93
|
|
|
|
|
|
|
{ |
|
94
|
|
|
|
|
|
|
"a" : "Perl (Computer program language)" |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
], |
|
97
|
|
|
|
|
|
|
"ind1" : " ", |
|
98
|
|
|
|
|
|
|
"ind2" : "0", |
|
99
|
|
|
|
|
|
|
"tag" : "650" |
|
100
|
|
|
|
|
|
|
}, |
|
101
|
|
|
|
|
|
|
{ |
|
102
|
|
|
|
|
|
|
"ind1" : " ", |
|
103
|
|
|
|
|
|
|
"subfields" : [ |
|
104
|
|
|
|
|
|
|
{ |
|
105
|
|
|
|
|
|
|
"a" : "Web servers." |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
], |
|
108
|
|
|
|
|
|
|
"tag" : "650", |
|
109
|
|
|
|
|
|
|
"ind2" : "0" |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
] |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Copy MARC data referred by MARC_TAG in a structured way to JSON path. |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
In contrast to L<Catmandu::Fix::marc_map> and L<Catmandu::Fix::marc_spec> |
|
119
|
|
|
|
|
|
|
marc_copy will not only copy data content (values) but also all data elements |
|
120
|
|
|
|
|
|
|
like tag, indicators and subfield codes into a nested data structure. |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 METHODS |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 marc_copy(MARC_PATH, JSON_PATH, [equals: REGEX]) |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Copy this MARC fields referred by a MARC_PATH to a JSON_PATH. |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
When the MARC_PATH points to a MARC tag then only the fields mathching the MARC |
|
129
|
|
|
|
|
|
|
tag will be copied. When the MATCH_PATH contains indicators or subfields, then |
|
130
|
|
|
|
|
|
|
only the MARC_FIELDS which contain data in these subfields will be copied. Optional, |
|
131
|
|
|
|
|
|
|
a C<equals> regular expression can be provided that should match the subfields that |
|
132
|
|
|
|
|
|
|
need to be copied: |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# Copy all the 300 fields |
|
135
|
|
|
|
|
|
|
marc_copy(300,tmp) |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# Copy all the 300 fields with indicator 1 = 1 |
|
138
|
|
|
|
|
|
|
marc_copy(300[1],tmp) |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# Copy all the 300 fields which have subfield c |
|
141
|
|
|
|
|
|
|
marc_copy(300c,tmp) |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# Copy all the 300 fields which have subfield c equal to 'ABC' |
|
144
|
|
|
|
|
|
|
marc_copy(300c,tmp,equal:"^ABC") |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 JSON PATHS |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Catmandu Fixes can be used to edit the data in the copied fields. To have easy access |
|
149
|
|
|
|
|
|
|
to the data in the copied fields, these JSON paths can be used (where VAR is the |
|
150
|
|
|
|
|
|
|
name of field into which you copied the data) |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
VAR.*.tag - The names of all MARC tags |
|
153
|
|
|
|
|
|
|
VAR.*.ind1 - All first indicators |
|
154
|
|
|
|
|
|
|
VAR.*.ind2 - All second indicators |
|
155
|
|
|
|
|
|
|
VAR.*.subfields.*.a - The value of all $a subfields |
|
156
|
|
|
|
|
|
|
VAR.*.subfields.$first.a - The value of the first $a subfield |
|
157
|
|
|
|
|
|
|
VAR.*.subfields.$last.a - The value of the last $a subfield |
|
158
|
|
|
|
|
|
|
VAR.*.content - The value of the first control field |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
VAR.$first.subfields.$first.z - The value of the second $z subfield in the first MARC field |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
These JSON paths can be used like: |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# Set the first indicator of all 300 fields |
|
165
|
|
|
|
|
|
|
do marc_each() |
|
166
|
|
|
|
|
|
|
if marc_has(300) |
|
167
|
|
|
|
|
|
|
marc_copy(300,tmp) |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# Set the first indicator to 1 |
|
170
|
|
|
|
|
|
|
set_field(tmp.*.ind1,1) |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
marc_paste(tmp) |
|
173
|
|
|
|
|
|
|
end |
|
174
|
|
|
|
|
|
|
end |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 INLINE |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
This Fix can be used inline in a Perl script: |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
use Catmandu::Fix::marc_copy as => 'marc_copy'; |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
my $data = { record => ['650', ' ', 0, 'a', 'Perl'] }; |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
$data = marc_copy($data,'650','subject'); |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
print $data->{subject}->[0]->{tag} , "\n"; # '650' |
|
187
|
|
|
|
|
|
|
print $data->{subject}->[0]->{ind1} , "\n"; # ' ' |
|
188
|
|
|
|
|
|
|
print $data->{subject}->[0]->{ind2} , "\n"; # 0 |
|
189
|
|
|
|
|
|
|
print $data->{subject}->[0]->{subfields}->[0]->{a} , "\n"; # 'Perl' |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=over |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=item * L<Catmandu::Fix::marc_cut> |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=item * L<Catmandu::Fix::marc_paste> |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=back |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
204
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
|
205
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=cut |