Merge branch 'new/bug_6328'
[koha.git] / misc / stage_biblios_file.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2007 LibLime
6 # Parts Copyright BSZ 2011
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23 BEGIN {
24     # find Koha's Perl modules
25     # test carefully before changing this
26     use FindBin;
27     eval { require "$FindBin::Bin/kohalib.pl" };
28 }
29
30 use C4::Context;
31 use C4::ImportBatch;
32 use C4::Matcher;
33 use Getopt::Long;
34
35 $| = 1;
36
37 # command-line parameters
38 my $encoding = "";
39 my $match_bibs = 0;
40 my $add_items = 0;
41 my $input_file = "";
42 my $batch_comment = "";
43 my $want_help = 0;
44 my $no_replace ;
45
46 my $result = GetOptions(
47     'encoding:s'    => \$encoding,
48     'file:s'        => \$input_file,
49     'match-bibs:s'  => \$match_bibs,
50     'add-items'     => \$add_items,
51     'no-replace'    => \$no_replace,
52     'comment:s'     => \$batch_comment,
53     'h|help'        => \$want_help
54 );
55
56 if ($encoding eq "") {
57     $encoding = "utf8";
58 }
59
60 if (not $result or $input_file eq "" or $want_help) {
61     print_usage();
62     exit 0;
63 }
64
65 unless (-r $input_file) {
66     die "$0: cannot open input file $input_file: $!\n";
67 }
68
69 my $dbh = C4::Context->dbh;
70 $dbh->{AutoCommit} = 0;
71 process_batch($input_file, $match_bibs, $add_items, $batch_comment);
72 $dbh->commit();
73
74 exit 0;
75
76 sub process_batch {
77     my ($input_file, $match_bibs, $add_items, $batch_comment) = @_;
78
79     open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
80     my $marc_records = "";
81     $/ = "\035";
82     my $num_input_records = 0;
83     while (<IN>) {
84         s/^\s+//;
85         s/\s+$//;
86         next unless $_; # skip if record has only whitespace, as might occur
87                         # if file includes newlines between each MARC record
88         $marc_records .= $_; # FIXME - this sort of string concatenation
89                              # is probably rather inefficient
90         $num_input_records++;
91     }
92     close IN;
93
94     print "... staging MARC records -- please wait\n";
95     my ($batch_id, $num_valid, $num_items, @import_errors) = 
96         BatchStageMarcRecords($encoding, $marc_records, $input_file, $batch_comment, '', $add_items, 0,
97                               100, \&print_progress_and_commit);
98     print "... finished staging MARC records\n";
99
100     my $num_with_matches = 0;
101     if ($match_bibs) {
102         my $matcher = C4::Matcher->fetch($match_bibs) ;
103         if (! defined $matcher) {
104             $matcher = C4::Matcher->new('biblio');
105             $matcher->add_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, '');
106             $matcher->add_simple_required_check('245', 'a', -1, 0, '', 
107                                             '245', 'a', -1, 0, '');
108         } else {
109             SetImportBatchMatcher($batch_id, $match_bibs);
110         }
111         # set default record overlay behavior
112         SetImportBatchOverlayAction($batch_id, ($no_replace) ? 'ignore' : 'replace');
113         SetImportBatchNoMatchAction($batch_id, 'create_new');
114         SetImportBatchItemAction($batch_id, 'always_add');
115         print "... looking for matches with records already in database\n";
116         $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, 10, 100, \&print_progress_and_commit);
117         print "... finished looking for matches\n";
118     }
119
120     my $num_invalid_bibs = scalar(@import_errors);
121     print <<_SUMMARY_;
122
123 MARC record staging report
124 ------------------------------------
125 Input file:              $input_file
126 Number of input bibs:    $num_input_records
127 Number of valid bibs:    $num_valid
128 Number of invalid bibs:  $num_invalid_bibs
129 _SUMMARY_
130     if ($match_bibs) {
131         print "Number of bibs matched:  $num_with_matches\n";
132     } else {
133         print "Incoming bibs not matched against existing bibs (--match-bibs option not supplied)\n";
134     }
135     if ($add_items) {
136         print "Number of items parsed:  $num_items\n";
137     } else {
138         print "No items parsed (--add-items option not supplied)\n";
139     }
140
141     print "\n";
142     print "Batch number assigned:  $batch_id\n";
143     print "\n";
144 }
145
146 sub print_progress_and_commit {
147     my $recs = shift;
148     $dbh->commit();
149     print "... processed $recs records\n";
150 }
151
152 sub print_usage {
153     print <<_USAGE_;
154 $0: stage MARC bib file into reservoir.
155
156 Use this batch job to load a file of MARC bibliographic records
157 (with optional item information) into the Koha reservoir.
158
159 After running this program to stage your file, you can use
160 either the batch job commit_biblios_file.pl or the Koha
161 Tools option "Manage Staged MARC Records" to load the
162 records into the main Koha database.
163
164 Parameters:
165     --file <file_name>      name of input MARC bib file
166     --encoding <encoding>   encoding of MARC records, default is utf8.
167                             Other possible options are: MARC-8,
168                             ISO_5426, ISO_6937, ISO_8859-1, EUC-KR
169     --match-bibs <match_id> use this option to match bibs
170                             in the file with bibs already in 
171                             the database for future overlay.
172                             If <match_id> isn't defined, a default 
173                             MARC21 ISBN & title match rule will be applied.
174     --add-items             use this option to specify that
175                             item data is embedded in the MARC
176                             bibs and should be parsed.
177     --no-replace            overlay action for bib record: default is to 
178                             replace extant bib with the imported record.
179     --comment <comment>     optional comment to describe
180                             the record batch; if the comment
181                             has spaces in it, surround the
182                             comment with quotation marks.
183     --help or -h            show this message.
184 _USAGE_
185 }