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