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