Bug 15098: Itemtype description missing from facets for itypes in a search group
[koha.git] / misc / stage_file.pl
blob30030849896519f2c0acad45c6652b33b7649745
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 ;
48 my $item_action = 'always_add';
50 my $result = GetOptions(
51 'encoding:s' => \$encoding,
52 'file:s' => \$input_file,
53 'match|match-bibs:s' => \$match,
54 'add-items' => \$add_items,
55 'item-action:s' => \$item_action,
56 'no-replace' => \$no_replace,
57 'comment:s' => \$batch_comment,
58 'authorities' => \$authorities,
59 'h|help' => \$want_help
62 $record_type = 'auth' if ($authorities);
64 if ($encoding eq "") {
65 $encoding = "utf8";
68 if (not $result or $input_file eq "" or $want_help) {
69 print_usage();
70 exit 0;
73 unless (-r $input_file) {
74 die "$0: cannot open input file $input_file: $!\n";
77 my $dbh = C4::Context->dbh;
78 $dbh->{AutoCommit} = 0;
79 process_batch($input_file, $record_type, $match, $add_items, $batch_comment);
80 $dbh->commit();
82 exit 0;
84 sub process_batch {
85 my ($input_file, $record_type, $match, $add_items, $batch_comment) = @_;
87 open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
88 my $marc_records = "";
89 $/ = "\035";
90 my $num_input_records = 0;
91 while (<IN>) {
92 s/^\s+//;
93 s/\s+$//;
94 next unless $_; # skip if record has only whitespace, as might occur
95 # if file includes newlines between each MARC record
96 $marc_records .= $_; # FIXME - this sort of string concatenation
97 # is probably rather inefficient
98 $num_input_records++;
100 close IN;
102 print "... staging MARC records -- please wait\n";
103 #FIXME: We should really allow the use of marc modification frameworks and to_marc plugins here if possible
104 my ($batch_id, $num_valid_records, $num_items, @import_errors) =
105 BatchStageMarcRecords($record_type, $encoding, $marc_records, $input_file, undef, undef, $batch_comment, '', $add_items, 0,
106 100, \&print_progress_and_commit);
107 print "... finished staging MARC records\n";
109 my $num_with_matches = 0;
110 if ($match) {
111 my $matcher = C4::Matcher->fetch($match) ;
112 if (defined $matcher) {
113 SetImportBatchMatcher($batch_id, $match);
114 } elsif ($record_type eq 'biblio') {
115 $matcher = C4::Matcher->new($record_type);
116 $matcher->add_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, '');
117 $matcher->add_simple_required_check('245', 'a', -1, 0, '',
118 '245', 'a', -1, 0, '');
120 # set default record overlay behavior
121 SetImportBatchOverlayAction($batch_id, ($no_replace) ? 'ignore' : 'replace');
122 SetImportBatchNoMatchAction($batch_id, 'create_new');
123 SetImportBatchItemAction($batch_id, $item_action);
124 print "... looking for matches with records already in database\n";
125 $num_with_matches = BatchFindDuplicates($batch_id, $matcher, 10, 100, \&print_progress_and_commit);
126 print "... finished looking for matches\n";
129 my $num_invalid_records = scalar(@import_errors);
130 print <<_SUMMARY_;
132 MARC record staging report
133 ------------------------------------
134 Input file: $input_file
135 Record type: $record_type
136 Number of input records: $num_input_records
137 Number of valid records: $num_valid_records
138 Number of invalid records: $num_invalid_records
139 _SUMMARY_
140 if ($match) {
141 print "Number of records matched: $num_with_matches\n";
142 } else {
143 print "Incoming records not matched against existing records (--match option not supplied)\n";
145 if ($record_type eq 'biblio') {
146 if ($add_items) {
147 print "Number of items parsed: $num_items\n";
148 } else {
149 print "No items parsed (--add-items option not supplied)\n";
153 print "\n";
154 print "Batch number assigned: $batch_id\n";
155 print "\n";
158 sub print_progress_and_commit {
159 my $recs = shift;
160 $dbh->commit();
161 print "... processed $recs records\n";
164 sub print_usage {
165 print <<_USAGE_;
166 $0: stage MARC file into reservoir.
168 Use this batch job to load a file of MARC bibliographic
169 (with optional item information) or authority records into
170 the Koha reservoir.
172 After running this program to stage your file, you can use
173 either the batch job commit_file.pl or the Koha
174 Tools option "Manage Staged MARC Records" to load the
175 records into the main Koha database.
177 Parameters:
178 --file <file_name> name of input MARC bib file
179 --authorities stage authority records instead of bibs
180 --encoding <encoding> encoding of MARC records, default is utf8.
181 Other possible options are: MARC-8,
182 ISO_5426, ISO_6937, ISO_8859-1, EUC-KR
183 --match <match_id> use this option to match records
184 in the file with records already in
185 the database for future overlay.
186 If <match_id> isn't defined, a default
187 MARC21 ISBN & title match rule will be applied
188 for bib imports.
189 --add-items use this option to specify that
190 item data is embedded in the MARC
191 bibs and should be parsed.
192 --item-action action to take if --add-items is specifed;
193 choices are 'always_add',
194 'add_only_for_matches', 'add_only_for_new',
195 'ignore', or 'replace'
196 --no-replace overlay action for record: default is to
197 replace extant with the imported record.
198 --comment <comment> optional comment to describe
199 the record batch; if the comment
200 has spaces in it, surround the
201 comment with quotation marks.
202 --help or -h show this message.
203 _USAGE_