Revert "Merging hdl's Search.pm changes"
[koha.git] / misc / stage_biblios_file.pl
blobbfe26ce972721f701497f9d427316e750a35d81e
1 #!/usr/bin/perl
3 use strict;
4 BEGIN {
5 # find Koha's Perl modules
6 # test carefully before changing this
7 use FindBin;
8 eval { require "$FindBin::Bin/kohalib.pl" };
11 use C4::Context;
12 use C4::ImportBatch;
13 use C4::Matcher;
14 use Getopt::Long;
16 $| = 1;
18 # command-line parameters
19 my $match_bibs = 0;
20 my $add_items = 0;
21 my $input_file = "";
22 my $batch_comment = "";
23 my $want_help = 0;
25 my $result = GetOptions(
26 'file:s' => \$input_file,
27 'match-bibs' => \$match_bibs,
28 'add-items' => \$add_items,
29 'comment:s' => \$batch_comment,
30 'h|help' => \$want_help
33 if (not $result or $input_file eq "" or $want_help) {
34 print_usage();
35 exit 0;
38 unless (-r $input_file) {
39 die "$0: cannot open input file $input_file: $!\n";
42 process_batch($input_file, $match_bibs, $add_items, $batch_comment);
44 exit 0;
46 sub process_batch {
47 my ($input_file, $match_bibs, $add_items, $batch_comment) = @_;
49 open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
50 my $marc_records = "";
51 $/ = "\035";
52 my $num_input_records = 0;
53 while (<IN>) {
54 $marc_records .= $_; # FIXME - this sort of string concatenation
55 # is probably rather inefficient
56 $num_input_records++;
58 close IN;
60 my $marc_flavor = C4::Context->preference('marcflavour');
62 print "... staging MARC records -- please wait\n";
63 my ($batch_id, $num_valid, $num_items, @import_errors) =
64 BatchStageMarcRecords($marc_flavor, $marc_records, $input_file, $batch_comment, '', $add_items, 0,
65 100, \&print_progress);
66 print "... finished staging MARC records\n";
68 my $num_with_matches = 0;
69 if ($match_bibs) {
70 my $matcher = C4::Matcher->new('biblio');
71 $matcher->add_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, '');
72 $matcher->add_simple_required_check('245', 'a', -1, 0, '',
73 '245', 'a', -1, 0, '');
74 print "... looking for matches with records already in database\n";
75 $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, 10, 100, \&print_progress);
76 print "... finished looking for matches\n";
79 my $num_invalid_bibs = scalar(@import_errors);
80 print <<_SUMMARY_;
82 MARC record staging report
83 ------------------------------------
84 Input file: $input_file
85 Number of input bibs: $num_input_records
86 Number of valid bibs: $num_valid
87 Number of invalid bibs: $num_invalid_bibs
88 _SUMMARY_
89 if ($match_bibs) {
90 print "Number of bibs matched: $num_with_matches\n";
91 } else {
92 print "Incoming bibs not matched against existing bibs (--match-bibs option not supplied)\n";
94 if ($add_items) {
95 print "Number of items parsed: $num_items\n";
96 } else {
97 print "No items parsed (--add-items option not supplied)\n";
100 print "\n";
101 print "Batch number assigned: $batch_id\n";
102 print "\n";
105 sub print_progress {
106 my $recs = shift;
107 print "... processed $recs records\n";
110 sub print_usage {
111 print <<_USAGE_;
112 $0: stage MARC bib file into reservoir.
114 Use this batch job to load a file of MARC bibliographic records
115 (with optional item information) into the Koha reservoir.
117 After running this program to stage your file, you can use
118 either the batch job commit_biblios_file.pl or the Koha
119 Tools option "Manage Staged MARC Records" to load the
120 records into the main Koha database.
122 Parameters:
123 --file <file_name> name of input MARC bib file
124 --match-bibs use this option to match bibs
125 in the file with bibs already in
126 the database for future overlay.
127 --add-items use this option to specify that
128 item data is embedded in the MARC
129 bibs and should be parsed.
130 --comment <comment> optional comment to describe
131 the record batch; if the comment
132 has spaces in it, surround the
133 comment with quotation marks.
134 --help or -h show this message.
135 _USAGE_