4e42171c373913d629ea9fc1230c6e415e6770b7
[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 use Text::CSV;
17
18 use OpenSRF::EX qw(:try);
19 use OpenSRF::Utils qw/:datetime/;
20 use OpenSRF::Utils::Cache;
21 use OpenSRF::System;
22 use OpenSRF::AppSession;
23 use XML::LibXML;
24 use XML::LibXSLT;
25
26 use Encode;
27 use Unicode::Normalize;
28 use OpenILS::Utils::Fieldmapper;
29 use OpenSRF::Utils::Logger qw/$logger/;
30
31 use MARC::Record;
32 use MARC::File::XML;
33
34 use UNIVERSAL::require;
35
36 our @formats = qw/USMARC UNIMARC XML BRE/;
37
38 # set the bootstrap config and template include directory when
39 # this module is loaded
40 my $bootstrap;
41
42 sub import {
43         my $self = shift;
44         $bootstrap = shift;
45 }
46
47
48 sub child_init {
49         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
50 }
51
52 sub handler {
53         my $r = shift;
54         my $cgi = new CGI;
55
56         # find some IDs ...
57         my @records;
58
59         @records = map { $_ ? ($_) : () } $cgi->param('id');
60
61         if (!@records) { # try for a file
62                 my $file = $cgi->param('idfile');
63                 if ($file) {
64                         my $col = $cgi->param('idcolumn') || 0;
65                         my $csv = new Text::CSV;
66
67                         while (<$file>) {
68                                 chomp;
69                                 $csv->parse($_);
70                                 my @data = $csv->fields;
71                                 my $id = $data[$col];
72                                 $id =~ s/\D+//o;
73                                 next unless ($id);
74                                 push @records, $id;
75                         }
76                 }
77         }
78
79         if (!@records) { # try pathinfo
80                 my $path_rec = $cgi->path_info();
81                 if ($path_rec) {
82                         @records = map { $_ ? ($_) : () } split '/', $path_rec;
83                 }
84         }
85
86         my $ses = OpenSRF::AppSession->create('open-ils.cstore');
87
88         # still no records ...
89         my $container = $cgi->param('containerid');
90         if ($container) {
91                 my $bucket = $ses->request( 'open-ils.cstore.direct.container.biblio_record_entry_bucket.retrieve', $container )->gather(1);
92         if ($bucket->pub !~ /t|1/oi) {
93                 my $authid = $cgi->cookie('ses') || $cgi->param('ses');
94                 my $auth = verify_login($authid);
95                     if (!$auth) {
96                             return 403;
97                 }
98         }
99                 my $recs = $ses->request( 'open-ils.cstore.direct.container.biblio_record_entry_bucket_item.search.atomic', { bucket => $container } )->gather(1);
100                 @records = map { ($_->target_biblio_record_entry) } @$recs;
101         }
102
103         return show_template($r) unless (@records);
104
105         my $type = $cgi->param('rectype') || 'biblio';
106         if ($type ne 'biblio' && $type ne 'authority') {
107                 return 400;
108         }
109
110         my $tcn_v = 'tcn_value';
111         my $tcn_s = 'tcn_source';
112
113         if ($type eq 'authority') {
114                 $tcn_v = 'arn_value';
115                 $tcn_s = 'arn_source';
116         }
117
118         my $holdings = $cgi->param('holdings') if ($type eq 'biblio');
119         my $location = $cgi->param('location') || 'gaaagpl'; # just because...
120
121         my $format = $cgi->param('format') || 'USMARC';
122         $format = uc($format);
123
124         my $encoding = $cgi->param('encoding') || 'UTF-8';
125         $encoding = uc($encoding);
126
127         my $filename = $cgi->param('filename') || "export.$type.$encoding.$format";
128
129         binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
130         binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
131
132         if (!grep { uc($format) eq $_ } @formats) {
133                 return 400;
134         }
135
136         if ($format ne 'XML') {
137                 my $ftype = 'MARC::File::' . $format;
138                 $ftype->require;
139         }
140
141
142         $r->headers_out->set("Content-Disposition" => "inline; filename=$filename");
143
144         if (uc($format) eq 'XML') {
145                 $r->content_type('application/xml');
146         } else {
147                 $r->content_type('application/octet-stream');
148         }
149
150         $r->print( <<"  HEADER" ) if (uc($format) eq 'XML');
151 <?xml version="1.0" encoding="$encoding"?>
152 <collection xmlns='http://www.loc.gov/MARC21/slim'>
153         HEADER
154
155         my %orgs;
156         my %shelves;
157
158         my $flesh = {};
159         if ($holdings) {
160
161                 my $req = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
162
163                 while (my $o = $req->recv) {
164                         next if ($req->failed);
165                         $o = $o->content;
166                         last unless ($o);
167                         $orgs{$o->id} = $o;
168                 }
169                 $req->finish;
170
171                 $req = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
172
173                 while (my $s = $req->recv) {
174                         next if ($req->failed);
175                         $s = $s->content;
176                         last unless ($s);
177                         $shelves{$s->id} = $s;
178                 }
179                 $req->finish;
180
181                 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
182         }
183
184         for my $i ( @records ) {
185                 my $bib;
186                 try {
187                         local $SIG{ALRM} = sub { die "TIMEOUT\n" };
188                         alarm(1);
189                         $bib = $ses->request( "open-ils.cstore.direct.$type.record_entry.retrieve", $i, $flesh )->gather(1);
190                         alarm(0);
191                 } otherwise {
192                         warn "\n!!!!!! Timed out trying to read record $i\n";
193                 };
194                 alarm(0);
195
196                 next unless $bib;
197
198                 if (uc($format) eq 'BRE') {
199                         $r->print( OpenSRF::Utils::JSON->perl2JSON($bib) );
200                         next;
201                 }
202
203                 try {
204
205                         my $req = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
206                         $req->delete_field( $_ ) for ($req->field(901));
207
208                         $req->append_fields(
209                                 MARC::Field->new(
210                                         901, '', '', 
211                                         a => $bib->$tcn_v,
212                                         b => $bib->$tcn_s,
213                                         c => $bib->id
214                                 )
215                         );
216
217
218                         if ($holdings) {
219                                 my $cn_list = $bib->call_numbers;
220                                 if ($cn_list && @$cn_list) {
221
222                                         my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
223                                         if ($cp_list && @$cp_list) {
224
225                                                 my %cn_map;
226                                                 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
227                                         
228                                                 for my $cn ( @$cn_list ) {
229                                                         my $cn_map_list = $cn_map{$cn->id};
230         
231                                                         for my $cp ( @$cn_map_list ) {
232                                         
233                                                                 $req->append_fields(
234                                                                         MARC::Field->new(
235                                                                                 852, '4', '', 
236                                                                                 a => $location,
237                                                                                 b => $orgs{$cn->owning_lib}->shortname,
238                                                                                 b => $orgs{$cp->circ_lib}->shortname,
239                                                                                 c => $shelves{$cp->location}->name,
240                                                                                 j => $cn->label,
241                                                                                 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
242                                                                                 p => $cp->barcode,
243                                                                                 ($cp->price ? ( y => $cp->price ) : ()),
244                                                                                 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
245                                                                                 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
246                                                                                 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
247                                                                                 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
248                                                                                 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
249                                                                         )
250                                                                 );
251
252                                                         }
253                                                 }
254                                         }
255                                 }
256                         }
257
258                         if (uc($format) eq 'XML') {
259                                 my $x = $req->as_xml_record;
260                                 $x =~ s/^<\?xml version="1.0" encoding="UTF-8"\?>//o;
261                                 $r->print($x);
262                         } elsif (uc($format) eq 'UNIMARC') {
263                                 $r->print($req->as_unimarc);
264                         } elsif (uc($format) eq 'USMARC') {
265                                 $r->print($req->as_usmarc);
266                         }
267
268             $r->rflush();
269
270                 } otherwise {
271                         my $e = shift;
272                         warn "\n$e\n";
273                 };
274
275         }
276
277         $r->print("</collection>\n") if ($format eq 'XML');
278
279         return Apache2::Const::OK;
280
281 }
282
283 sub verify_login {
284         my $auth_token = shift;
285         return undef unless $auth_token;
286
287         my $user = OpenSRF::AppSession
288                 ->create("open-ils.auth")
289                 ->request( "open-ils.auth.session.retrieve", $auth_token )
290                 ->gather(1);
291
292         if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
293                 return undef;
294         }
295
296         return $user if ref($user);
297         return undef;
298 }
299
300 sub show_template {
301         my $r = shift;
302
303         $r->content_type('text/html');
304         $r->print(<<HTML);
305
306 <html>
307         <head>
308                 <title>Record Export</title>
309         </head>
310         <body>
311                 <form method="POST" enctype="multipart/form-data">
312                         Use field number <input type="text" size="2" maxlength="2" name="idcolumn" value="0"/> (starting from 0)
313                         from CSV file <input type="file" name="idfile"/>
314                         <br/><br/> <b>or</b> <br/><br/>
315                         Record ID <input type="text" size="12" maxlength="12" name="id"/>
316                         <br/><br/> Record Type:
317                         <select name="rectype">
318                                 <option value="biblio">Bibliographic Records</option>
319                                 <option value="authority">Authority Records</option>
320                         </select>
321                         <br/> Record Format:
322                         <select name="format">
323                                 <option value="USMARC">MARC21</option>
324                                 <option value="UNIMARC">UNIMARC</option>
325                                 <option value="XML">MARC XML</option>
326                                 <option value="BRE">Evergreen BRE</option>
327                         </select>
328                         <br/> Record Encoding:
329                         <select name="encoding">
330                                 <option value="UTF-8">UTF-8</option>
331                                 <option value="MARC8">MARC8</option>
332                         </select>
333                         <br/> Include holdings in Bibliographic Records:
334                         <input type="checkbox" name="holdings" value="1">
335                         <br/><br/><input type="submit" value="Retrieve Records"/>
336                 </form>
337         </body>
338 </html>
339
340 HTML
341
342         return Apache2::Const::OK;
343 }
344
345 1;