Bug 19164: Add MARC modification template support to misc/stage-file.pl.
[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 Modern::Perl;
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 C4::MarcModificationTemplates;
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 my $marc_mod_template = '';
52 my $marc_mod_template_id = undef;
53
54 my $result = GetOptions(
55     'encoding:s'    => \$encoding,
56     'file:s'        => \$input_file,
57     'format:s'      => \$format,
58     'match|match-bibs:s'  => \$match,
59     'add-items'     => \$add_items,
60     'item-action:s' => \$item_action,
61     'no-replace'    => \$no_replace,
62     'no-create'     => \$no_create,
63     'comment:s'     => \$batch_comment,
64     'authorities'   => \$authorities,
65     'marcmodtemplate:s' => \$marc_mod_template,
66     'h|help'        => \$want_help
67 );
68
69 if($marc_mod_template ne '') {
70    my @templates = GetModificationTemplates();
71    foreach my $this_template (@templates) {
72        if($this_template->{'name'} eq $marc_mod_template) {
73           if(!defined $marc_mod_template_id) {
74                $marc_mod_template_id = $this_template->{'template_id'};
75            } else {
76                print "WARNING: MARC modification template name " .
77                    "'$marc_mod_template' matches multiple templates. " .
78                    "Please fix this issue before proceeding.\n";
79                exit 1;
80            }
81        }
82    }
83
84    if(!defined $marc_mod_template_id ) {
85        die "Can't locate MARC modification template '$marc_mod_template'\n";
86    }
87 }
88
89 $record_type = 'auth' if ($authorities);
90
91 if (not $result or $input_file eq "" or $want_help) {
92     print_usage();
93     exit 0;
94 }
95 if ( $format !~ /^(MARCXML|ISO2709)$/i ) {
96     print "\n --format must be MARCXML or ISO2709\n";
97     print_usage();
98     exit 0;
99 }
100
101 unless (-r $input_file) {
102     die "$0: cannot open input file $input_file: $!\n";
103 }
104
105 my $dbh = C4::Context->dbh;
106 $dbh->{AutoCommit} = 0;
107 process_batch({
108     format        => $format,
109     input_file    => $input_file,
110     record_type   => $record_type,
111     match         => $match,
112     add_items     => $add_items,
113     batch_comment => $batch_comment,
114     encoding      => $encoding,
115     no_replace    => $no_replace,
116     no_create     => $no_create,
117     item_action   => $item_action,
118     marc_mod_template_id => $marc_mod_template_id,
119 });
120 $dbh->commit();
121
122 exit 0;
123
124 sub process_batch {
125     my ( $params ) = @_;  #Possible params are: format input_file record_type match add_items batch_comment encoding no_replace no_create item_action
126     my $format = $params->{format} // '';
127     my $record_type = $params->{record_type} // 'biblio';
128
129     my ( $errors, $marc_records );
130     if( $format eq 'ISO2709' ) {
131         ( $errors, $marc_records ) = C4::ImportBatch::RecordsFromISO2709File(
132             $params->{input_file}, $record_type, $params->{encoding} );
133     } elsif( $format eq 'MARCXML' ) {
134         ( $errors, $marc_records ) = C4::ImportBatch::RecordsFromMARCXMLFile(
135             $params->{input_file}, $params->{encoding} );
136     }
137     warn ( join ',', @$errors ) if @$errors;
138     my $num_input_records = ($marc_records) ? scalar(@$marc_records) : 0;
139
140     print "... staging MARC records -- please wait\n";
141     #FIXME: We should really allow the use of marc modification frameworks and to_marc plugins here if possible
142     my ($batch_id, $num_valid_records, $num_items, @import_errors) =
143         BatchStageMarcRecords(
144             $record_type, $params->{encoding},
145             $marc_records, $params->{input_file},
146             $params->{'marc_mod_template_id'},
147             $params->{batch_comment}, '',
148             $params->{add_items}, 0,
149             100, \&print_progress_and_commit);
150     print "... finished staging MARC records\n";
151
152     my $num_with_matches = 0;
153     if ( $params->{match} ) {
154         my $matcher = C4::Matcher->fetch( $params->{match} );
155         if (defined $matcher) {
156             SetImportBatchMatcher( $batch_id, $params->{match} );
157         } elsif ($record_type eq 'biblio')  {
158             $matcher = C4::Matcher->new($record_type);
159             $matcher->add_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, '');
160             $matcher->add_simple_required_check('245', 'a', -1, 0, '',
161                                             '245', 'a', -1, 0, '');
162         }
163         # set default record overlay behavior
164         SetImportBatchOverlayAction( $batch_id, $params->{no_replace} ? 'ignore' : 'replace' );
165         SetImportBatchNoMatchAction( $batch_id, $params->{no_create} ? 'ignore' : 'create_new' );
166         SetImportBatchItemAction( $batch_id, $params->{item_action} );
167         print "... looking for matches with records already in database\n";
168         $num_with_matches = BatchFindDuplicates($batch_id, $matcher, 10, 100, \&print_progress_and_commit);
169         print "... finished looking for matches\n";
170     }
171
172     my $num_invalid_records = scalar(@import_errors);
173     print <<_SUMMARY_;
174
175 MARC record staging report
176 ------------------------------------
177 Input file:                 $params->{input_file}
178 Record type:                $record_type
179 Number of input records:    $num_input_records
180 Number of valid records:    $num_valid_records
181 Number of invalid records:  $num_invalid_records
182 _SUMMARY_
183     if( $params->{match} ) {
184         print "Number of records matched:  $num_with_matches\n";
185     } else {
186         print "Incoming records not matched against existing records (--match option not supplied)\n";
187     }
188     if ($record_type eq 'biblio') {
189         if ( $params->{add_items} ) {
190             print "Number of items parsed:  $num_items\n";
191         } else {
192             print "No items parsed (--add-items option not supplied)\n";
193         }
194     }
195
196     print "\n";
197     print "Batch number assigned:  $batch_id\n";
198     print "\n";
199 }
200
201 sub print_progress_and_commit {
202     my $recs = shift;
203     $dbh->commit();
204     print "... processed $recs records\n";
205 }
206
207 sub print_usage {
208     print <<_USAGE_;
209 $0: stage MARC file into reservoir.
210
211 Use this batch job to load a file of MARC bibliographic
212 (with optional item information) or authority records into
213 the Koha reservoir.
214
215 After running this program to stage your file, you can use
216 either the batch job commit_file.pl or the Koha
217 Tools option "Manage Staged MARC Records" to load the
218 records into the main Koha database.
219
220 Parameters:
221     --file <file_name>      name of input MARC bib file
222     --authorities           stage authority records instead of bibs
223     --encoding <encoding>   encoding of MARC records, default is UTF-8.
224                             Other possible options are: MARC-8,
225                             ISO_5426, ISO_6937, ISO_8859-1, EUC-KR
226     --format                The MARC transport format to use?
227                             Defaults to ISO2709.
228                             Available values, MARCXML, ISO2709.
229     --match <match_id>      use this option to match records
230                             in the file with records already in
231                             the database for future overlay.
232                             If <match_id> isn't defined, a default
233                             MARC21 ISBN & title match rule will be applied
234                             for bib imports.
235     --add-items             use this option to specify that
236                             item data is embedded in the MARC
237                             bibs and should be parsed.
238     --item-action           action to take if --add-items is specifed;
239                             choices are 'always_add',
240                             'add_only_for_matches', 'add_only_for_new',
241                             'ignore', or 'replace'
242     --no-replace            overlay action for record: default is to
243                             replace extant with the imported record.
244     --no-create             nomatch action for record: default is to
245                             create new record with imported record.
246     --comment <comment>     optional comment to describe
247                             the record batch; if the comment
248                             has spaces in it, surround the
249                             comment with quotation marks.
250     --marcmodtemplate <TEMPLATE>
251                             This parameter allows you to specify the
252                             name of an existing MARC modification
253                             template to apply as the MARC records are
254                             imported (these templates are created in
255                             the "MARC modification templates" tool in
256                             Koha). If not specified, no MARC modification
257                             templates are used (default).
258     --help or -h            show this message.
259 _USAGE_
260 }