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);
11 use Apache2::RequestRec ();
12 use Apache2::RequestIO ();
13 use Apache2::RequestUtil;
17 use OpenSRF::EX qw(:try);
18 use OpenSRF::Utils qw/:datetime/;
19 use OpenSRF::Utils::Cache;
21 use OpenSRF::AppSession;
26 use Unicode::Normalize;
27 use OpenILS::Utils::Fieldmapper;
28 use OpenSRF::Utils::Logger qw/$logger/;
33 use UNIVERSAL::require;
35 our @formats = qw/USMARC UNIMARC XML BRE/;
37 # set the bootstrap config and template include directory when
38 # this module is loaded
48 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
55 my @records = $cgi->param('id');
57 return 200 unless (@records);
59 my $type = $cgi->param('rectype') || 'biblio';
60 if ($type ne 'biblio' && $type ne 'authority') {
61 die "Bad record type: $type";
64 my $tcn_v = 'tcn_value';
65 my $tcn_s = 'tcn_source';
67 if ($type eq 'authority') {
69 $tcn_s = 'arn_source';
72 my $holdings = $cgi->param('holdings') if ($type eq 'biblio');
73 my $location = $cgi->param('location') || 'gaaagpl'; # just because...
75 my $format = $cgi->param('format') || 'USMARC';
76 $format = uc($format);
78 my $encoding = $cgi->param('encoding') || 'UTF-8';
79 $encoding = uc($encoding);
81 my $filename = $cgi->param('filename') || "export.$type.$encoding.$format";
83 binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
84 binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
86 if (!grep { uc($format) eq $_ } @formats) {
87 die "Please select a supported format. ".
88 "Right now that means one of [".
89 join('|',@formats). "]\n";
92 if ($format ne 'XML') {
93 my $ftype = 'MARC::File::' . $format;
97 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
99 $r->headers_out->set("Content-Disposition" => "inline; filename=$filename");
101 if (uc($format) eq 'XML') {
102 $r->send_http_header('application/xml');
104 $r->send_http_header('application/octet-stream');
107 $r->print( <<" HEADER" ) if (uc($format) eq 'XML');
108 <?xml version="1.0" encoding="$encoding"?>
109 <collection xmlns='http://www.loc.gov/MARC21/slim'>
118 my $req = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
120 while (my $o = $req->recv) {
121 die $req->failed->stringify if ($req->failed);
128 $req = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
130 while (my $s = $req->recv) {
131 die $req->failed->stringify if ($req->failed);
134 $shelves{$s->id} = $s;
138 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
141 for my $i ( @records ) {
144 local $SIG{ALRM} = sub { die "TIMEOUT\n" };
146 $bib = $ses->request( "open-ils.cstore.direct.$type.record_entry.retrieve", $i, $flesh )->gather(1);
149 warn "\n!!!!!! Timed out trying to read record $i\n";
155 if (uc($format) eq 'BRE') {
156 $r->print( OpenSRF::Utils::JSON->perl2JSON($bib) );
162 my $req = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
163 $req->delete_field( $_ ) for ($req->field(901));
176 my $cn_list = $bib->call_numbers;
177 if ($cn_list && @$cn_list) {
179 my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
180 if ($cp_list && @$cp_list) {
183 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
185 for my $cn ( @$cn_list ) {
186 my $cn_map_list = $cn_map{$cn->id};
188 for my $cp ( @$cn_map_list ) {
194 b => $orgs{$cn->owning_lib}->shortname,
195 b => $orgs{$cp->circ_lib}->shortname,
196 c => $shelves{$cp->location}->name,
198 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
200 ($cp->price ? ( y => $cp->price ) : ()),
201 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
202 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
203 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
204 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
205 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
215 if (uc($format) eq 'XML') {
216 my $x = $req->as_xml_record;
217 $x =~ s/^<\?xml version="1.0" encoding="UTF-8"\?>//o;
219 } elsif (uc($format) eq 'UNIMARC') {
220 $r->print($req->as_unimarc);
221 } elsif (uc($format) eq 'USMARC') {
222 $r->print($req->as_usmarc);
232 $r->print("</collection>\n") if ($format eq 'XML');
234 return Apache2::Const::OK;