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