]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/Exporter.pm
allow for alternate user-supplied filename
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / Exporter.pm
1 package OpenILS::WWW::Exporter;
2 use strict;
3 use warnings;
4 use bytes;
5
6 use Apache2::Log;
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;
12 use CGI;
13 use Data::Dumper;
14
15 use OpenSRF::EX qw(:try);
16 use OpenSRF::Utils qw/:datetime/;
17 use OpenSRF::Utils::Cache;
18 use OpenSRF::System;
19 use OpenSRF::AppSession;
20 use XML::LibXML;
21 use XML::LibXSLT;
22
23 use Encode;
24 use Unicode::Normalize;
25 use OpenILS::Utils::Fieldmapper;
26 use OpenSRF::Utils::Logger qw/$logger/;
27
28 use MARC::Record;
29 use MARC::File::XML;
30
31 use UNIVERSAL::require;
32
33 our @formats = qw/USMARC UNIMARC XML BRE/;
34
35 # set the bootstrap config and template include directory when
36 # this module is loaded
37 my $bootstrap;
38
39 sub import {
40         my $self = shift;
41         $bootstrap = shift;
42 }
43
44
45 sub child_init {
46         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
47 }
48
49 sub handler {
50         my $r = shift;
51         my $cgi = new CGI;
52         
53         my @records = $cgi->param('id');
54
55         return 200 unless (@records);
56
57         my $type = $cgi->param('rectype') || 'biblio';
58         if ($type ne 'biblio' && $type ne 'authority') {
59                 die "Bad record type: $type";
60         }
61
62         my $tcn_v = 'tcn_value';
63         my $tcn_s = 'tcn_source';
64
65         if ($type eq 'authority') {
66                 $tcn_v = 'arn_value';
67                 $tcn_s = 'arn_source';
68         }
69
70         my $holdings = $cgi->param('holdings') if ($type eq 'biblio');
71         my $location = $cgi->param('location') || 'gaaagpl'; # just because...
72
73         my $format = $cgi->param('format') || 'USMARC';
74         $format = uc($format);
75
76         my $encoding = $cgi->param('encoding') || 'UTF-8';
77         $encoding = uc($encoding);
78
79         my $filename = $cgi->param('filename') || "export.$type.$encoding.$format";
80
81         binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
82         binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
83
84         if (!grep { uc($format) eq $_ } @formats) {
85                 die     "Please select a supported format.  ".
86                         "Right now that means one of [".
87                         join('|',@formats). "]\n";
88         }
89
90         if ($format ne 'XML') {
91                 my $ftype = 'MARC::File::' . $format;
92                 $ftype->require;
93         }
94
95         my $ses = OpenSRF::AppSession->create('open-ils.cstore');
96
97         $r->headers_out->set("Content-Disposition" => "inline; filename=$filename");
98
99         if (uc($format) eq 'XML') {
100                 $r->send_http_header('application/xml');
101         } else {
102                 $r->send_http_header('application/octet-stream');
103         }
104
105         $r->print( <<"  HEADER" ) if (uc($format) eq 'XML');
106 <?xml version="1.0" encoding="$encoding"?>
107 <collection xmlns='http://www.loc.gov/MARC21/slim'>
108         HEADER
109
110         my %orgs;
111         my %shelves;
112
113         my $flesh = {};
114         if ($holdings) {
115
116                 my $req = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
117
118                 while (my $o = $req->recv) {
119                         die $req->failed->stringify if ($req->failed);
120                         $o = $o->content;
121                         last unless ($o);
122                         $orgs{$o->id} = $o;
123                 }
124                 $req->finish;
125
126                 $req = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
127
128                 while (my $s = $req->recv) {
129                         die $req->failed->stringify if ($req->failed);
130                         $s = $s->content;
131                         last unless ($s);
132                         $shelves{$s->id} = $s;
133                 }
134                 $req->finish;
135
136                 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
137         }
138
139         for my $i ( @records ) {
140                 my $bib;
141                 try {
142                         local $SIG{ALRM} = sub { die "TIMEOUT\n" };
143                         alarm(1);
144                         $bib = $ses->request( "open-ils.cstore.direct.$type.record_entry.retrieve", $i, $flesh )->gather(1);
145                         alarm(0);
146                 } otherwise {
147                         warn "\n!!!!!! Timed out trying to read record $i\n";
148                 };
149                 alarm(0);
150
151                 next unless $bib;
152
153                 if (uc($format) eq 'BRE') {
154                         $r->print( OpenSRF::Utils::JSON->perl2JSON($bib) );
155                         next;
156                 }
157
158                 try {
159
160                         my $req = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
161                         $req->delete_field( $_ ) for ($req->field(901));
162
163                         $req->append_fields(
164                                 MARC::Field->new(
165                                         901, '', '', 
166                                         a => $bib->$tcn_v,
167                                         b => $bib->$tcn_s,
168                                         c => $bib->id
169                                 )
170                         );
171
172
173                         if ($holdings) {
174                                 my $cn_list = $bib->call_numbers;
175                                 if ($cn_list && @$cn_list) {
176
177                                         my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
178                                         if ($cp_list && @$cp_list) {
179
180                                                 my %cn_map;
181                                                 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
182                                         
183                                                 for my $cn ( @$cn_list ) {
184                                                         my $cn_map_list = $cn_map{$cn->id};
185         
186                                                         for my $cp ( @$cn_map_list ) {
187                                         
188                                                                 $req->append_fields(
189                                                                         MARC::Field->new(
190                                                                                 852, '4', '', 
191                                                                                 a => $location,
192                                                                                 b => $orgs{$cn->owning_lib}->shortname,
193                                                                                 b => $orgs{$cp->circ_lib}->shortname,
194                                                                                 c => $shelves{$cp->location}->name,
195                                                                                 j => $cn->label,
196                                                                                 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
197                                                                                 p => $cp->barcode,
198                                                                                 ($cp->price ? ( y => $cp->price ) : ()),
199                                                                                 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
200                                                                                 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
201                                                                                 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
202                                                                                 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
203                                                                                 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
204                                                                         )
205                                                                 );
206
207                                                         }
208                                                 }
209                                         }
210                                 }
211                         }
212
213                         if (uc($format) eq 'XML') {
214                                 my $x = $req->as_xml_record;
215                                 $x =~ s/^<\?xml version="1.0" encoding="UTF-8"\?>//o;
216                                 $r->print($x);
217                         } elsif (uc($format) eq 'UNIMARC') {
218                                 $r->print($req->as_unimarc);
219                         } elsif (uc($format) eq 'USMARC') {
220                                 $r->print($req->as_usmarc);
221                         }
222
223                 } otherwise {
224                         my $e = shift;
225                         warn "\n$e\n";
226                 };
227
228         }
229
230         $r->print("</collection>\n") if ($format eq 'XML');
231
232         return Apache2::Const::OK;
233
234 }
235
236 1;