]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/Exporter.pm
force flushing of the apache/mod_perl buffer after each record
[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         return show_template($r) unless (@records);
87
88         my $type = $cgi->param('rectype') || 'biblio';
89         if ($type ne 'biblio' && $type ne 'authority') {
90                 die "Bad record type: $type";
91         }
92
93         my $tcn_v = 'tcn_value';
94         my $tcn_s = 'tcn_source';
95
96         if ($type eq 'authority') {
97                 $tcn_v = 'arn_value';
98                 $tcn_s = 'arn_source';
99         }
100
101         my $holdings = $cgi->param('holdings') if ($type eq 'biblio');
102         my $location = $cgi->param('location') || 'gaaagpl'; # just because...
103
104         my $format = $cgi->param('format') || 'USMARC';
105         $format = uc($format);
106
107         my $encoding = $cgi->param('encoding') || 'UTF-8';
108         $encoding = uc($encoding);
109
110         my $filename = $cgi->param('filename') || "export.$type.$encoding.$format";
111
112         binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
113         binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
114
115         if (!grep { uc($format) eq $_ } @formats) {
116                 die     "Please select a supported format.  ".
117                         "Right now that means one of [".
118                         join('|',@formats). "]\n";
119         }
120
121         if ($format ne 'XML') {
122                 my $ftype = 'MARC::File::' . $format;
123                 $ftype->require;
124         }
125
126         my $ses = OpenSRF::AppSession->create('open-ils.cstore');
127
128         $r->headers_out->set("Content-Disposition" => "inline; filename=$filename");
129
130         if (uc($format) eq 'XML') {
131                 $r->content_type('application/xml');
132         } else {
133                 $r->content_type('application/octet-stream');
134         }
135
136         $r->print( <<"  HEADER" ) if (uc($format) eq 'XML');
137 <?xml version="1.0" encoding="$encoding"?>
138 <collection xmlns='http://www.loc.gov/MARC21/slim'>
139         HEADER
140
141         my %orgs;
142         my %shelves;
143
144         my $flesh = {};
145         if ($holdings) {
146
147                 my $req = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
148
149                 while (my $o = $req->recv) {
150                         die $req->failed->stringify if ($req->failed);
151                         $o = $o->content;
152                         last unless ($o);
153                         $orgs{$o->id} = $o;
154                 }
155                 $req->finish;
156
157                 $req = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
158
159                 while (my $s = $req->recv) {
160                         die $req->failed->stringify if ($req->failed);
161                         $s = $s->content;
162                         last unless ($s);
163                         $shelves{$s->id} = $s;
164                 }
165                 $req->finish;
166
167                 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
168         }
169
170         for my $i ( @records ) {
171                 my $bib;
172                 try {
173                         local $SIG{ALRM} = sub { die "TIMEOUT\n" };
174                         alarm(1);
175                         $bib = $ses->request( "open-ils.cstore.direct.$type.record_entry.retrieve", $i, $flesh )->gather(1);
176                         alarm(0);
177                 } otherwise {
178                         warn "\n!!!!!! Timed out trying to read record $i\n";
179                 };
180                 alarm(0);
181
182                 next unless $bib;
183
184                 if (uc($format) eq 'BRE') {
185                         $r->print( OpenSRF::Utils::JSON->perl2JSON($bib) );
186                         next;
187                 }
188
189                 try {
190
191                         my $req = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
192                         $req->delete_field( $_ ) for ($req->field(901));
193
194                         $req->append_fields(
195                                 MARC::Field->new(
196                                         901, '', '', 
197                                         a => $bib->$tcn_v,
198                                         b => $bib->$tcn_s,
199                                         c => $bib->id
200                                 )
201                         );
202
203
204                         if ($holdings) {
205                                 my $cn_list = $bib->call_numbers;
206                                 if ($cn_list && @$cn_list) {
207
208                                         my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
209                                         if ($cp_list && @$cp_list) {
210
211                                                 my %cn_map;
212                                                 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
213                                         
214                                                 for my $cn ( @$cn_list ) {
215                                                         my $cn_map_list = $cn_map{$cn->id};
216         
217                                                         for my $cp ( @$cn_map_list ) {
218                                         
219                                                                 $req->append_fields(
220                                                                         MARC::Field->new(
221                                                                                 852, '4', '', 
222                                                                                 a => $location,
223                                                                                 b => $orgs{$cn->owning_lib}->shortname,
224                                                                                 b => $orgs{$cp->circ_lib}->shortname,
225                                                                                 c => $shelves{$cp->location}->name,
226                                                                                 j => $cn->label,
227                                                                                 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
228                                                                                 p => $cp->barcode,
229                                                                                 ($cp->price ? ( y => $cp->price ) : ()),
230                                                                                 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
231                                                                                 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
232                                                                                 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
233                                                                                 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
234                                                                                 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
235                                                                         )
236                                                                 );
237
238                                                         }
239                                                 }
240                                         }
241                                 }
242                         }
243
244                         if (uc($format) eq 'XML') {
245                                 my $x = $req->as_xml_record;
246                                 $x =~ s/^<\?xml version="1.0" encoding="UTF-8"\?>//o;
247                                 $r->print($x);
248                         } elsif (uc($format) eq 'UNIMARC') {
249                                 $r->print($req->as_unimarc);
250                         } elsif (uc($format) eq 'USMARC') {
251                                 $r->print($req->as_usmarc);
252                         }
253
254             $r->rflush();
255
256                 } otherwise {
257                         my $e = shift;
258                         warn "\n$e\n";
259                 };
260
261         }
262
263         $r->print("</collection>\n") if ($format eq 'XML');
264
265         return Apache2::Const::OK;
266
267 }
268
269 sub show_template {
270         my $r = shift;
271
272         $r->content_type('text/html');
273         $r->print(<<HTML);
274
275 <html>
276         <head>
277                 <title>Record Export</title>
278         </head>
279         <body>
280                 <form method="POST" enctype="multipart/form-data">
281                         Use field number <input type="text" size="2" maxlength="2" name="idcolumn" value="0"/> (starting from 0)
282                         from CSV file <input type="file" name="idfile"/>
283                         <br/><br/> <b>or</b> <br/><br/>
284                         Record ID <input type="text" size="12" maxlength="12" name="id"/>
285                         <br/><br/> Record Type:
286                         <select name="rectype">
287                                 <option value="biblio">Bibliographic Records</option>
288                                 <option value="authority">Authority Records</option>
289                         </select>
290                         <br/> Record Fromat:
291                         <select name="format">
292                                 <option value="USMARC">MARC21</option>
293                                 <option value="UNIMARC">UNIMARC</option>
294                                 <option value="XML">MARC XML</option>
295                                 <option value="BRE">Evergreen BRE</option>
296                         </select>
297                         <br/> Record Encoding:
298                         <select name="encoding">
299                                 <option value="UTF-8">UTF-8</option>
300                                 <option value="MARC8">MARC8</option>
301                         </select>
302                         <br/> Include holdings in Bibliographic Records:
303                         <input type="checkbox" name="holdings" value="1">
304                         <br/><br/><input type="submit" value="Retrieve Records"/>
305                 </form>
306         </body>
307 </html>
308
309 HTML
310
311         return Apache2::Const::OK;
312 }
313
314 1;