d140a249234c4442e976e5fa1c6a7b81c6cb62e7
[migration-tools.git] / extract_holdings
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use XML::Twig;
6 use YAML::Tiny;
7 use JSON;
8
9 my $marcxml = shift || help();
10 my $htag     = shift || help();
11
12 open HOLDINGS, '>', "holdings";
13 open X, '>', "holdings.x";
14 open Z, '>', "holdings.z";
15 my $holdings = {};
16 my %all852 = ( x => {}, z => {} ); # hash of all subfields in all 852s
17 my $copyid = 0;
18
19
20 $| = 1;
21 my $count = 0;
22 my $total = `grep -c '<record' $marcxml`;
23 my $percent = 0;
24 my $prevper = -1;
25
26 my $yaml = YAML::Tiny->new;
27 my $t = XML::Twig->new( twig_handlers => { record => \&record } );
28 $t->parsefile($marcxml);
29 $yaml->[0] = \%all852;
30 $yaml->write('holdings.sample');
31 print "\n\n";
32
33 sub record {
34     my($t, $r)= @_;
35     $holdings = { copies => [] };
36
37     my @dfields = $r->children('datafield');
38     for my $d (@dfields) 
39       { process_datafields($d) }
40
41     for my $copy (@{$holdings->{copies}})
42       { print_reports($copy) }
43     $r->purge;
44
45     $count++;
46     $percent = int(($count / $total) * 100);
47     print "\r$percent% done ($count)" if ($percent != $prevper);
48     $prevper = $percent;
49 }
50
51 sub process_datafields {
52     my ($d) = @_;
53     # get 903
54     if ($d->{'att'}->{'tag'} == 903) {
55         my $s = $d->first_child('subfield');
56         $holdings->{id} = $s->text;;
57     }
58
59     # and holdings data
60     if ($d->{'att'}->{'tag'} == $htag) {
61         push @{$holdings->{copies}}, { x =>[], z => [] };
62         $holdings->{copies}[-1]{copyid} = $copyid;
63         my @subs = $d->children('subfield');
64         for my $s (@subs)
65           { process_subs($s) }
66         $copyid++;
67     }
68 }
69
70 sub process_subs {
71     my ($s) = @_;
72     my $copy = $holdings->{copies}[-1];
73
74     my $code = $s->{'att'}->{'code'};
75     my $value = $s->text;
76
77     if ($code eq 'x' or $code eq 'z') {
78         push @{$copy->{$code}}, $value;
79         my ($k,$v) = split /:/, $value;
80         $all852{$code}{$k} = $v;
81     } else {
82         $copy->{$code} = $value;
83         $all852{$code} = $value;
84     }
85 }
86
87 sub print_reports {
88     return unless defined $holdings->{id};
89     my ($copy) = @_;
90     my $note = 0;
91     for (@{$copy->{x}}) {
92         print X join("\t", $holdings->{id}, $copy->{copyid}, $note, $_), "\n";
93         $note++;
94     }
95     $note = 0;
96     for (@{$copy->{z}}) {
97         print Z join("\t", $holdings->{id}, $copy->{copyid}, $note, $_), "\n";
98         $note++;
99     }
100     for (qw( copyid b p h 9 ))
101       { $copy->{$_} = '' unless defined $copy->{$_} }
102     print HOLDINGS join("\t", $holdings->{id}, $copy->{copyid},
103                         $copy->{b}, $copy->{p}, $copy->{h}, $copy->{9}), "\n";
104 }
105
106
107 sub help {
108     print <<HELP;
109 Usage is: extract_holdings MARCXML_FILE HOLDINGS_TAG
110 HELP
111     exit;
112 }