Bug 10407: Add marcxml import (follow-up)
[koha.git] / misc / stage_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 # Parts Copyright C & P Bibliography Services 2012
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 use strict;
23 use warnings;
24 BEGIN {
25     # find Koha's Perl modules
26     # test carefully before changing this
27     use FindBin;
28     eval { require "$FindBin::Bin/kohalib.pl" };
29 }
30
31 use C4::Context;
32 use C4::ImportBatch;
33 use C4::Matcher;
34 use Getopt::Long;
35
36 $| = 1;
37
38 # command-line parameters
39 my $record_type = "biblio";
40 my $encoding = "UTF-8";
41 my $authorities = 0;
42 my $match = 0;
43 my $add_items = 0;
44 my $input_file = "";
45 my $batch_comment = "";
46 my $want_help = 0;
47 my $no_replace;
48 my $format = 'ISO2709';
49 my $no_create;
50 my $item_action = 'always_add';
51
52 my $result = GetOptions(
53     'encoding:s'    => \$encoding,
54     'file:s'        => \$input_file,
55     'format:s'      => \$format,
56     'match|match-bibs:s'  => \$match,
57     'add-items'     => \$add_items,
58     'item-action:s' => \$item_action,
59     'no-replace'    => \$no_replace,
60     'no-create'     => \$no_create,
61     'comment:s'     => \$batch_comment,
62     'authorities'   => \$authorities,
63     'h|help'        => \$want_help
64 );
65
66 $record_type = 'auth' if ($authorities);
67
68 if (not $result or $input_file eq "" or $want_help) {
69     print_usage();
70     exit 0;
71 }
72 if ( $format !~ /^(MARCXML|ISO2709)$/i ) {
73     print "\n --format must be MARCXML or ISO2709\n";
74     print_usage();
75     exit 0;
76 }
77
78 unless (-r $input_file) {
79     die "$0: cannot open input file $input_file: $!\n";
80 }
81
82 my $dbh = C4::Context->dbh;
83 $dbh->{AutoCommit} = 0;
84 process_batch($format, $input_file, $record_type, $match, $add_items, $batch_comment);
85 $dbh->commit();
86
87 exit 0;
88
89 sub process_batch {
90     my ($format, $input_file, $record_type, $match, $add_items, $batch_comment) = @_;
91
92     my ( $errors, $marc_records );
93     ( $errors, $marc_records ) = C4::ImportBatch::RecordsFromISO2709File($input_file, $record_type, $encoding) if $format eq 'ISO2709';
94     ( $errors, $marc_records ) = C4::ImportBatch::RecordsFromMARCXMLFile($input_file, $encoding) if $format eq 'MARCXML';
95     warn ( join ',', @$errors ) if @$errors;
96     my $num_input_records = ($marc_records) ? scalar(@$marc_records) : 0;
97
98     print "... staging MARC records -- please wait\n";
99     #FIXME: We should really allow the use of marc modification frameworks and to_marc plugins here if possible
100     my ($batch_id, $num_valid_records, $num_items, @import_errors) =
101         BatchStageMarcRecords($record_type, $encoding, $marc_records, $input_file, undef, undef, $batch_comment, '', $add_items, 0,
102                               100, \&print_progress_and_commit);
103     print "... finished staging MARC records\n";
104
105     my $num_with_matches = 0;
106     if ($match) {
107         my $matcher = C4::Matcher->fetch($match) ;
108         if (defined $matcher) {
109             SetImportBatchMatcher($batch_id, $match);
110         } elsif ($record_type eq 'biblio')  {
111             $matcher = C4::Matcher->new($record_type);
112             $matcher->add_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, '');
113             $matcher->add_simple_required_check('245', 'a', -1, 0, '',
114                                             '245', 'a', -1, 0, '');
115         }
116         # set default record overlay behavior
117         SetImportBatchOverlayAction($batch_id, ($no_replace) ? 'ignore' : 'replace');
118         SetImportBatchNoMatchAction($batch_id, ($no_create) ? 'ignore' : 'create_new');
119         SetImportBatchItemAction($batch_id, $item_action);
120         print "... looking for matches with records already in database\n";
121         $num_with_matches = BatchFindDuplicates($batch_id, $matcher, 10, 100, \&print_progress_and_commit);
122         print "... finished looking for matches\n";
123     }
124
125     my $num_invalid_records = scalar(@import_errors);
126     print <<_SUMMARY_;
127
128 MARC record staging report
129 ------------------------------------
130 Input file:                 $input_file
131 Record type:                $record_type
132 Number of input records:    $num_input_records
133 Number of valid records:    $num_valid_records
134 Number of invalid records:  $num_invalid_records
135 _SUMMARY_
136     if ($match) {
137         print "Number of records matched:  $num_with_matches\n";
138     } else {
139         print "Incoming records not matched against existing records (--match option not supplied)\n";
140     }
141     if ($record_type eq 'biblio') {
142         if ($add_items) {
143             print "Number of items parsed:  $num_items\n";
144         } else {
145             print "No items parsed (--add-items option not supplied)\n";
146         }
147     }
148
149     print "\n";
150     print "Batch number assigned:  $batch_id\n";
151     print "\n";
152 }
153
154 sub print_progress_and_commit {
155     my $recs = shift;
156     $dbh->commit();
157     print "... processed $recs records\n";
158 }
159
160 sub print_usage {
161     print <<_USAGE_;
162 $0: stage MARC file into reservoir.
163
164 Use this batch job to load a file of MARC bibliographic
165 (with optional item information) or authority records into
166 the Koha reservoir.
167
168 After running this program to stage your file, you can use
169 either the batch job commit_file.pl or the Koha
170 Tools option "Manage Staged MARC Records" to load the
171 records into the main Koha database.
172
173 Parameters:
174     --file <file_name>      name of input MARC bib file
175     --authorities           stage authority records instead of bibs
176     --encoding <encoding>   encoding of MARC records, default is UTF-8.
177                             Other possible options are: MARC-8,
178                             ISO_5426, ISO_6937, ISO_8859-1, EUC-KR
179     --format                The MARC transport format to use?
180                             Defaults to ISO2709.
181                             Available values, MARCXML, ISO2709.
182     --match <match_id>      use this option to match records
183                             in the file with records already in
184                             the database for future overlay.
185                             If <match_id> isn't defined, a default
186                             MARC21 ISBN & title match rule will be applied
187                             for bib imports.
188     --add-items             use this option to specify that
189                             item data is embedded in the MARC
190                             bibs and should be parsed.
191     --item-action           action to take if --add-items is specifed;
192                             choices are 'always_add',
193                             'add_only_for_matches', 'add_only_for_new',
194                             'ignore', or 'replace'
195     --no-replace            overlay action for record: default is to
196                             replace extant with the imported record.
197     --no-create             nomatch action for record: default is to
198                             create new record with imported record.
199     --comment <comment>     optional comment to describe
200                             the record batch; if the comment
201                             has spaces in it, surround the
202                             comment with quotation marks.
203     --help or -h            show this message.
204 _USAGE_
205 }