3 # This file is part of Koha.
5 # Copyright (C) 2007 LibLime
6 # Parts Copyright BSZ 2011
7 # Parts Copyright C & P Bibliography Services 2012
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
26 use C4::ImportBatch qw( RecordsFromISO2709File RecordsFromMARCXMLFile BatchStageMarcRecords SetImportBatchMatcher SetImportBatchOverlayAction SetImportBatchNoMatchAction SetImportBatchItemAction BatchFindDuplicates );
28 use C4::MarcModificationTemplates qw( GetModificationTemplates );
29 use Getopt::Long qw( GetOptions );
33 # command-line parameters
34 my $record_type = "biblio";
35 my $encoding = "UTF-8";
40 my $batch_comment = "";
43 my $format = 'ISO2709';
45 my $item_action = 'always_add';
46 my $marc_mod_template = '';
47 my $marc_mod_template_id = undef;
49 my $result = GetOptions(
50 'encoding:s' => \$encoding,
51 'file:s' => \$input_file,
52 'format:s' => \$format,
53 'match|match-bibs:s' => \$match,
54 'add-items' => \$add_items,
55 'item-action:s' => \$item_action,
56 'no-replace' => \$no_replace,
57 'no-create' => \$no_create,
58 'comment:s' => \$batch_comment,
59 'authorities' => \$authorities,
60 'marcmodtemplate:s' => \$marc_mod_template,
61 'h|help' => \$want_help
64 if($marc_mod_template ne '') {
65 my @templates = GetModificationTemplates();
66 foreach my $this_template (@templates) {
67 if($this_template->{'name'} eq $marc_mod_template) {
68 if(!defined $marc_mod_template_id) {
69 $marc_mod_template_id = $this_template->{'template_id'};
71 print "WARNING: MARC modification template name " .
72 "'$marc_mod_template' matches multiple templates. " .
73 "Please fix this issue before proceeding.\n";
79 if(!defined $marc_mod_template_id ) {
80 die "Can't locate MARC modification template '$marc_mod_template'\n";
84 $record_type = 'auth' if ($authorities);
86 if (not $result or $input_file eq "" or $want_help) {
90 if ( $format !~ /^(MARCXML|ISO2709)$/i ) {
91 print "\n --format must be MARCXML or ISO2709\n";
96 unless (-r $input_file) {
97 die "$0: cannot open input file $input_file: $!\n";
100 my $dbh = C4::Context->dbh;
101 $dbh->{AutoCommit} = 0;
105 input_file => $input_file,
106 record_type => $record_type,
108 add_items => $add_items,
109 batch_comment => $batch_comment,
110 encoding => $encoding,
111 no_replace => $no_replace,
112 no_create => $no_create,
113 item_action => $item_action,
114 marc_mod_template_id => $marc_mod_template_id,
122 my ( $params ) = @_; #Possible params are: format input_file record_type match add_items batch_comment encoding no_replace no_create item_action
123 my $format = $params->{format} // '';
124 my $record_type = $params->{record_type} // 'biblio';
126 my ( $errors, $marc_records );
127 if( $format eq 'ISO2709' ) {
128 ( $errors, $marc_records ) = C4::ImportBatch::RecordsFromISO2709File(
129 $params->{input_file}, $record_type, $params->{encoding} );
130 } elsif( $format eq 'MARCXML' ) {
131 ( $errors, $marc_records ) = C4::ImportBatch::RecordsFromMARCXMLFile(
132 $params->{input_file}, $params->{encoding} );
134 warn ( join ',', @$errors ) if @$errors;
135 my $num_input_records = ($marc_records) ? scalar(@$marc_records) : 0;
137 print "... staging MARC records -- please wait\n";
138 #FIXME: We should really allow the use of marc modification frameworks and to_marc plugins here if possible
139 my ( $batch_id, $num_valid_records, $num_items, @import_errors ) =
140 BatchStageMarcRecords(
141 $record_type, $params->{encoding},
142 $marc_records, $params->{input_file},
143 $params->{'marc_mod_template_id'}, $params->{batch_comment},
144 '', $params->{add_items},
146 \&print_progress_and_commit
148 print "... finished staging MARC records\n";
150 my $num_with_matches = 0;
151 if ( $params->{match} ) {
152 my $matcher = C4::Matcher->fetch( $params->{match} );
153 if (defined $matcher) {
154 SetImportBatchMatcher( $batch_id, $params->{match} );
155 } elsif ($record_type eq 'biblio') {
156 $matcher = C4::Matcher->new($record_type);
157 $matcher->add_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, '');
158 $matcher->add_simple_required_check('245', 'a', -1, 0, '',
159 '245', 'a', -1, 0, '');
161 # set default record overlay behavior
162 SetImportBatchOverlayAction( $batch_id, $params->{no_replace} ? 'ignore' : 'replace' );
163 SetImportBatchNoMatchAction( $batch_id, $params->{no_create} ? 'ignore' : 'create_new' );
164 SetImportBatchItemAction( $batch_id, $params->{item_action} );
165 print "... looking for matches with records already in database\n";
166 $num_with_matches = BatchFindDuplicates($batch_id, $matcher, 10, 100, \&print_progress_and_commit);
167 print "... finished looking for matches\n";
170 my $num_invalid_records = scalar(@import_errors);
173 MARC record staging report
174 ------------------------------------
175 Input file: $params->{input_file}
176 Record type: $record_type
177 Number of input records: $num_input_records
178 Number of valid records: $num_valid_records
179 Number of invalid records: $num_invalid_records
181 if( $params->{match} ) {
182 print "Number of records matched: $num_with_matches\n";
184 print "Incoming records not matched against existing records (--match option not supplied)\n";
186 if ($record_type eq 'biblio') {
187 if ( $params->{add_items} ) {
188 print "Number of items parsed: $num_items\n";
190 print "No items parsed (--add-items option not supplied)\n";
195 print "Batch number assigned: $batch_id\n";
199 sub print_progress_and_commit {
202 print "... processed $recs records\n";
207 $0: stage MARC file into reservoir.
209 Use this batch job to load a file of MARC bibliographic
210 (with optional item information) or authority records into
213 After running this program to stage your file, you can use
214 either the batch job commit_file.pl or the Koha
215 Tools option "Manage Staged MARC Records" to load the
216 records into the main Koha database.
219 --file <file_name> name of input MARC bib file
220 --authorities stage authority records instead of bibs
221 --encoding <encoding> encoding of MARC records, default is UTF-8.
222 Other possible options are: MARC-8,
223 ISO_5426, ISO_6937, ISO_8859-1, EUC-KR
224 --format The MARC transport format to use?
226 Available values, MARCXML, ISO2709.
227 --match <match_id> use this option to match records
228 in the file with records already in
229 the database for future overlay.
230 If <match_id> isn't defined, a default
231 MARC21 ISBN & title match rule will be applied
233 --add-items use this option to specify that
234 item data is embedded in the MARC
235 bibs and should be parsed.
236 --item-action action to take if --add-items is specifed;
237 choices are 'always_add',
238 'add_only_for_matches', 'add_only_for_new',
239 'ignore', or 'replace'
240 --no-replace overlay action for record: default is to
241 replace extant with the imported record.
242 --no-create nomatch action for record: default is to
243 create new record with imported record.
244 --comment <comment> optional comment to describe
245 the record batch; if the comment
246 has spaces in it, surround the
247 comment with quotation marks.
248 --marcmodtemplate <TEMPLATE>
249 This parameter allows you to specify the
250 name of an existing MARC modification
251 template to apply as the MARC records are
252 imported (these templates are created in
253 the "MARC modification templates" tool in
254 Koha). If not specified, no MARC modification
255 templates are used (default).
256 --help or -h show this message.