Fix for Bug 4024, Search history template problems, and other fixes.
[koha.git] / misc / commit_biblios_file.pl
blob2f9851fe8224a0d4a6c50a42358ca8fdf8926726
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 Getopt::Long;
15 $| = 1;
17 # command-line parameters
18 my $batch_number = "";
19 my $list_batches = 0;
20 my $want_help = 0;
22 my $result = GetOptions(
23 'batch-number:s' => \$batch_number,
24 'list-batches' => \$list_batches,
25 'h|help' => \$want_help
28 if ($want_help or (not $batch_number and not $list_batches)) {
29 print_usage();
30 exit 0;
33 if ($list_batches) {
34 list_batches();
35 exit 0;
38 # FIXME dummy user so that logging won't fail
39 # in future, probably should tie to a real user account
40 C4::Context->set_userenv(0, 'batch', 0, 'batch', 'batch', 'batch', 'batch', 'batch');
42 my $dbh = C4::Context->dbh;
43 $dbh->{AutoCommit} = 0;
44 if ($batch_number =~ /^\d+$/ and $batch_number > 0) {
45 my $batch = GetImportBatch($batch_number);
46 die "$0: import batch $batch_number does not exist in database\n" unless defined $batch;
47 die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
48 unless $batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted";
49 process_batch($batch_number);
50 $dbh->commit();
51 } else {
52 die "$0: please specify a numeric batch ID\n";
55 exit 0;
57 sub list_batches {
58 my $results = GetAllImportBatches();
59 print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n", "#", "File name", "Batch comments", "Status");
60 print '-' x 5, ' ' , '-' x 25, ' ', '-' x 25, ' ', '-' x 10, "\n" ;
61 foreach my $batch (@{ $results}) {
62 if ($batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted") {
63 print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n",
64 $batch->{'import_batch_id'},
65 $batch->{'file_name'},
66 $batch->{'comments'},
67 $batch->{'import_status'});
72 sub process_batch {
73 my ($import_batch_id) = @_;
75 print "... importing MARC records -- please wait\n";
76 my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) =
77 BatchCommitBibRecords($import_batch_id, 100, \&print_progress_and_commit);
78 print "... finished importing MARC records\n";
80 print <<_SUMMARY_;
82 MARC record import report
83 ----------------------------------------
84 Batch number: $import_batch_id
85 Number of new bibs added: $num_added
86 Number of bibs replaced: $num_updated
87 Number of bibs ignored: $num_ignored
88 Number of items added: $num_items_added
89 Number of items ignored: $num_items_errored
91 Note: an item is ignored if its barcode is a
92 duplicate of one already in the database.
93 _SUMMARY_
96 sub print_progress_and_commit {
97 my $recs = shift;
98 print "... processed $recs records\n";
99 $dbh->commit();
102 sub print_usage {
103 print <<_USAGE_;
104 $0: import a batch of staged MARC records into database.
106 Use this batch job to complete the import of a batch of
107 MARC records that was staged either by the batch job
108 stage_biblios_file.pl or by the Koha Tools option
109 "Stage MARC Records for Import".
111 Parameters:
112 --batch-number <#> number of the record batch
113 to import
114 --list-batches print a list of record batches
115 available to commit
116 --help or -h show this message.
117 _USAGE_