From e977e049ba15324ae29f1f6dbdcbc1cbe53b7ab9 Mon Sep 17 00:00:00 2001
From: David Roberts <david@koha-ptfs.co.uk>
Date: Wed, 22 Apr 2020 11:07:07 +0000
Subject: [PATCH] Bug 25246: Add script to export bib data to EBSCO EDS

This script adds the capability to export bibliographic data to EBSCO
EDS via a cronjob.

To test:

1) Apply the patch
2) Edit the file so that line 73 contains a file name.
3) Run the script and a file should be created in /tmp
4) If you have a valid username and password for the EBSCO EDS ftp
server, add these into lines 84 and 85 and run the script again to
ensure that it transfers correctly.
---
 misc/export2ebsco.pl | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)
 create mode 100755 misc/export2ebsco.pl

diff --git a/misc/export2ebsco.pl b/misc/export2ebsco.pl
new file mode 100755
index 0000000000..f5ca56fb77
--- /dev/null
+++ b/misc/export2ebsco.pl
@@ -0,0 +1,136 @@
+#!/usr/bin/env perl
+
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+# Suite 330, Boston, MA  02111-1307 USA
+
+use strict;
+use warnings;
+use FindBin;
+use lib "$FindBin::Bin/../..";
+use Carp;
+use C4::Context;
+use C4::Auth;
+use C4::Output;
+use C4::Biblio;    # GetMarcBiblio GetXmlBiblio
+use C4::Koha;      # GetItemTypes
+use Net::FTP;
+use Getopt::Long;
+use Pod::Usage;
+
+sub usage {
+    pod2usage( -verbose => 2 );
+    exit;
+}
+	
+$| = 1;
+	
+# command-line parameters
+ my $want_help   = 0;
+#
+ my $result = GetOptions(
+  'h|help'         => \$want_help
+);
+	
+my $result = GetOptions(
+    'h|help'         => \$want_help
+);
+	
+binmode( STDOUT, ":utf8" );
+	
+if ( not $result or $want_help ) {
+    usage();
+}
+
+unless ( chdir '/tmp' ) {
+    croak 'Unable to cd to /tmp';
+}
+
+my $filename = get_filename();
+
+write_file($filename);
+
+transfer_file($filename);
+
+sub write_file {
+    my $export_filename = shift;
+    my $dbh             = C4::Context->dbh;
+
+    my $query =
+#      'SELECT distinct biblioitems.biblionumber FROM biblioitems WHERE biblionumber >0 ';
+       'SELECT distinct biblioitems.biblionumber FROM biblioitems LEFT JOIN items USING (biblioitemnumber) WHERE biblioitems.biblionumber >0 ';
+
+    my $sth = $dbh->prepare($query);
+    $sth->execute();
+
+    open my $fh, '>:encoding(utf8)', $export_filename
+      or croak "Cannot open $export_filename : $!";
+
+    while ( my ($biblionumber) = $sth->fetchrow_array ) {
+#        my $marc_record = GetMarcBiblio($biblionumber, 1);
+         my $marc_record = GetMarcBiblio({ biblionumber => $biblionumber,embed_items => 1});
+        if ($marc_record) {
+            print {$fh} $marc_record->as_usmarc();
+        }
+    }
+    if ( !close $fh ) {
+        croak "Writing to $export_filename failed on close";
+    }
+    return;
+}
+
+sub get_filename {
+    my @abbr  = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
+    my @tm    = localtime();
+    my $month = $tm[4];
+    my $year  = $tm[5] - 100;
+    my $name  = 'INSERT FILE NAME HERE' . $abbr[$month] . $year;
+
+    $name .= '.mrc';
+
+    # will need to add a directory & unique date id to file
+    return $name;
+}
+
+sub transfer_file {
+    my $marc_file = shift;
+    my $remote    = 'ftp.epnet.com';
+    my $username  = q(INSERT USERNAME HERE);
+    my $password = q(INSERT PASSWORD HERE);
+
+    my $ftp = Net::FTP->new( $remote, Passive => 1, Debug => 0 )
+      or croak "Cannot connect to epnet: $@";
+
+  $ftp->login( $username, $password )
+      or croak 'Cannot login to Epnet ', $ftp->message;
+    $ftp->cwd("full")
+          or die "Cannot change working directory ", $ftp->message;
+    $ftp->put($marc_file);
+  
+   $ftp->quit;
+
+    return;
+}
+=head1 NAME
+
+export2ebsco.pl
+
+=head1 SYNOPSIS
+
+  export2ebsco.pl
+
+=head1 DESCRIPTION
+
+This script exports bibliographic data to EBSCO EDS. It is imperative that the ifilename, username and password are all edited in the script for successful transfer to EBSCO. 
-- 
2.11.0