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 = map { $_ ? ($_) : () } $cgi->param('id');
61 if (!@records) { # try for a file
62 my $file = $cgi->param('idfile');
65 my $col = $cgi->param('idcolumn') || 0;
66 my $csv = new Text::CSV;
72 my @data = $csv->fields;
82 if (!@records) { # try pathinfo
83 my $path_rec = $cgi->path_info();
85 @records = map { $_ ? ($_) : () } split '/', $path_rec;
89 return show_template($r) unless (@records);
91 warn "ids: ". join(',',@records);
93 my $type = $cgi->param('rectype') || 'biblio';
94 if ($type ne 'biblio' && $type ne 'authority') {
95 die "Bad record type: $type";
98 my $tcn_v = 'tcn_value';
99 my $tcn_s = 'tcn_source';
101 if ($type eq 'authority') {
102 $tcn_v = 'arn_value';
103 $tcn_s = 'arn_source';
106 my $holdings = $cgi->param('holdings') if ($type eq 'biblio');
107 my $location = $cgi->param('location') || 'gaaagpl'; # just because...
109 my $format = $cgi->param('format') || 'USMARC';
110 $format = uc($format);
112 my $encoding = $cgi->param('encoding') || 'UTF-8';
113 $encoding = uc($encoding);
115 my $filename = $cgi->param('filename') || "export.$type.$encoding.$format";
117 binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
118 binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
120 if (!grep { uc($format) eq $_ } @formats) {
121 die "Please select a supported format. ".
122 "Right now that means one of [".
123 join('|',@formats). "]\n";
126 if ($format ne 'XML') {
127 my $ftype = 'MARC::File::' . $format;
131 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
133 $r->headers_out->set("Content-Disposition" => "inline; filename=$filename");
135 if (uc($format) eq 'XML') {
136 $r->content_type('application/xml');
138 $r->content_type('application/octet-stream');
141 $r->print( <<" HEADER" ) if (uc($format) eq 'XML');
142 <?xml version="1.0" encoding="$encoding"?>
143 <collection xmlns='http://www.loc.gov/MARC21/slim'>
152 my $req = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
154 while (my $o = $req->recv) {
155 die $req->failed->stringify if ($req->failed);
162 $req = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
164 while (my $s = $req->recv) {
165 die $req->failed->stringify if ($req->failed);
168 $shelves{$s->id} = $s;
172 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
175 for my $i ( @records ) {
178 local $SIG{ALRM} = sub { die "TIMEOUT\n" };
180 $bib = $ses->request( "open-ils.cstore.direct.$type.record_entry.retrieve", $i, $flesh )->gather(1);
183 warn "\n!!!!!! Timed out trying to read record $i\n";
189 if (uc($format) eq 'BRE') {
190 $r->print( OpenSRF::Utils::JSON->perl2JSON($bib) );
196 my $req = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
197 $req->delete_field( $_ ) for ($req->field(901));
210 my $cn_list = $bib->call_numbers;
211 if ($cn_list && @$cn_list) {
213 my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
214 if ($cp_list && @$cp_list) {
217 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
219 for my $cn ( @$cn_list ) {
220 my $cn_map_list = $cn_map{$cn->id};
222 for my $cp ( @$cn_map_list ) {
228 b => $orgs{$cn->owning_lib}->shortname,
229 b => $orgs{$cp->circ_lib}->shortname,
230 c => $shelves{$cp->location}->name,
232 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
234 ($cp->price ? ( y => $cp->price ) : ()),
235 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
236 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
237 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
238 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
239 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
249 if (uc($format) eq 'XML') {
250 my $x = $req->as_xml_record;
251 $x =~ s/^<\?xml version="1.0" encoding="UTF-8"\?>//o;
253 } elsif (uc($format) eq 'UNIMARC') {
254 $r->print($req->as_unimarc);
255 } elsif (uc($format) eq 'USMARC') {
256 $r->print($req->as_usmarc);
266 $r->print("</collection>\n") if ($format eq 'XML');
268 return Apache2::Const::OK;
275 $r->content_type('text/html');
280 <title>Record Export</title>
283 <form method="POST" enctype="multipart/form-data">
284 Use field number <input type="text" size="2" maxlength="2" name="idcolumn" value="0"/> (starting from 0)
285 from CSV file <input type="file" name="idfile"/>
286 <br/><br/> <b>or</b> <br/><br/>
287 Record ID <input type="text" size="12" maxlength="12" name="id"/>
288 <br/><br/> Record Type:
290 <option value="biblio">Bibliographic Records</option>
291 <option value="authority">Authority Records</option>
294 <select name="format">
295 <option value="USMARC">MARC21</option>
296 <option value="UNIMARC">UNIMARC</option>
297 <option value="XML">MARC XML</option>
298 <option value="BRE">Evergreen BRE</option>
300 <br/> Record Encoding:
301 <select name="encoding">
302 <option value="UTF-8">UTF-8</option>
303 <option value="MARC8">MARC8</option>
305 <br/> Include holdings in Bibliographic Records:
306 <input type="checkbox" name="holdings" value="1">
307 <br/><br/><input type="submit" value="Retrieve Records"/>
314 return Apache2::Const::OK;