package OpenILS::WWW::Exporter;
use strict;
use warnings;
use bytes;
use Apache2::Log;
use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
use APR::Const -compile => qw(:error SUCCESS);
use APR::Table;
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::RequestUtil;
use CGI;
use Data::Dumper;
use Text::CSV;
use OpenSRF::EX qw(:try);
use OpenSRF::Utils qw/:datetime/;
use OpenSRF::Utils::Cache;
use OpenSRF::System;
use OpenSRF::AppSession;
use XML::LibXML;
use XML::LibXSLT;
use Encode;
use Unicode::Normalize;
use OpenILS::Utils::Fieldmapper;
use OpenSRF::Utils::Logger qw/$logger/;
use MARC::Record;
use MARC::File::XML;
use UNIVERSAL::require;
our @formats = qw/USMARC UNIMARC XML BRE/;
# set the bootstrap config and template include directory when
# this module is loaded
my $bootstrap;
sub import {
my $self = shift;
$bootstrap = shift;
}
sub child_init {
OpenSRF::System->bootstrap_client( config_file => $bootstrap );
}
sub handler {
my $r = shift;
my $cgi = new CGI;
# find some IDs ...
my @records;
@records = map { $_ ? ($_) : () } $cgi->param('id');
if (!@records) { # try for a file
my $file = $cgi->param('idfile');
if ($file) {
my $col = $cgi->param('idcolumn') || 0;
my $csv = new Text::CSV;
while (<$file>) {
chomp;
$csv->parse($_);
my @data = $csv->fields;
my $id = $data[$col];
$id =~ s/\D+//o;
next unless ($id);
push @records, $id;
}
}
}
if (!@records) { # try pathinfo
my $path_rec = $cgi->path_info();
if ($path_rec) {
@records = map { $_ ? ($_) : () } split '/', $path_rec;
}
}
my $ses = OpenSRF::AppSession->create('open-ils.cstore');
# still no records ...
my $container = $cgi->param('containerid');
if ($container) {
my $bucket = $ses->request( 'open-ils.cstore.direct.container.biblio_record_entry_bucket.retrieve', $container )->gather(1);
if ($bucket->pub !~ /t|1/oi) {
my $authid = $cgi->cookie('ses') || $cgi->param('ses');
my $auth = verify_login($authid);
if (!$auth) {
return 403;
}
}
my $recs = $ses->request( 'open-ils.cstore.direct.container.biblio_record_entry_bucket_item.search.atomic', { bucket => $container } )->gather(1);
@records = map { ($_->target_biblio_record_entry) } @$recs;
}
return show_template($r) unless (@records);
my $type = $cgi->param('rectype') || 'biblio';
if ($type ne 'biblio' && $type ne 'authority') {
return 400;
}
my $tcn_v = 'tcn_value';
my $tcn_s = 'tcn_source';
if ($type eq 'authority') {
$tcn_v = 'arn_value';
$tcn_s = 'arn_source';
}
my $holdings = $cgi->param('holdings') if ($type eq 'biblio');
my $location = $cgi->param('location') || 'gaaagpl'; # just because...
my $format = $cgi->param('format') || 'USMARC';
$format = uc($format);
my $encoding = $cgi->param('encoding') || 'UTF-8';
$encoding = uc($encoding);
my $filename = $cgi->param('filename') || "export.$type.$encoding.$format";
binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
if (!grep { uc($format) eq $_ } @formats) {
return 400;
}
if ($format ne 'XML') {
my $ftype = 'MARC::File::' . $format;
$ftype->require;
}
$r->headers_out->set("Content-Disposition" => "inline; filename=$filename");
if (uc($format) eq 'XML') {
$r->content_type('application/xml');
} else {
$r->content_type('application/octet-stream');
}
$r->print( <<" HEADER" ) if (uc($format) eq 'XML');