From f73daa2b16bc81ab855aadebe4603ec363ec3de1 Mon Sep 17 00:00:00 2001 From: David Roberts Date: Wed, 22 Apr 2020 14:03:29 +0000 Subject: [PATCH] Bug 25252: Add script to export bib data to CollectionHQ This script adds the capability to export bibliographic data to CollectionHQ via a cronjob. To test: 1) Apply the patch 2) Run the script and an export file should be created in /tmp 3) If you have a valid username and password for the CollectionHQ ftp server, add these credentials into lines 83 and 84 and run the script again to ensure it transfers correctly. --- misc/export2collectionhq.pl | 97 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100755 misc/export2collectionhq.pl diff --git a/misc/export2collectionhq.pl b/misc/export2collectionhq.pl new file mode 100755 index 0000000000..a7e7b55ae0 --- /dev/null +++ b/misc/export2collectionhq.pl @@ -0,0 +1,97 @@ +#!/usr/bin/env perl + +# +# Copyright 2012 PTFS Europe Ltd +# 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 2 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; + +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 biblioitems.biblionumber FROM biblioitems WHERE 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 = 'export' . $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.emea.collectionhq.com'; + my $username = q{INSERT USERNAME HERE}; + my $password = q{INSERT PASSWORD HERE}; + + my $ftp = Net::FTP->new( $remote, Debug => 0 ) + or croak "Cannot connect to CollectionHQ $@"; + + $ftp->login( $username, $password ) + or croak 'Cannot login to CollectionHQ ', $ftp->message; + $ftp->binary(); + $ftp->put($marc_file); + + $ftp->quit; + + return; +} -- 2.11.0