1 package OpenILS::WWW::Exporter;
7 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
8 use APR::Const -compile => qw(:error SUCCESS);
9 use Apache2::RequestRec ();
10 use Apache2::RequestIO ();
11 use Apache2::RequestUtil;
15 use OpenSRF::EX qw(:try);
16 use OpenSRF::Utils qw/:datetime/;
17 use OpenSRF::Utils::Cache;
19 use OpenSRF::AppSession;
24 use Unicode::Normalize;
25 use OpenILS::Utils::Fieldmapper;
26 use OpenSRF::Utils::Logger qw/$logger/;
31 use UNIVERSAL::require;
33 our @formats = qw/USMARC UNIMARC XML BRE/;
35 # set the bootstrap config and template include directory when
36 # this module is loaded
46 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
53 my @records = $cgi->param('id');
55 return 200 unless (@records);
57 my $holdings = $cgi->param('holdings');
58 my $location = $cgi->param('location') || 'gaaagpl'; # just because...
60 my $format = $cgi->param('format') || 'USMARC';
61 $format = uc($format);
63 my $encoding = $cgi->param('encoding') || 'UTF-8';
64 $encoding = uc($encoding);
66 binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
67 binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
69 if (!grep { uc($format) eq $_ } @formats) {
70 die "Please select a supported format. ".
71 "Right now that means one of [".
72 join('|',@formats). "]\n";
75 if ($format ne 'XML') {
76 my $type = 'MARC::File::' . $format;
80 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
82 print "Content-type: application/octet-stream\n\n" if (uc($format) ne 'XML');
83 print <<" HEADER" if (uc($format) eq 'XML');
84 Content-type: application/xml
86 <?xml version="1.0" encoding="$encoding"?>
87 <collection xmlns='http://www.loc.gov/MARC21/slim'>
96 print STDERR "Retrieving Org Units ... ";
97 my $r = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
99 while (my $o = $r->recv) {
100 die $r->failed->stringify if ($r->failed);
108 print STDERR "Retrieving Shelving locations ... ";
109 $r = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
111 while (my $s = $r->recv) {
112 die $r->failed->stringify if ($r->failed);
115 $shelves{$s->id} = $s;
120 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
123 for my $i ( @records ) {
126 local $SIG{ALRM} = sub { die "TIMEOUT\n" };
128 $bib = $ses->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve', $i, $flesh )->gather(1);
131 warn "\n!!!!!! Timed out trying to read record $i\n";
137 if (uc($format) eq 'BRE') {
138 print OpenSRF::Utils::JSON->perl2JSON($bib);
144 my $r = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
145 $r->delete_field( $_ ) for ($r->field(901));
150 a => $bib->tcn_value,
151 b => $bib->tcn_source,
157 my $cn_list = $bib->call_numbers;
158 if ($cn_list && @$cn_list) {
160 my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
161 if ($cp_list && @$cp_list) {
164 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
166 for my $cn ( @$cn_list ) {
167 my $cn_map_list = $cn_map{$cn->id};
169 for my $cp ( @$cn_map_list ) {
175 b => $orgs{$cn->owning_lib}->shortname,
176 b => $orgs{$cp->circ_lib}->shortname,
177 c => $shelves{$cp->location}->name,
179 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
181 ($cp->price ? ( y => $cp->price ) : ()),
182 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
183 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
184 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
185 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
186 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
195 if (uc($format) eq 'XML') {
196 print $r->as_xml_record;
197 } elsif (uc($format) eq 'UNIMARC') {
199 } elsif (uc($format) eq 'USMARC') {
210 print "</collection>\n" if ($format eq 'XML');