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 use OpenILS::Utils::CStoreEditor;
17 use UNIVERSAL::require;
19 use Time::HiRes qw/time/;
23 my @formats = qw/USMARC UNIMARC XML BRE/;
25 my ($config,$format,$encoding,$location,$dollarsign,$idl,$help,$holdings,$timeout,$export_mfhd) = ('/openils/conf/opensrf_core.xml','USMARC','MARC8','','$',0,undef,undef,0,undef);
29 'items' => \$holdings,
30 'mfhd' => \$export_mfhd,
31 'location=s' => \$location,
32 'money=s' => \$dollarsign,
33 'config=s' => \$config,
34 'format=s' => \$format,
36 'encoding=s' => \$encoding,
37 'timeout=i' => \$timeout,
43 --help or -h This screen.
44 --config or -c Configuration file [/openils/conf/opensrf_core.xml]
45 --format or -f Output format (USMARC, UNIMARC, XML, BRE) [USMARC]
46 --encoding or -e Output Encoding (UTF-8, ISO-8859-?, MARC8) [MARC8]
47 --items or -i Include items (holdings) in the output
48 --mfhd Export serial MFHD records for associated bib records
49 Not compatible with --format=BRE
50 --xml-idl or -x Location of the IDL XML
51 --location or -l MARC Location Code for holdings from
52 http://www.loc.gov/marc/organizations/orgshome.html
53 --timeout Timeout for exporting a single record; increase if you
54 are using --holdings and are exporting bibs that
55 have a lot of items attached to them.
59 cat list_of_ids | $0 > output_file
65 $format = uc($format);
66 $encoding = uc($encoding);
68 binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
69 binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
71 if (!grep { uc($format) eq $_ } @formats) {
72 die "Please select a supported format. ".
73 "Right now that means one of [".
74 join('|',@formats). "]\n";
77 if ($format ne 'XML') {
78 my $type = 'MARC::File::' . $format;
83 # set default timeout and/or correct silly user who
84 # supplied a negative timeout; default timeout of
85 # 300 seconds if exporting items determined empirically.
86 $timeout = $holdings ? 300 : 1;
89 OpenSRF::System->bootstrap_client( config_file => $config );
92 $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
95 Fieldmapper->import(IDL => $idl);
97 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
98 OpenILS::Utils::CStoreEditor::init();
99 my $editor = OpenILS::Utils::CStoreEditor->new();
101 print <<HEADER if ($format eq 'XML');
102 <?xml version="1.0" encoding="$encoding"?>
103 <collection xmlns='http://www.loc.gov/MARC21/slim'>
112 print STDERR "Retrieving Org Units ... ";
113 my $r = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
115 while (my $o = $r->recv) {
116 die $r->failed->stringify if ($r->failed);
124 print STDERR "Retrieving Shelving locations ... ";
125 $r = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
127 while (my $s = $r->recv) {
128 die $r->failed->stringify if ($r->failed);
131 $shelves{$s->id} = $s;
136 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
140 my $last_time = time;
143 while ( my $i = <> ) {
146 my $r = $ses->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve', $i, $flesh );
147 my $s = $r->recv(timeout => $timeout);
149 warn "\n!!!!!! Failed trying to read record $i: " . $r->failed->stringify . "\n";
153 warn "\n!!!!!! Timed out trying to read record $i\n";
162 if (uc($format) eq 'BRE') {
163 print OpenSRF::Utils::JSON->perl2JSON($bib);
170 my $r = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
171 my $cn_list = $bib->call_numbers;
172 if ($cn_list && @$cn_list) {
174 $count{cn} += @$cn_list;
176 my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
177 if ($cp_list && @$cp_list) {
180 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
182 for my $cn ( @$cn_list ) {
183 my $cn_map_list = $cn_map{$cn->id};
185 for my $cp ( @$cn_map_list ) {
192 b => $orgs{$cn->owning_lib}->shortname,
193 b => $orgs{$cp->circ_lib}->shortname,
194 c => $shelves{$cp->location}->name,
196 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
198 ($cp->price ? ( y => $dollarsign.$cp->price ) : ()),
199 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
200 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
201 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
202 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
203 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
207 stats() if (! ($count{cp} % 100 ));
213 if (uc($format) eq 'XML') {
214 my $xml = $r->as_xml_record;
215 $xml =~ s/^<\?.+?\?>$//mo;
217 } elsif (uc($format) eq 'UNIMARC') {
219 } elsif (uc($format) eq 'USMARC') {
228 import MARC::File::XML; # reset SAX parser so that one bad record doesn't kill the entire export
232 my $mfhds = $editor->search_serial_record_entry({record => $i, deleted => 'f'});
233 foreach my $mfhd (@$mfhds) {
235 my $r = MARC::Record->new_from_xml( $mfhd->marc, $encoding, $format );
237 if (uc($format) eq 'XML') {
238 my $xml = $r->as_xml_record;
239 $xml =~ s/^<\?.+?\?>$//mo;
241 } elsif (uc($format) eq 'UNIMARC') {
243 } elsif (uc($format) eq 'USMARC') {
249 import MARC::File::XML; # reset SAX parser so that one bad record doesn't kill the entire export
254 stats() if (! ($count{bib} % 50 ));
257 print "</collection>\n" if ($format eq 'XML');
259 $speed = $count{did} / (time - $start);
260 my $time = time - $start;
263 Exports Attempted : $count{bib}
264 Exports Completed : $count{did}
265 Overall Speed : $speed
266 Total Time Elapsed: $time seconds
275 $speed = $count{did} / (time - $start);
277 my $speed_now = ($count{did} - $count{did_last}) / (time - $count{time_last});
278 my $cn_speed = $count{cn} / (time - $start);
279 my $cp_speed = $count{cp} / (time - $start);
281 printf STDERR "\r $count{did} of $count{bib} @ \%0.4f/s ttl / \%0.4f/s rt ".
282 "($count{cn} CNs @ \%0.4f/s :: $count{cp} CPs @ \%0.4f/s)\r",
288 $count{did_last} = $count{did};
289 $count{time_last} = time;