]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/marc_export
Patch from Dan Scott to move JSON to OpenSRF::Utils::JSON:
[Evergreen.git] / Open-ILS / src / support-scripts / marc_export
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use bytes;
5
6 use OpenSRF::System;
7 use OpenSRF::EX qw/:try/;
8 use OpenSRF::AppSession;
9 use OpenSRF::Utils::JSON;
10 use OpenSRF::Utils::SettingsClient;
11 use OpenILS::Application::AppUtils;
12 use OpenILS::Utils::Fieldmapper;
13
14 use MARC::Record;
15 use MARC::File::XML;
16 use UNIVERSAL::require;
17
18 use Time::HiRes qw/time/;
19 use Getopt::Long;
20
21
22 my @formats = qw/USMARC UNIMARC XML BRE/;
23
24 my ($config,$format,$encoding,$location,$dollarsign,$idl,$help,$holdings) = ('/openils/conf/opensrf_core.xml','USMARC','MARC8','','$');
25
26 GetOptions(
27         'help'      => \$help,
28         'items'      => \$holdings,
29         'location=s'      => \$location,
30         'money=s'      => \$dollarsign,
31         'config=s'      => \$config,
32         'format=s'      => \$format,
33         'xml-idl=s'      => \$idl,
34         'encoding=s'      => \$encoding,
35 );
36
37 if ($help) {
38         print <<"       HELP";
39 Usage: $0 [options]
40  --help or -h           This screen.
41  --config or -c         Configuration file [/openils/conf/opensrf_core.xml]
42  --format or -f         Output format (USMARC, UNIMARC, XML) [USMARC]
43  --encoding or -e       Output Encoding (UTF-8, ISO-8859-?, MARC8) [MARC8]
44  --items or -i          Include items (holdings) in the output
45  --xml-idl or -x        Location of the IDL XML
46  --location or -l       MARC Location Code for holdings from
47                         http://www.loc.gov/marc/organizations/orgshome.html
48
49 Example:
50
51   cat list_of_ids | $0 > output_file
52
53         HELP
54         exit;
55 }
56
57 $format = uc($format);
58 $encoding = uc($encoding);
59
60 binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
61 binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
62
63 if (!grep { uc($format) eq $_ } @formats) {
64         die     "Please select a supported format.  ".
65                 "Right now that means one of [".
66                 join('|',@formats). "]\n";
67 }
68
69 if ($format ne 'XML') {
70         my $type = 'MARC::File::' . $format;
71         $type->require;
72 }
73
74 OpenSRF::System->bootstrap_client( config_file => $config );
75
76 if (!$idl) {
77         $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
78 }
79
80 Fieldmapper->import(IDL => $idl);
81
82 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
83
84 print <<HEADER if ($format eq 'XML');
85 <?xml version="1.0" encoding="$encoding"?>
86 <collection xmlns='http://www.loc.gov/MARC21/slim'>
87 HEADER
88
89 my %orgs;
90 my %shelves;
91
92 my $flesh = {};
93 if ($holdings) {
94
95         print STDERR "Retrieving Org Units ... ";
96         my $r = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
97
98     while (my $o = $r->recv) {
99         die $r->failed->stringify if ($r->failed);
100         $o = $o->content;
101         last unless ($o);
102             $orgs{$o->id} = $o;
103     }
104     $r->finish;
105         print STDERR "OK\n";
106
107         print STDERR "Retrieving Shelving locations ... ";
108         $r = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
109
110     while (my $s = $r->recv) {
111         die $r->failed->stringify if ($r->failed);
112         $s = $s->content;
113         last unless ($s);
114             $shelves{$s->id} = $s;
115     }
116     $r->finish;
117         print STDERR "OK\n";
118
119     $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
120 }
121
122 my $start = time;
123 my $last_time = time;
124 my %count = ();
125 my $speed = 0;
126 while ( my $i = <> ) {
127     my $bib;
128     try {
129         local $SIG{ALRM} = sub { die "TIMEOUT\n" };
130         alarm(1);
131             $bib = $ses->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve', $i, $flesh )->gather(1);
132         alarm(0);
133     } otherwise {
134         warn "\n!!!!!! Timed out trying to read record $i\n";
135     };
136     alarm(0);
137
138     $count{bib}++;
139         next unless $bib;
140
141     if (uc($format) eq 'BRE') {
142         print OpenSRF::Utils::JSON->perl2JSON($bib);
143             stats();
144         next;
145     }
146
147         try {
148
149                 my $r = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
150                 $r->delete_field( $_ ) for ($r->field(901));
151
152                 $r->append_fields(
153                         MARC::Field->new(
154                                 901, '', '', 
155                                 a => $bib->tcn_value,
156                                 b => $bib->tcn_source,
157                                 c => $bib->id
158                         )
159                 );
160
161
162         my $cn_list = $bib->call_numbers;
163         if ($cn_list && @$cn_list) {
164
165                 $count{cn} += @$cn_list;
166                 
167             my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
168             if ($cp_list && @$cp_list) {
169
170                     my %cn_map;
171                     push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
172                                         
173                     for my $cn ( @$cn_list ) {
174                         my $cn_map_list = $cn_map{$cn->id};
175         
176                         for my $cp ( @$cn_map_list ) {
177                             $count{cp}++;
178                                         
179                                                 $r->append_fields(
180                                                         MARC::Field->new(
181                                                                 852, '4', '', 
182                                                                 a => $location,
183                                                                 b => $orgs{$cn->owning_lib}->shortname,
184                                                                 b => $orgs{$cp->circ_lib}->shortname,
185                                                                 c => $shelves{$cp->location}->name,
186                                                                 j => $cn->label,
187                                                                 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
188                                                                 p => $cp->barcode,
189                                                                 ($cp->price ? ( y => $dollarsign.$cp->price ) : ()),
190                                                                 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
191                                                                 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
192                                                                 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
193                                                                 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
194                                                                 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
195                                                         )
196                                                 );
197
198                         stats() if (! ($count{cp} % 100 ));
199                                         }
200                                 }
201                         }
202         }
203
204                 if (uc($format) eq 'XML') {
205                         print $r->as_xml_record;
206                 } elsif (uc($format) eq 'UNIMARC') {
207                         print $r->as_unimarc
208                 } elsif (uc($format) eq 'USMARC') {
209                         print $r->as_usmarc
210                 }
211
212         $count{did}++;
213
214         } otherwise {
215                 my $e = shift;
216                 warn "\n$e\n";
217         };
218
219         stats() if (! ($count{bib} % 50 ));
220 }
221
222 print "</collection>\n" if ($format eq 'XML');
223
224 $speed = $count{did} / (time - $start);
225 my $time = time - $start;
226 print STDERR <<DONE;
227
228 Exports Attempted : $count{bib}
229 Exports Completed : $count{did}
230 Overall Speed     : $speed
231 Total Time Elapsed: $time seconds
232
233 DONE
234
235
236 sub stats {
237     try {
238         no warnings;
239
240         $speed = $count{did} / (time - $start);
241
242         my $speed_now = ($count{did} - $count{did_last}) / (time - $count{time_last});
243             my $cn_speed = $count{cn} / (time - $start);
244         my $cp_speed = $count{cp} / (time - $start);
245
246             printf STDERR "\r  $count{did} of $count{bib} @  \%0.4f/s ttl / \%0.4f/s rt ".
247                 "($count{cn} CNs @ \%0.4f/s :: $count{cp} CPs @ \%0.4f/s)\r",
248                 $speed,
249                 $speed_now,
250                 $cn_speed,
251                 $cp_speed;
252     } otherwise {};
253         $count{did_last} = $count{did};
254         $count{time_last} = time;
255 }
256
257