new batch job to commit a batch of imported bibs
[koha.git] / misc / commit_biblios_file.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 use C4::Context;
6 use C4::ImportBatch;
7 use Getopt::Long;
8
9 $| = 1;
10
11 # command-line parameters
12 my $batch_number = "";
13 my $list_batches = 0;
14 my $want_help = 0;
15
16 my $result = GetOptions(
17     'batch-number:s' => \$batch_number,
18     'list-batches'   => \$list_batches,
19     'h|help'         => \$want_help
20 );
21
22 if ($want_help or (not $batch_number and not $list_batches)) {
23     print_usage();
24     exit 0;
25 }
26
27 if ($list_batches) {
28     list_batches();
29     exit 0;
30 }
31
32 # FIXME dummy user so that logging won't fail
33 # in future, probably should tie to a real user account
34 C4::Context->set_userenv(0, 'batch', 0, 'batch', 'batch', 'batch', 'batch', 'batch');
35
36 if ($batch_number =~ /^\d+$/ and $batch_number > 0) {
37     my $batch = GetImportBatch($batch_number);
38     die "$0: import batch $batch_number does not exist in database\n" unless defined $batch;
39     die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
40         unless $batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted";
41     process_batch($batch_number);
42 } else {
43     die "$0: please specify a numeric batch ID\n";
44 }
45
46 exit 0;
47
48 sub list_batches {
49     my $results = GetAllImportBatches();
50     print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n", "#", "File name", "Batch comments", "Status");
51     print '-' x 5, ' ' , '-' x 25, ' ', '-' x 25, ' ', '-' x 10, "\n" ;
52     foreach my $batch (@{ $results}) {
53         if ($batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted") {
54             print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n",
55                           $batch->{'import_batch_id'},
56                           $batch->{'file_name'},
57                           $batch->{'comments'},
58                           $batch->{'import_status'});
59         }
60     }
61 }
62
63 sub process_batch {
64     my ($import_batch_id) = @_;
65
66     print "... importing MARC records -- please wait\n";
67     my ($num_added, $num_updated, $num_items_added, $num_ignored) = BatchCommitBibRecords($import_batch_id, 100, \&print_progress);
68     print "... finished importing MARC records\n";
69
70     print <<_SUMMARY_;
71
72 MARC record import report
73 ----------------------------------------
74 Batch number:                    $import_batch_id
75 Number of new bibs added:        $num_added
76 Number of bibs replaced:         $num_updated
77 Number of bibs ignored:          $num_ignored
78 Number of items added:           $num_items_added
79 _SUMMARY_
80 }
81
82 sub print_progress {
83     my $recs = shift;
84     print "... processed $recs records\n";
85 }
86
87 sub print_usage {
88     print <<_USAGE_;
89 $0: import a batch of staged MARC records into database.
90
91 Use this batch job to complete the import of a batch of
92 MARC records that was staged either by the batch job
93 stage_biblios_file.pl or by the Koha Tools option
94 "Stage MARC Records for Import".
95
96 Parameters:
97     --batch-number <#>   number of the record batch
98                          to import
99     --list-batches       print a list of record batches
100                          available to commit
101     --help or -h            show this message.
102 _USAGE_
103 }