View | Details | Raw Unified | Return to bug 25252
Collapse All | Expand All

(-)a/misc/export2collectionhq.pl (-1 / +131 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
#
4
# Copyright 2012 PTFS Europe Ltd
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along with
17
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18
# Suite 330, Boston, MA  02111-1307 USA
19
20
use strict;
21
use warnings;
22
use FindBin;
23
use lib "$FindBin::Bin/../..";
24
use Carp;
25
use C4::Context;
26
use C4::Auth;
27
use C4::Output;
28
use C4::Biblio;    # GetMarcBiblio GetXmlBiblio
29
use C4::Koha;      # GetItemTypes
30
use Net::FTP;
31
use Getopt::Long;
32
use Pod::Usage;
33
34
sub usage {
35
    pod2usage( -verbose => 2 );
36
    exit;
37
}
38
39
$| = 1;
40
41
# command-line parameters
42
 my $want_help   = 0;
43
44
my $result = GetOptions(
45
    'h|help'         => \$want_help
46
);
47
48
binmode( STDOUT, ":encoding(UTF-8)" );
49
50
if ( not $result or $want_help ) {
51
    usage();
52
}
53
54
unless ( chdir '/tmp' ) {
55
    croak 'Unable to cd to /tmp';
56
}
57
58
my $filename = get_filename();
59
60
write_file($filename);
61
62
transfer_file($filename);
63
64
sub write_file {
65
    my $export_filename = shift;
66
    my $dbh             = C4::Context->dbh;
67
68
    my $query =
69
      'SELECT biblioitems.biblionumber FROM biblioitems WHERE biblionumber >0 ';
70
71
    my $sth = $dbh->prepare($query);
72
    $sth->execute();
73
74
    open my $fh, '>:encoding(utf8)', $export_filename
75
      or croak "Cannot open $export_filename : $!";
76
77
    while ( my ($biblionumber) = $sth->fetchrow_array ) {
78
        #my $marc_record = GetMarcBiblio($biblionumber,1);
79
        my $marc_record = GetMarcBiblio( { biblionumber => $biblionumber,embed_items =>1 } );
80
        if ($marc_record) {
81
            print {$fh} $marc_record->as_usmarc();
82
        }
83
    }
84
    if ( !close $fh ) {
85
        croak "Writing to $export_filename failed on close";
86
    }
87
    return;
88
}
89
90
sub get_filename {
91
    my @abbr  = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
92
    my @tm    = localtime();
93
    my $month = $tm[4];
94
    my $year  = $tm[5] - 100;
95
    my $name  = 'export' . $abbr[$month] . $year;
96
    $name .= '.mrc';
97
98
    # will need to add a directory & unique date id to file
99
    return $name;
100
}
101
102
sub transfer_file {
103
    my $marc_file = shift;
104
    my $remote    = 'ftp.emea.collectionhq.com';
105
    my $username  = q{INSERT USERNAME HERE};
106
    my $password = q{INSERT PASSWORD HERE};
107
108
    my $ftp = Net::FTP->new( $remote, Debug => 0 )
109
      or croak "Cannot connect to CollectionHQ $@";
110
111
    $ftp->login( $username, $password )
112
      or croak 'Cannot login to CollectionHQ ', $ftp->message;
113
    $ftp->binary();
114
    $ftp->put($marc_file);
115
116
    $ftp->quit;
117
118
    return;
119
}
120
121
=head1 NAME
122
123
export2collectionhq.pl
124
125
=head1 SYNOPSIS
126
127
  export2collectionhq.pl
128
129
=head1 DESCRIPTION
130
131
This script exports bibliographic data to CollectionHQ. It is imperative that the username and password are all edited in the script for successful transfer to CollectionHQ.

Return to bug 25252