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;
16 use UNIVERSAL::require;
18 use Time::HiRes qw/time/;
22 my @formats = qw/USMARC UNIMARC XML BRE/;
24 my ($config,$format,$encoding,$location,$dollarsign,$idl,$help,$holdings) = ('/openils/conf/opensrf_core.xml','USMARC','MARC8','','$');
28 'items' => \$holdings,
29 'location=s' => \$location,
30 'money=s' => \$dollarsign,
31 'config=s' => \$config,
32 'format=s' => \$format,
34 'encoding=s' => \$encoding,
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
51 cat list_of_ids | $0 > output_file
57 $format = uc($format);
58 $encoding = uc($encoding);
60 binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
61 binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
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";
69 if ($format ne 'XML') {
70 my $type = 'MARC::File::' . $format;
74 OpenSRF::System->bootstrap_client( config_file => $config );
77 $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
80 Fieldmapper->import(IDL => $idl);
82 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
84 print <<HEADER if ($format eq 'XML');
85 <?xml version="1.0" encoding="$encoding"?>
86 <collection xmlns='http://www.loc.gov/MARC21/slim'>
95 print STDERR "Retrieving Org Units ... ";
96 my $r = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
98 while (my $o = $r->recv) {
99 die $r->failed->stringify if ($r->failed);
107 print STDERR "Retrieving Shelving locations ... ";
108 $r = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
110 while (my $s = $r->recv) {
111 die $r->failed->stringify if ($r->failed);
114 $shelves{$s->id} = $s;
119 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
123 my $last_time = time;
126 while ( my $i = <> ) {
129 local $SIG{ALRM} = sub { die "TIMEOUT\n" };
131 $bib = $ses->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve', $i, $flesh )->gather(1);
134 warn "\n!!!!!! Timed out trying to read record $i\n";
141 if (uc($format) eq 'BRE') {
142 print OpenSRF::Utils::JSON->perl2JSON($bib);
149 my $r = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
150 my $cn_list = $bib->call_numbers;
151 if ($cn_list && @$cn_list) {
153 $count{cn} += @$cn_list;
155 my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
156 if ($cp_list && @$cp_list) {
159 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
161 for my $cn ( @$cn_list ) {
162 my $cn_map_list = $cn_map{$cn->id};
164 for my $cp ( @$cn_map_list ) {
171 b => $orgs{$cn->owning_lib}->shortname,
172 b => $orgs{$cp->circ_lib}->shortname,
173 c => $shelves{$cp->location}->name,
175 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
177 ($cp->price ? ( y => $dollarsign.$cp->price ) : ()),
178 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
179 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
180 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
181 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
182 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
186 stats() if (! ($count{cp} % 100 ));
192 if (uc($format) eq 'XML') {
193 print $r->as_xml_record;
194 } elsif (uc($format) eq 'UNIMARC') {
196 } elsif (uc($format) eq 'USMARC') {
205 import MARC::File::XML; # reset SAX parser so that one bad record doesn't kill the entire export
208 stats() if (! ($count{bib} % 50 ));
211 print "</collection>\n" if ($format eq 'XML');
213 $speed = $count{did} / (time - $start);
214 my $time = time - $start;
217 Exports Attempted : $count{bib}
218 Exports Completed : $count{did}
219 Overall Speed : $speed
220 Total Time Elapsed: $time seconds
229 $speed = $count{did} / (time - $start);
231 my $speed_now = ($count{did} - $count{did_last}) / (time - $count{time_last});
232 my $cn_speed = $count{cn} / (time - $start);
233 my $cp_speed = $count{cp} / (time - $start);
235 printf STDERR "\r $count{did} of $count{bib} @ \%0.4f/s ttl / \%0.4f/s rt ".
236 "($count{cn} CNs @ \%0.4f/s :: $count{cp} CPs @ \%0.4f/s)\r",
242 $count{did_last} = $count{did};
243 $count{time_last} = time;