kohabug 2076 - remove three tables (DB rev 077)
[koha.git] / misc / stage_biblios_file.pl
1 #!/usr/bin/perl
2
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" };
9 }
10
11 use C4::Context;
12 use C4::ImportBatch;
13 use C4::Matcher;
14 use Getopt::Long;
15
16 $| = 1;
17
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;
24
25 my $result = GetOptions(
26     'file:s'        => \$input_file,
27     'match-bibs:s'    => \$match_bibs,
28     'add-items'     => \$add_items,
29     'comment:s'     => \$batch_comment,
30     'h|help'        => \$want_help
31 );
32
33 if (not $result or $input_file eq "" or $want_help) {
34     print_usage();
35     exit 0;
36 }
37
38 unless (-r $input_file) {
39     die "$0: cannot open input file $input_file: $!\n";
40 }
41
42 my $dbh = C4::Context->dbh;
43 $dbh->{AutoCommit} = 0;
44 process_batch($input_file, $match_bibs, $add_items, $batch_comment);
45 $dbh->commit();
46
47 exit 0;
48
49 sub process_batch {
50     my ($input_file, $match_bibs, $add_items, $batch_comment) = @_;
51
52     open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
53     my $marc_records = "";
54     $/ = "\035";
55     my $num_input_records = 0;
56     while (<IN>) {
57         s/^\s+//;
58         s/\s+$//;
59         next unless $_; # skip if record has only whitespace, as might occur
60                         # if file includes newlines between each MARC record
61         $marc_records .= $_; # FIXME - this sort of string concatenation
62                              # is probably rather inefficient
63         $num_input_records++;
64     }
65     close IN;
66
67     my $marc_flavor = C4::Context->preference('marcflavour');
68
69     print "... staging MARC records -- please wait\n";
70     my ($batch_id, $num_valid, $num_items, @import_errors) = 
71         BatchStageMarcRecords($marc_flavor, $marc_records, $input_file, $batch_comment, '', $add_items, 0,
72                               100, \&print_progress_and_commit);
73     print "... finished staging MARC records\n";
74
75     my $num_with_matches = 0;
76     if ($match_bibs) {
77         my $matcher = C4::Matcher->fetch($match_bibs) ;
78         if (! defined $matcher) {
79             $matcher = C4::Matcher->new('biblio');
80             $matcher->add_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, '');
81             $matcher->add_simple_required_check('245', 'a', -1, 0, '', 
82                                             '245', 'a', -1, 0, '');
83         } else {
84             SetImportBatchMatcher($batch_id, $match_bibs);
85         }
86         # set default record overlay behavior
87         SetImportBatchOverlayAction($batch_id, 'replace');
88         SetImportBatchNoMatchAction($batch_id, 'create_new');
89         SetImportBatchItemAction($batch_id, 'always_add');
90         print "... looking for matches with records already in database\n";
91         $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, 10, 100, \&print_progress_and_commit);
92         print "... finished looking for matches\n";
93     }
94
95     my $num_invalid_bibs = scalar(@import_errors);
96     print <<_SUMMARY_;
97
98 MARC record staging report
99 ------------------------------------
100 Input file:              $input_file
101 Number of input bibs:    $num_input_records
102 Number of valid bibs:    $num_valid
103 Number of invalid bibs:  $num_invalid_bibs
104 _SUMMARY_
105     if ($match_bibs) {
106         print "Number of bibs matched:  $num_with_matches\n";
107     } else {
108         print "Incoming bibs not matched against existing bibs (--match-bibs option not supplied)\n";
109     }
110     if ($add_items) {
111         print "Number of items parsed:  $num_items\n";
112     } else {
113         print "No items parsed (--add-items option not supplied)\n";
114     }
115
116     print "\n";
117     print "Batch number assigned:  $batch_id\n";
118     print "\n";
119 }
120
121 sub print_progress_and_commit {
122     my $recs = shift;
123     $dbh->commit();
124     print "... processed $recs records\n";
125 }
126
127 sub print_usage {
128     print <<_USAGE_;
129 $0: stage MARC bib file into reservoir.
130
131 Use this batch job to load a file of MARC bibliographic records
132 (with optional item information) into the Koha reservoir.
133
134 After running this program to stage your file, you can use
135 either the batch job commit_biblios_file.pl or the Koha
136 Tools option "Manage Staged MARC Records" to load the
137 records into the main Koha database.
138
139 Parameters:
140     --file <file_name>      name of input MARC bib file
141     --match-bibs <match_id> use this option to match bibs
142                             in the file with bibs already in 
143                             the database for future overlay.
144                             If <match_id> isn't defined, a default 
145                             MARC21 ISBN & title match rule will be applied.
146     --add-items             use this option to specify that
147                             item data is embedded in the MARC
148                             bibs and should be parsed.
149     --comment <comment>     optional comment to describe
150                             the record batch; if the comment
151                             has spaces in it, surround the
152                             comment with quotation marks.
153     --help or -h            show this message.
154 _USAGE_
155 }