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

(-)a/misc/export2collectionhq.pl (-1 / +135 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
my $result = GetOptions(
49
    'h|help'         => \$want_help
50
);
51
52
binmode( STDOUT, ":utf8" );
53
54
if ( not $result or $want_help ) {
55
    usage();
56
}
57
58
unless ( chdir '/tmp' ) {
59
    croak 'Unable to cd to /tmp';
60
}
61
62
my $filename = get_filename();
63
64
write_file($filename);
65
66
transfer_file($filename);
67
68
sub write_file {
69
    my $export_filename = shift;
70
    my $dbh             = C4::Context->dbh;
71
72
    my $query =
73
      'SELECT biblioitems.biblionumber FROM biblioitems WHERE biblionumber >0 ';
74
75
    my $sth = $dbh->prepare($query);
76
    $sth->execute();
77
78
    open my $fh, '>:encoding(utf8)', $export_filename
79
      or croak "Cannot open $export_filename : $!";
80
81
    while ( my ($biblionumber) = $sth->fetchrow_array ) {
82
        #my $marc_record = GetMarcBiblio($biblionumber,1);
83
        my $marc_record = GetMarcBiblio( { biblionumber => $biblionumber,embed_items =>1 } );
84
        if ($marc_record) {
85
            print {$fh} $marc_record->as_usmarc();
86
        }
87
    }
88
    if ( !close $fh ) {
89
        croak "Writing to $export_filename failed on close";
90
    }
91
    return;
92
}
93
94
sub get_filename {
95
    my @abbr  = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
96
    my @tm    = localtime();
97
    my $month = $tm[4];
98
    my $year  = $tm[5] - 100;
99
    my $name  = 'export' . $abbr[$month] . $year;
100
    $name .= '.mrc';
101
102
    # will need to add a directory & unique date id to file
103
    return $name;
104
}
105
106
sub transfer_file {
107
    my $marc_file = shift;
108
    my $remote    = 'ftp.emea.collectionhq.com';
109
    my $username  = q{INSERT USERNAME HERE};
110
    my $password = q{INSERT PASSWORD HERE};
111
112
    my $ftp = Net::FTP->new( $remote, Debug => 0 )
113
      or croak "Cannot connect to CollectionHQ $@";
114
115
    $ftp->login( $username, $password )
116
      or croak 'Cannot login to CollectionHQ ', $ftp->message;
117
    $ftp->binary();
118
    $ftp->put($marc_file);
119
120
    $ftp->quit;
121
122
    return;
123
}
124
125
=head1 NAME
126
127
export2collectionhq.pl
128
129
=head1 SYNOPSIS
130
131
  export2collectionhq.pl
132
133
=head1 DESCRIPTION
134
135
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