1 package OpenILS::WWW::Exporter;
7 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
8 use APR::Const -compile => qw(:error SUCCESS);
11 use Apache2::RequestRec ();
12 use Apache2::RequestIO ();
13 use Apache2::RequestUtil;
18 use OpenSRF::EX qw(:try);
19 use OpenSRF::Utils qw/:datetime/;
20 use OpenSRF::Utils::Cache;
22 use OpenSRF::AppSession;
27 use Unicode::Normalize;
28 use OpenILS::Utils::Fieldmapper;
29 use OpenSRF::Utils::Logger qw/$logger/;
34 use UNIVERSAL::require;
36 our @formats = qw/USMARC UNIMARC XML BRE/;
38 # set the bootstrap config and template include directory when
39 # this module is loaded
49 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
59 @records = $cgi->param('id');
61 if (!@records) { # try for a file
62 my $file = $cgi->param('idfile');
64 my $col = $cgi->param('idcolumn') || 0;
65 my $csv = new Text::CSV;
70 my @data = $csv->fields;
80 if (!@records) { # try pathinfo
81 my $path_rec = $cgi->path_info();
83 @records = map { $_ ? ($_) : () } split '/', $path_rec;
87 return show_template($r) unless (@records);
89 my $type = $cgi->param('rectype') || 'biblio';
90 if ($type ne 'biblio' && $type ne 'authority') {
91 die "Bad record type: $type";
94 my $tcn_v = 'tcn_value';
95 my $tcn_s = 'tcn_source';
97 if ($type eq 'authority') {
99 $tcn_s = 'arn_source';
102 my $holdings = $cgi->param('holdings') if ($type eq 'biblio');
103 my $location = $cgi->param('location') || 'gaaagpl'; # just because...
105 my $format = $cgi->param('format') || 'USMARC';
106 $format = uc($format);
108 my $encoding = $cgi->param('encoding') || 'UTF-8';
109 $encoding = uc($encoding);
111 my $filename = $cgi->param('filename') || "export.$type.$encoding.$format";
113 binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
114 binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
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";
122 if ($format ne 'XML') {
123 my $ftype = 'MARC::File::' . $format;
127 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
129 $r->headers_out->set("Content-Disposition" => "inline; filename=$filename");
131 if (uc($format) eq 'XML') {
132 $r->content_type('application/xml');
134 $r->content_type('application/octet-stream');
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'>
148 my $req = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
150 while (my $o = $req->recv) {
151 die $req->failed->stringify if ($req->failed);
158 $req = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
160 while (my $s = $req->recv) {
161 die $req->failed->stringify if ($req->failed);
164 $shelves{$s->id} = $s;
168 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
171 for my $i ( @records ) {
174 local $SIG{ALRM} = sub { die "TIMEOUT\n" };
176 $bib = $ses->request( "open-ils.cstore.direct.$type.record_entry.retrieve", $i, $flesh )->gather(1);
179 warn "\n!!!!!! Timed out trying to read record $i\n";
185 if (uc($format) eq 'BRE') {
186 $r->print( OpenSRF::Utils::JSON->perl2JSON($bib) );
192 my $req = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
193 $req->delete_field( $_ ) for ($req->field(901));
206 my $cn_list = $bib->call_numbers;
207 if ($cn_list && @$cn_list) {
209 my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
210 if ($cp_list && @$cp_list) {
213 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
215 for my $cn ( @$cn_list ) {
216 my $cn_map_list = $cn_map{$cn->id};
218 for my $cp ( @$cn_map_list ) {
224 b => $orgs{$cn->owning_lib}->shortname,
225 b => $orgs{$cp->circ_lib}->shortname,
226 c => $shelves{$cp->location}->name,
228 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
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' ) : ()),
245 if (uc($format) eq 'XML') {
246 my $x = $req->as_xml_record;
247 $x =~ s/^<\?xml version="1.0" encoding="UTF-8"\?>//o;
249 } elsif (uc($format) eq 'UNIMARC') {
250 $r->print($req->as_unimarc);
251 } elsif (uc($format) eq 'USMARC') {
252 $r->print($req->as_usmarc);
262 $r->print("</collection>\n") if ($format eq 'XML');
264 return Apache2::Const::OK;
271 $r->content_type('text/html');
276 <title>Record Export</title>
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:
286 <option value="biblio">Bibliographic Records</option>
287 <option value="authority">Authority Records</option>
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>
296 <br/><br/> Record Encoding:
297 <select name="encoding">
298 <option value="UTF-8">UTF-8</option>
299 <option value="MARC8">MARC8</option>
301 <br/><br/> Include holdings in Bibliographic Records:
302 <input type="checkbox" name="holdings" value="1">
303 <br/><input type="submit" value="Retrieve Records"/>
310 return Apache2::Const::OK;