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