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 next if ($c->{map}->mod($_) =~ /^bib/);
89 if (defined $copy->{uni}{ $c->{map}{fields}{$_}{sub} }) {
90 print HOLDINGS "\t", $copy->{uni}{ $c->{map}{fields}{$_}{sub} };
97 for my $m (sort keys %{$copy->{multi}}) {
98 my $fh = $c->{files}{multi}{$m};
99 print $fh join("\t", $holdings->{id}, $i, @{$copy->{multi}{$m}}), "\n";
105 sub write_sample_fields {
108 #------------------------------------------------
115 # set mode on existing filehandles
116 binmode(STDIN, ':utf8');
118 my $rc = GetOptions( $c,
126 show_help() unless $rc;
127 show_help() if ($c->{help});
129 my @keys = keys %{$c};
130 show_help() unless (@ARGV and @keys);
131 for my $key ('prefix', 'map', 'ils')
132 { push @missing, $key unless $c->{$key} }
134 print "Required option: ", join(', ', @missing), " missing!\n";
138 # generate subfield map
139 $c->{map} = Equinox::Migration::SubfieldMapper->new( file => $c->{map} );
141 # explode sample tags string
142 if (defined $c->{sample}) {
143 my $sample = $c->{sample};
145 for (split /,/, $c->{sample}) {
146 $c->{sample}{$_} = [];
150 # open required files
151 open HOLDINGS, '>', ($c->{prefix} . ".holdings.pg");
152 for my $f (keys %{$c->{map}{fields}}) {
153 if ($c->{map}->mod($f)) {
154 next if ($c->{map}->mod($f) =~ /bib/);
155 open my $mfh, '>', join('.', $c->{prefix}, "holdings",
156 $c->{map}{fields}{$f}{tag},
157 $c->{map}{fields}{$f}{sub}, "pg");
158 $c->{files}{multi}{ ($c->{map}{fields}{$f}{tag} . $c->{map}{fields}{$f}{sub}) }
164 print HOLDINGS "BEGIN;\n";
165 print HOLDINGS "CREATE TABLE ", $c->{prefix}, ".asset_copy_", $c->{ils};
166 print HOLDINGS $c->{library} if (defined $c->{library});
167 print HOLDINGS " (eg_bib_id INTEGER, eg_copy_id INTEGER, l_tag INTEGER";
168 for ( sort keys %{ $c->{map}{fields} } ) {
169 next if ($c->{map}->mod($_) =~ /bib/);
170 print HOLDINGS ", l_", $_, " TEXT";
172 print HOLDINGS ") INHERITS (", $c->{prefix}, ".asset_copy);\n";
173 print HOLDINGS "COPY ", $c->{prefix}, ".asset_copy_", $c->{ils};
174 print HOLDINGS $c->{library} if (defined $c->{library});
175 print HOLDINGS " (eg_bib_id, eg_copy_id, l_tag";
176 for ( sort keys %{ $c->{map}{fields} } ) {
177 print HOLDINGS ", l_", $_;
179 print HOLDINGS ") FROM STDIN;\n";