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 $r->delete_field( $_ ) for ($r->field(901));
155 a => $bib->tcn_value,
156 b => $bib->tcn_source,
162 my $cn_list = $bib->call_numbers;
163 if ($cn_list && @$cn_list) {
165 $count{cn} += @$cn_list;
167 my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
168 if ($cp_list && @$cp_list) {
171 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
173 for my $cn ( @$cn_list ) {
174 my $cn_map_list = $cn_map{$cn->id};
176 for my $cp ( @$cn_map_list ) {
183 b => $orgs{$cn->owning_lib}->shortname,
184 b => $orgs{$cp->circ_lib}->shortname,
185 c => $shelves{$cp->location}->name,
187 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
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' ) : ()),
198 stats() if (! ($count{cp} % 100 ));
204 if (uc($format) eq 'XML') {
205 print $r->as_xml_record;
206 } elsif (uc($format) eq 'UNIMARC') {
208 } elsif (uc($format) eq 'USMARC') {
219 stats() if (! ($count{bib} % 50 ));
222 print "</collection>\n" if ($format eq 'XML');
224 $speed = $count{did} / (time - $start);
225 my $time = time - $start;
228 Exports Attempted : $count{bib}
229 Exports Completed : $count{did}
230 Overall Speed : $speed
231 Total Time Elapsed: $time seconds
240 $speed = $count{did} / (time - $start);
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);
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",
253 $count{did_last} = $count{did};
254 $count{time_last} = time;