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