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