10 my $conf = initialize();
13 open HOLDINGS, '>', $conf->{output};
14 open X, '>', $conf->{pubnotesfile};
15 open Z, '>', $conf->{privnotesfile};
17 my %sample = ( x => {}, z => {} ); # hash of all subfields in all 852s
23 my $total = `grep -c '<record' $marcxml`;
27 my $yaml = YAML::Tiny->new;
28 my $t = XML::Twig->new( twig_handlers => { record => \&record } );
29 $t->parsefile($marcxml);
30 $yaml->[0] = \%sample;
31 $yaml->write('holdings.sample');
36 $holdings = { copies => [] };
38 my @dfields = $r->children('datafield');
40 { process_datafields($d) }
42 for my $copy (@{$holdings->{copies}})
43 { print_reports($copy) }
47 $percent = int(($count / $total) * 100);
48 print "\r$percent% done ($count)" if ($percent != $prevper);
52 sub process_datafields {
55 if ($d->{'att'}->{'tag'} == 903) {
56 my $s = $d->first_child('subfield');
57 $holdings->{id} = $s->text;;
61 if ($d->{'att'}->{'tag'} == $conf->{tag}) {
62 push @{$holdings->{copies}}, { x =>[], z => [] };
63 $holdings->{copies}[-1]{copyid} = $copyid;
64 my @subs = $d->children('subfield');
73 my $copy = $holdings->{copies}[-1];
75 my $code = $s->{'att'}->{'code'};
78 if ($code eq $conf->{pubnotes} or $code eq $conf->{privnotes}) {
79 push @{$copy->{$code}}, $value;
80 my ($k,$v) = split /:/, $value;
81 $sample{$code}{$k} = $v;
83 $copy->{$code} = $value;
84 $sample{$code} = $value;
89 return unless defined $holdings->{id};
93 print X join("\t", $holdings->{id}, $copy->{copyid}, $note, $_), "\n";
98 print Z join("\t", $holdings->{id}, $copy->{copyid}, $note, $_), "\n";
102 for ( @{$conf->{fields}} )
103 { $copy->{$_} = '' unless defined $copy->{$_}; push @fields, $copy->{$_} }
104 print HOLDINGS join("\t", $holdings->{id}, $copy->{copyid}, @fields), "\n";
108 #------------------------------------------------
115 # set mode on existing filehandles
116 binmode(STDIN, ':utf8');
118 my $rc = GetOptions( $c,
129 show_help() unless $rc;
130 show_help() if ($c->{help});
133 $c->{prefix} = (defined $c->{prefix}) ? ($c->{prefix} . '.') : '';
134 $c->{tag} = $c->{tag} || '852';
136 $c->{output} || join('', $c->{prefix}, "holdings.pg");
137 $c->{pubnotes} = $c->{pubnotes} || 'x';
139 $c->{pubnotesfile} || join('', $c->{prefix}, "holdings.pubnote.pg");
140 $c->{privnotes} = $c->{privnotes} || 'z';
141 $c->{privnotesfile} =
142 $c->{privnotesfile} || join('', $c->{prefix}, "holdings.privnote.pg");
144 my @keys = keys %{$c};
145 show_help() unless (@ARGV and @keys);
146 for my $key ('fields', 'tag')
147 { push @missing, $key unless $c->{$key} }
149 print "Required option: ", join(', ', @missing), " missing!\n";
153 # explode and validate fields string
160 my @holdings_fields = split /,/, $c->{fields};
161 for (@holdings_fields) {
162 die "Field names must be alphanumeric!\n" if /\W/;
163 die "Field names must be single characters!\n"
166 $c->{fields} = \@holdings_fields;
171 Usage is: extract_holdings MARCXML_FILE HOLDINGS_TAG