3 # This file is part of Koha.
5 # Copyright (C) 2007 LibLime
6 # Parts Copyright BSZ 2011
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
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.
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.
24 # find Koha's Perl modules
25 # test carefully before changing this
27 eval { require "$FindBin::Bin/kohalib.pl" };
37 # command-line parameters
42 my $batch_comment = "";
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
56 if ($encoding eq "") {
60 if (not $result or $input_file eq "" or $want_help) {
65 unless (-r $input_file) {
66 die "$0: cannot open input file $input_file: $!\n";
69 my $dbh = C4::Context->dbh;
70 $dbh->{AutoCommit} = 0;
71 process_batch($input_file, $match_bibs, $add_items, $batch_comment);
77 my ($input_file, $match_bibs, $add_items, $batch_comment) = @_;
79 open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
80 my $marc_records = "";
82 my $num_input_records = 0;
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
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";
100 my $num_with_matches = 0;
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, '');
109 SetImportBatchMatcher($batch_id, $match_bibs);
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";
120 my $num_invalid_bibs = scalar(@import_errors);
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
131 print "Number of bibs matched: $num_with_matches\n";
133 print "Incoming bibs not matched against existing bibs (--match-bibs option not supplied)\n";
136 print "Number of items parsed: $num_items\n";
138 print "No items parsed (--add-items option not supplied)\n";
142 print "Batch number assigned: $batch_id\n";
146 sub print_progress_and_commit {
149 print "... processed $recs records\n";
154 $0: stage MARC bib file into reservoir.
156 Use this batch job to load a file of MARC bibliographic records
157 (with optional item information) into the Koha reservoir.
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.
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.