8 use Equinox::Migration::SubfieldMapper;
18 my $total = `grep -c '<record' $marcxml`;
22 my $yaml = YAML::Tiny->new;
23 my $t = XML::Twig->new( twig_handlers => { record => \&record } );
24 $t->parsefile($marcxml);
31 my @dfields = $r->children('datafield');
32 for my $d (@dfields) {
33 process_datafields($d);
39 $percent = int(($count / $total) * 100);
40 print "\r$percent% done ($count)";# if ($percent != $prevper);
44 sub process_datafields {
46 my $tag = $d->{'att'}->{'tag'};
49 my $s = $d->first_child('subfield');
50 $holdings->{id} = $s->text;;
51 } elsif ($c->{map}->has($tag)) {
52 push @{$holdings->{copies}}, { tag => $tag };
53 my @subs = $d->children('subfield');
55 { process_subs($tag,$sub) }
61 my $code = $sub->{'att'}->{'code'};
63 unless ($c->{map}->has($tag, $code)) {
64 # this is a subfield code we don't have mapped. report on it if this is a sample tag
65 push @{$c->{sample}{$tag}}, $code if defined $c->{sample}{tag};
69 my $copy = $holdings->{copies}[-1];
70 my $field = $c->{map}->field($tag, $code);
71 if ($c->{map}->mod($field) eq 'multi') {
72 my $name = $tag . $code;
73 push @{$copy->{multi}{$name}}, $sub->text;
75 $copy->{uni}{$code} = $sub->text;
80 #------------------------------------------------
85 for my $copy (@{$holdings->{copies}}) {
86 print HOLDINGS $holdings->{id}, "\t$i\t", $copy->{tag};
87 for ( sort keys %{ $c->{map}{fields} } ) {
88 if (defined $copy->{uni}{$_->{sub}}) {
89 print HOLDINGS "\t", $copy->{uni}{$_->{sub}};
96 for my $m (sort keys %{$copy->{multi}}) {
97 my $fh = $c->{files}{multi}{$m};
98 print $fh join("\t", $holdings->{id}, $i, @{$copy->{multi}{$m}}), "\n";
104 sub write_sample_fields {
107 #------------------------------------------------
114 # set mode on existing filehandles
115 binmode(STDIN, ':utf8');
117 my $rc = GetOptions( $c,
124 show_help() unless $rc;
125 show_help() if ($c->{help});
127 my @keys = keys %{$c};
128 show_help() unless (@ARGV and @keys);
129 for my $key ('prefix', 'map', 'ils')
130 { push @missing, $key unless $c->{$key} }
132 print "Required option: ", join(', ', @missing), " missing!\n";
136 # generate subfield map
137 $c->{map} = Equinox::Migration::SubfieldMapper->new( file => $c->{map} );
139 # explode sample tags string
140 if (defined $c->{sample}) {
141 my $sample = $c->{sample};
143 for (split /,/, $c->{sample}) {
144 $c->{sample}{$_} = [];
148 # open required files
149 open HOLDINGS, '>', ($c->{prefix} . ".holdings.pg");
150 for my $f (keys %{$c->{map}{fields}}) {
151 if ($c->{map}->mod($f)) {
152 open my $mfh, '>', join('.', $c->{prefix}, "holdings",
153 $c->{map}{fields}{$f}{tag},
154 $c->{map}{fields}{$f}{sub}, "pg");
155 $c->{files}{multi}{ ($c->{map}{fields}{$f}{tag} . $c->{map}{fields}{$f}{sub}) }
161 print HOLDINGS "BEGIN;\n";
162 print HOLDINGS "CREATE TABLE ", $c->{prefix}, ".asset_copy_", $c->{ils};
163 print HOLDINGS $c->{library} if (defined $c->{library});
165 for ( sort keys %{ $c->{map}{fields} } ) {
166 print HOLDINGS "l_", $_, " TEXT, ";
168 print HOLDINGS ") INHERITS FROM (", $c->{prefix}, ".asset_copy);\n";
169 print HOLDINGS "COPY ", $c->{prefix}, ".asset_copy_", $c->{ils};
170 print HOLDINGS $c->{library} if (defined $c->{library});
172 for ( sort keys %{ $c->{map}{fields} } ) {
173 print HOLDINGS "l_", $_, ", ";
175 print HOLDINGS ") FROM STDIN;\n";