updating page-numbers.inc for OPAC
[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_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, '');
66         $matcher->add_simple_required_check('245', 'a', -1, 0, '', 
67                                             '245', 'a', -1, 0, '');
68         print "... looking for matches with records already in database\n";
69         $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, 10, 100, \&print_progress);
70         print "... finished looking for matches\n";
71     }
72
73     my $num_invalid_bibs = scalar(@import_errors);
74     print <<_SUMMARY_;
75
76 MARC record staging report
77 ------------------------------------
78 Input file:              $input_file
79 Number of input bibs:    $num_input_records
80 Number of valid bibs:    $num_valid
81 Number of invalid bibs:  $num_invalid_bibs
82 _SUMMARY_
83     if ($match_bibs) {
84         print "Number of bibs matched:  $num_with_matches\n";
85     } else {
86         print "Incoming bibs not matched against existing bibs (--match-bibs option not supplied)\n";
87     }
88     if ($add_items) {
89         print "Number of items parsed:  $num_items\n";
90     } else {
91         print "No items parsed (--add-items option not supplied)\n";
92     }
93
94     print "\n";
95     print "Batch number assigned:  $batch_id\n";
96     print "\n";
97 }
98
99 sub print_progress {
100     my $recs = shift;
101     print "... processed $recs records\n";
102 }
103
104 sub print_usage {
105     print <<_USAGE_;
106 $0: stage MARC bib file into reservoir.
107
108 Use this batch job to load a file of MARC bibliographic records
109 (with optional item information) into the Koha reservoir.
110
111 After running this program to stage your file, you can use
112 either the batch job commit_biblios_file.pl or the Koha
113 Tools option "Manage Staged MARC Records" to load the
114 records into the main Koha database.
115
116 Parameters:
117     --file <file_name>      name of input MARC bib file
118     --match-bibs            use this option to match bibs
119                             in the file with bibs already in 
120                             the database for future overlay.
121     --add-items             use this option to specify that
122                             item data is embedded in the MARC
123                             bibs and should be parsed.
124     --comment <comment>     optional comment to describe
125                             the record batch; if the comment
126                             has spaces in it, surround the
127                             comment with quotation marks.
128     --help or -h            show this message.
129 _USAGE_
130 }