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;
69 my @data = $csv->fields;
70 push @records, $data[$col];
75 if (!@records) { # try pathinfo
76 my $path_rec = $cgi->path_info();
78 @records = map { $_ ? ($_) : () } split '/', $path_rec;
82 return show_template($r) unless (@records);
84 my $type = $cgi->param('rectype') || 'biblio';
85 if ($type ne 'biblio' && $type ne 'authority') {
86 die "Bad record type: $type";
89 my $tcn_v = 'tcn_value';
90 my $tcn_s = 'tcn_source';
92 if ($type eq 'authority') {
94 $tcn_s = 'arn_source';
97 my $holdings = $cgi->param('holdings') if ($type eq 'biblio');
98 my $location = $cgi->param('location') || 'gaaagpl'; # just because...
100 my $format = $cgi->param('format') || 'USMARC';
101 $format = uc($format);
103 my $encoding = $cgi->param('encoding') || 'UTF-8';
104 $encoding = uc($encoding);
106 my $filename = $cgi->param('filename') || "export.$type.$encoding.$format";
108 binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
109 binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
111 if (!grep { uc($format) eq $_ } @formats) {
112 die "Please select a supported format. ".
113 "Right now that means one of [".
114 join('|',@formats). "]\n";
117 if ($format ne 'XML') {
118 my $ftype = 'MARC::File::' . $format;
122 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
124 $r->headers_out->set("Content-Disposition" => "inline; filename=$filename");
126 if (uc($format) eq 'XML') {
127 $r->content_type('application/xml');
129 $r->content_type('application/octet-stream');
132 $r->print( <<" HEADER" ) if (uc($format) eq 'XML');
133 <?xml version="1.0" encoding="$encoding"?>
134 <collection xmlns='http://www.loc.gov/MARC21/slim'>
143 my $req = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
145 while (my $o = $req->recv) {
146 die $req->failed->stringify if ($req->failed);
153 $req = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
155 while (my $s = $req->recv) {
156 die $req->failed->stringify if ($req->failed);
159 $shelves{$s->id} = $s;
163 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
166 for my $i ( @records ) {
169 local $SIG{ALRM} = sub { die "TIMEOUT\n" };
171 $bib = $ses->request( "open-ils.cstore.direct.$type.record_entry.retrieve", $i, $flesh )->gather(1);
174 warn "\n!!!!!! Timed out trying to read record $i\n";
180 if (uc($format) eq 'BRE') {
181 $r->print( OpenSRF::Utils::JSON->perl2JSON($bib) );
187 my $req = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
188 $req->delete_field( $_ ) for ($req->field(901));
201 my $cn_list = $bib->call_numbers;
202 if ($cn_list && @$cn_list) {
204 my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
205 if ($cp_list && @$cp_list) {
208 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
210 for my $cn ( @$cn_list ) {
211 my $cn_map_list = $cn_map{$cn->id};
213 for my $cp ( @$cn_map_list ) {
219 b => $orgs{$cn->owning_lib}->shortname,
220 b => $orgs{$cp->circ_lib}->shortname,
221 c => $shelves{$cp->location}->name,
223 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
225 ($cp->price ? ( y => $cp->price ) : ()),
226 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
227 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
228 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
229 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
230 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
240 if (uc($format) eq 'XML') {
241 my $x = $req->as_xml_record;
242 $x =~ s/^<\?xml version="1.0" encoding="UTF-8"\?>//o;
244 } elsif (uc($format) eq 'UNIMARC') {
245 $r->print($req->as_unimarc);
246 } elsif (uc($format) eq 'USMARC') {
247 $r->print($req->as_usmarc);
257 $r->print("</collection>\n") if ($format eq 'XML');
259 return Apache2::Const::OK;
266 $r->content_type('text/html');
271 <title>Record Export</title>
274 <form method="POST" enctype="multipart/form-data">
275 Use field number <input type="text" size="2" maxlength="2" name="idcolumn" value="0"/> (starting from 0)
276 from CSV file <input type="file" name="idfile"/>
277 <br/><br/> <b>or</b> <br/><br/>
278 Record ID <input type="text" size="12" maxlength="12" name="id"/>
279 <br/><br/> Record Type:
281 <option value="biblio">Bibliographic Records</option>
282 <option value="authority">Authority Records</option>
284 <br/><br/> Record Fromat:
285 <select name="format">
286 <option value="USMARC">MARC21</option>
287 <option value="UNIMARC">UNIMARC</option>
288 <option value="XML">MARC XML</option>
289 <option value="BRE">Evergreen BRE</option>
291 <br/><br/> Record Encoding:
292 <select name="encoding">
293 <option value="UTF-8">UTF-8</option>
294 <option value="MARC8">MARC8</option>
296 <br/><br/> Include holdings in Bibliographic Records:
297 <input type="checkbox" name="holdings" value="1">
298 <br/><input type="submit" value="Retrieve Records"/>
305 return Apache2::Const::OK;