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