Bug 766: remove CGI::scrolling_list from request.pl
[koha.git] / misc / stage_file.pl
blob4ce16b59e15d2ad493c527004280bb769d0c4ab0
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 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" };
31 use C4::Context;
32 use C4::ImportBatch;
33 use C4::Matcher;
34 use Getopt::Long;
36 $| = 1;
38 # command-line parameters
39 my $record_type = "biblio";
40 my $encoding = "";
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 ;
49 my $result = GetOptions(
50 'encoding:s' => \$encoding,
51 'file:s' => \$input_file,
52 'match|match-bibs:s' => \$match,
53 'add-items' => \$add_items,
54 'no-replace' => \$no_replace,
55 'comment:s' => \$batch_comment,
56 'authorities' => \$authorities,
57 'h|help' => \$want_help
60 $record_type = 'auth' if ($authorities);
62 if ($encoding eq "") {
63 $encoding = "utf8";
66 if (not $result or $input_file eq "" or $want_help) {
67 print_usage();
68 exit 0;
71 unless (-r $input_file) {
72 die "$0: cannot open input file $input_file: $!\n";
75 my $dbh = C4::Context->dbh;
76 $dbh->{AutoCommit} = 0;
77 process_batch($input_file, $record_type, $match, $add_items, $batch_comment);
78 $dbh->commit();
80 exit 0;
82 sub process_batch {
83 my ($input_file, $record_type, $match, $add_items, $batch_comment) = @_;
85 open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
86 my $marc_records = "";
87 $/ = "\035";
88 my $num_input_records = 0;
89 while (<IN>) {
90 s/^\s+//;
91 s/\s+$//;
92 next unless $_; # skip if record has only whitespace, as might occur
93 # if file includes newlines between each MARC record
94 $marc_records .= $_; # FIXME - this sort of string concatenation
95 # is probably rather inefficient
96 $num_input_records++;
98 close IN;
100 print "... staging MARC records -- please wait\n";
101 my ($batch_id, $num_valid_records, $num_items, @import_errors) =
102 BatchStageMarcRecords($record_type, $encoding, $marc_records, $input_file, $batch_comment, '', $add_items, 0,
103 100, \&print_progress_and_commit);
104 print "... finished staging MARC records\n";
106 my $num_with_matches = 0;
107 if ($match) {
108 my $matcher = C4::Matcher->fetch($match) ;
109 if (defined $matcher) {
110 SetImportBatchMatcher($batch_id, $match);
111 } elsif ($record_type eq 'biblio') {
112 $matcher = C4::Matcher->new($record_type);
113 $matcher->add_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, '');
114 $matcher->add_simple_required_check('245', 'a', -1, 0, '',
115 '245', 'a', -1, 0, '');
117 # set default record overlay behavior
118 SetImportBatchOverlayAction($batch_id, ($no_replace) ? 'ignore' : 'replace');
119 SetImportBatchNoMatchAction($batch_id, 'create_new');
120 SetImportBatchItemAction($batch_id, 'always_add');
121 print "... looking for matches with records already in database\n";
122 $num_with_matches = BatchFindDuplicates($batch_id, $matcher, 10, 100, \&print_progress_and_commit);
123 print "... finished looking for matches\n";
126 my $num_invalid_records = scalar(@import_errors);
127 print <<_SUMMARY_;
129 MARC record staging report
130 ------------------------------------
131 Input file: $input_file
132 Record type: $record_type
133 Number of input records: $num_input_records
134 Number of valid records: $num_valid_records
135 Number of invalid records: $num_invalid_records
136 _SUMMARY_
137 if ($match) {
138 print "Number of records matched: $num_with_matches\n";
139 } else {
140 print "Incoming records not matched against existing records (--match option not supplied)\n";
142 if ($record_type eq 'biblio') {
143 if ($add_items) {
144 print "Number of items parsed: $num_items\n";
145 } else {
146 print "No items parsed (--add-items option not supplied)\n";
150 print "\n";
151 print "Batch number assigned: $batch_id\n";
152 print "\n";
155 sub print_progress_and_commit {
156 my $recs = shift;
157 $dbh->commit();
158 print "... processed $recs records\n";
161 sub print_usage {
162 print <<_USAGE_;
163 $0: stage MARC file into reservoir.
165 Use this batch job to load a file of MARC bibliographic
166 (with optional item information) or authority records into
167 the Koha reservoir.
169 After running this program to stage your file, you can use
170 either the batch job commit_file.pl or the Koha
171 Tools option "Manage Staged MARC Records" to load the
172 records into the main Koha database.
174 Parameters:
175 --file <file_name> name of input MARC bib file
176 --authorities stage authority records instead of bibs
177 --encoding <encoding> encoding of MARC records, default is utf8.
178 Other possible options are: MARC-8,
179 ISO_5426, ISO_6937, ISO_8859-1, EUC-KR
180 --match <match_id> use this option to match records
181 in the file with records already in
182 the database for future overlay.
183 If <match_id> isn't defined, a default
184 MARC21 ISBN & title match rule will be applied
185 for bib imports.
186 --add-items use this option to specify that
187 item data is embedded in the MARC
188 bibs and should be parsed.
189 --no-replace overlay action for record: default is to
190 replace extant with the imported record.
191 --comment <comment> optional comment to describe
192 the record batch; if the comment
193 has spaces in it, surround the
194 comment with quotation marks.
195 --help or -h show this message.
196 _USAGE_