Bug 2505 - Add commented use warnings where missing in the misc/ directory
[koha.git] / misc / stage_biblios_file.pl
blob3f180821042813db35a705956b7c1b86c6dd7848
1 #!/usr/bin/perl
3 use strict;
4 #use warnings; FIXME - Bug 2505
5 BEGIN {
6 # find Koha's Perl modules
7 # test carefully before changing this
8 use FindBin;
9 eval { require "$FindBin::Bin/kohalib.pl" };
12 use C4::Context;
13 use C4::ImportBatch;
14 use C4::Matcher;
15 use Getopt::Long;
17 $| = 1;
19 # command-line parameters
20 my $match_bibs = 0;
21 my $add_items = 0;
22 my $input_file = "";
23 my $batch_comment = "";
24 my $want_help = 0;
25 my $no_replace ;
27 my $result = GetOptions(
28 'file:s' => \$input_file,
29 'match-bibs:s' => \$match_bibs,
30 'add-items' => \$add_items,
31 'no-replace' => \$no_replace,
32 'comment:s' => \$batch_comment,
33 'h|help' => \$want_help
36 if (not $result or $input_file eq "" or $want_help) {
37 print_usage();
38 exit 0;
41 unless (-r $input_file) {
42 die "$0: cannot open input file $input_file: $!\n";
45 my $dbh = C4::Context->dbh;
46 $dbh->{AutoCommit} = 0;
47 process_batch($input_file, $match_bibs, $add_items, $batch_comment);
48 $dbh->commit();
50 exit 0;
52 sub process_batch {
53 my ($input_file, $match_bibs, $add_items, $batch_comment) = @_;
55 open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
56 my $marc_records = "";
57 $/ = "\035";
58 my $num_input_records = 0;
59 while (<IN>) {
60 s/^\s+//;
61 s/\s+$//;
62 next unless $_; # skip if record has only whitespace, as might occur
63 # if file includes newlines between each MARC record
64 $marc_records .= $_; # FIXME - this sort of string concatenation
65 # is probably rather inefficient
66 $num_input_records++;
68 close IN;
70 my $marc_flavor = C4::Context->preference('marcflavour');
72 print "... staging MARC records -- please wait\n";
73 my ($batch_id, $num_valid, $num_items, @import_errors) =
74 BatchStageMarcRecords($marc_flavor, $marc_records, $input_file, $batch_comment, '', $add_items, 0,
75 100, \&print_progress_and_commit);
76 print "... finished staging MARC records\n";
78 my $num_with_matches = 0;
79 if ($match_bibs) {
80 my $matcher = C4::Matcher->fetch($match_bibs) ;
81 if (! defined $matcher) {
82 $matcher = C4::Matcher->new('biblio');
83 $matcher->add_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, '');
84 $matcher->add_simple_required_check('245', 'a', -1, 0, '',
85 '245', 'a', -1, 0, '');
86 } else {
87 SetImportBatchMatcher($batch_id, $match_bibs);
89 # set default record overlay behavior
90 SetImportBatchOverlayAction($batch_id, ($no_replace) ? 'ignore' : 'replace');
91 SetImportBatchNoMatchAction($batch_id, 'create_new');
92 SetImportBatchItemAction($batch_id, 'always_add');
93 print "... looking for matches with records already in database\n";
94 $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, 10, 100, \&print_progress_and_commit);
95 print "... finished looking for matches\n";
98 my $num_invalid_bibs = scalar(@import_errors);
99 print <<_SUMMARY_;
101 MARC record staging report
102 ------------------------------------
103 Input file: $input_file
104 Number of input bibs: $num_input_records
105 Number of valid bibs: $num_valid
106 Number of invalid bibs: $num_invalid_bibs
107 _SUMMARY_
108 if ($match_bibs) {
109 print "Number of bibs matched: $num_with_matches\n";
110 } else {
111 print "Incoming bibs not matched against existing bibs (--match-bibs option not supplied)\n";
113 if ($add_items) {
114 print "Number of items parsed: $num_items\n";
115 } else {
116 print "No items parsed (--add-items option not supplied)\n";
119 print "\n";
120 print "Batch number assigned: $batch_id\n";
121 print "\n";
124 sub print_progress_and_commit {
125 my $recs = shift;
126 $dbh->commit();
127 print "... processed $recs records\n";
130 sub print_usage {
131 print <<_USAGE_;
132 $0: stage MARC bib file into reservoir.
134 Use this batch job to load a file of MARC bibliographic records
135 (with optional item information) into the Koha reservoir.
137 After running this program to stage your file, you can use
138 either the batch job commit_biblios_file.pl or the Koha
139 Tools option "Manage Staged MARC Records" to load the
140 records into the main Koha database.
142 Parameters:
143 --file <file_name> name of input MARC bib file
144 --match-bibs <match_id> use this option to match bibs
145 in the file with bibs already in
146 the database for future overlay.
147 If <match_id> isn't defined, a default
148 MARC21 ISBN & title match rule will be applied.
149 --add-items use this option to specify that
150 item data is embedded in the MARC
151 bibs and should be parsed.
152 --no-replace overlay action for bib record: default is to
153 replace extant bib with the imported record.
154 --comment <comment> optional comment to describe
155 the record batch; if the comment
156 has spaces in it, surround the
157 comment with quotation marks.
158 --help or -h show this message.
159 _USAGE_