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 $type = $cgi->param('rectype') || 'biblio';
58 if ($type ne 'biblio' && $type ne 'authority') {
59 die "Bad record type: $type";
62 my $tcn_v = 'tcn_value';
63 my $tcn_s = 'tcn_source';
65 if ($type eq 'authority') {
67 $tcn_s = 'arn_source';
70 my $holdings = $cgi->param('holdings') if ($type eq 'biblio');
71 my $location = $cgi->param('location') || 'gaaagpl'; # just because...
73 my $format = $cgi->param('format') || 'USMARC';
74 $format = uc($format);
76 my $encoding = $cgi->param('encoding') || 'UTF-8';
77 $encoding = uc($encoding);
79 binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
80 binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
82 if (!grep { uc($format) eq $_ } @formats) {
83 die "Please select a supported format. ".
84 "Right now that means one of [".
85 join('|',@formats). "]\n";
88 if ($format ne 'XML') {
89 my $ftype = 'MARC::File::' . $format;
93 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
95 $r->content_type('application/octet-stream') if (uc($format) ne 'XML');
96 $r->content_type('application/xml') if (uc($format) eq 'XML');
101 $content .= <<" HEADER" if (uc($format) eq 'XML');
102 <?xml version="1.0" encoding="$encoding"?>
103 <collection xmlns='http://www.loc.gov/MARC21/slim'>
112 my $r = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
114 while (my $o = $r->recv) {
115 die $r->failed->stringify if ($r->failed);
122 $r = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
124 while (my $s = $r->recv) {
125 die $r->failed->stringify if ($r->failed);
128 $shelves{$s->id} = $s;
132 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
135 for my $i ( @records ) {
138 local $SIG{ALRM} = sub { die "TIMEOUT\n" };
140 $bib = $ses->request( "open-ils.cstore.direct.$type.record_entry.retrieve", $i, $flesh )->gather(1);
143 warn "\n!!!!!! Timed out trying to read record $i\n";
149 if (uc($format) eq 'BRE') {
150 $content .= OpenSRF::Utils::JSON->perl2JSON($bib);
156 my $r = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
157 $r->delete_field( $_ ) for ($r->field(901));
170 my $cn_list = $bib->call_numbers;
171 if ($cn_list && @$cn_list) {
173 my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
174 if ($cp_list && @$cp_list) {
177 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
179 for my $cn ( @$cn_list ) {
180 my $cn_map_list = $cn_map{$cn->id};
182 for my $cp ( @$cn_map_list ) {
188 b => $orgs{$cn->owning_lib}->shortname,
189 b => $orgs{$cp->circ_lib}->shortname,
190 c => $shelves{$cp->location}->name,
192 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
194 ($cp->price ? ( y => $cp->price ) : ()),
195 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
196 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
197 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
198 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
199 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
209 if (uc($format) eq 'XML') {
210 my $x = $r->as_xml_record;
211 $x =~ s/^<\?xml version="1.0" encoding="UTF-8"\?>//o;
213 } elsif (uc($format) eq 'UNIMARC') {
214 $content .= $r->as_unimarc
215 } elsif (uc($format) eq 'USMARC') {
216 $content .= $r->as_usmarc
226 $content .= "</collection>\n" if ($format eq 'XML');
229 return Apache2::Const::OK;