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');
64 my $col = $cgi->param('idcolumn') || 0;
65 my $csv = new Text::CSV;
70 my @data = $csv->fields;
79 if (!@records) { # try pathinfo
80 my $path_rec = $cgi->path_info();
82 @records = map { $_ ? ($_) : () } split '/', $path_rec;
86 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
88 # still no records ...
89 my $container = $cgi->param('containerid');
91 my $authid = $cgi->cookie('ses') || $cgi->param('ses');
92 my $auth = verify_login($authid);
96 my $recs = $ses->request( 'open-ils.cstore.direct.container.biblio_record_entry_bucket_item.search.atomic', { bucket => $container } )->gather(1);
97 @records = map { ($_->target_biblio_record_entry) } @$recs;
100 return show_template($r) unless (@records);
102 my $type = $cgi->param('rectype') || 'biblio';
103 if ($type ne 'biblio' && $type ne 'authority') {
107 my $tcn_v = 'tcn_value';
108 my $tcn_s = 'tcn_source';
110 if ($type eq 'authority') {
111 $tcn_v = 'arn_value';
112 $tcn_s = 'arn_source';
115 my $holdings = $cgi->param('holdings') if ($type eq 'biblio');
116 my $location = $cgi->param('location') || 'gaaagpl'; # just because...
118 my $format = $cgi->param('format') || 'USMARC';
119 $format = uc($format);
121 my $encoding = $cgi->param('encoding') || 'UTF-8';
122 $encoding = uc($encoding);
124 my $filename = $cgi->param('filename') || "export.$type.$encoding.$format";
126 binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
127 binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
129 if (!grep { uc($format) eq $_ } @formats) {
133 if ($format ne 'XML') {
134 my $ftype = 'MARC::File::' . $format;
139 $r->headers_out->set("Content-Disposition" => "inline; filename=$filename");
141 if (uc($format) eq 'XML') {
142 $r->content_type('application/xml');
144 $r->content_type('application/octet-stream');
147 $r->print( <<" HEADER" ) if (uc($format) eq 'XML');
148 <?xml version="1.0" encoding="$encoding"?>
149 <collection xmlns='http://www.loc.gov/MARC21/slim'>
158 my $req = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
160 while (my $o = $req->recv) {
161 next if ($req->failed);
168 $req = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
170 while (my $s = $req->recv) {
171 next if ($req->failed);
174 $shelves{$s->id} = $s;
178 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
181 for my $i ( @records ) {
184 local $SIG{ALRM} = sub { die "TIMEOUT\n" };
186 $bib = $ses->request( "open-ils.cstore.direct.$type.record_entry.retrieve", $i, $flesh )->gather(1);
189 warn "\n!!!!!! Timed out trying to read record $i\n";
195 if (uc($format) eq 'BRE') {
196 $r->print( OpenSRF::Utils::JSON->perl2JSON($bib) );
202 my $req = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
203 $req->delete_field( $_ ) for ($req->field(901));
216 my $cn_list = $bib->call_numbers;
217 if ($cn_list && @$cn_list) {
219 my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
220 if ($cp_list && @$cp_list) {
223 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
225 for my $cn ( @$cn_list ) {
226 my $cn_map_list = $cn_map{$cn->id};
228 for my $cp ( @$cn_map_list ) {
234 b => $orgs{$cn->owning_lib}->shortname,
235 b => $orgs{$cp->circ_lib}->shortname,
236 c => $shelves{$cp->location}->name,
238 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
240 ($cp->price ? ( y => $cp->price ) : ()),
241 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
242 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
243 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
244 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
245 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
255 if (uc($format) eq 'XML') {
256 my $x = $req->as_xml_record;
257 $x =~ s/^<\?xml version="1.0" encoding="UTF-8"\?>//o;
259 } elsif (uc($format) eq 'UNIMARC') {
260 $r->print($req->as_unimarc);
261 } elsif (uc($format) eq 'USMARC') {
262 $r->print($req->as_usmarc);
274 $r->print("</collection>\n") if ($format eq 'XML');
276 return Apache2::Const::OK;
281 my $auth_token = shift;
282 return undef unless $auth_token;
284 my $user = OpenSRF::AppSession
285 ->create("open-ils.auth")
286 ->request( "open-ils.auth.session.retrieve", $auth_token )
289 if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
293 return $user if ref($user);
300 $r->content_type('text/html');
305 <title>Record Export</title>
308 <form method="POST" enctype="multipart/form-data">
309 Use field number <input type="text" size="2" maxlength="2" name="idcolumn" value="0"/> (starting from 0)
310 from CSV file <input type="file" name="idfile"/>
311 <br/><br/> <b>or</b> <br/><br/>
312 Record ID <input type="text" size="12" maxlength="12" name="id"/>
313 <br/><br/> Record Type:
314 <select name="rectype">
315 <option value="biblio">Bibliographic Records</option>
316 <option value="authority">Authority Records</option>
319 <select name="format">
320 <option value="USMARC">MARC21</option>
321 <option value="UNIMARC">UNIMARC</option>
322 <option value="XML">MARC XML</option>
323 <option value="BRE">Evergreen BRE</option>
325 <br/> Record Encoding:
326 <select name="encoding">
327 <option value="UTF-8">UTF-8</option>
328 <option value="MARC8">MARC8</option>
330 <br/> Include holdings in Bibliographic Records:
331 <input type="checkbox" name="holdings" value="1">
332 <br/><br/><input type="submit" value="Retrieve Records"/>
339 return Apache2::Const::OK;