Merge git://git.koha.org/pub/scm/koha
[koha.git] / misc / commit_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 Getopt::Long;
14
15 $| = 1;
16
17 # command-line parameters
18 my $batch_number = "";
19 my $list_batches = 0;
20 my $want_help = 0;
21
22 my $result = GetOptions(
23     'batch-number:s' => \$batch_number,
24     'list-batches'   => \$list_batches,
25     'h|help'         => \$want_help
26 );
27
28 if ($want_help or (not $batch_number and not $list_batches)) {
29     print_usage();
30     exit 0;
31 }
32
33 if ($list_batches) {
34     list_batches();
35     exit 0;
36 }
37
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');
41
42 if ($batch_number =~ /^\d+$/ and $batch_number > 0) {
43     my $batch = GetImportBatch($batch_number);
44     die "$0: import batch $batch_number does not exist in database\n" unless defined $batch;
45     die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
46         unless $batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted";
47     process_batch($batch_number);
48 } else {
49     die "$0: please specify a numeric batch ID\n";
50 }
51
52 exit 0;
53
54 sub list_batches {
55     my $results = GetAllImportBatches();
56     print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n", "#", "File name", "Batch comments", "Status");
57     print '-' x 5, ' ' , '-' x 25, ' ', '-' x 25, ' ', '-' x 10, "\n" ;
58     foreach my $batch (@{ $results}) {
59         if ($batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted") {
60             print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n",
61                           $batch->{'import_batch_id'},
62                           $batch->{'file_name'},
63                           $batch->{'comments'},
64                           $batch->{'import_status'});
65         }
66     }
67 }
68
69 sub process_batch {
70     my ($import_batch_id) = @_;
71
72     print "... importing MARC records -- please wait\n";
73     my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) = 
74         BatchCommitBibRecords($import_batch_id, 100, \&print_progress);
75     print "... finished importing MARC records\n";
76
77     print <<_SUMMARY_;
78
79 MARC record import report
80 ----------------------------------------
81 Batch number:                    $import_batch_id
82 Number of new bibs added:        $num_added
83 Number of bibs replaced:         $num_updated
84 Number of bibs ignored:          $num_ignored
85 Number of items added:           $num_items_added
86 Number of items ignored:         $num_items_errored
87
88 Note: an item is ignored if its barcode is a 
89 duplicate of one already in the database.
90 _SUMMARY_
91 }
92
93 sub print_progress {
94     my $recs = shift;
95     print "... processed $recs records\n";
96 }
97
98 sub print_usage {
99     print <<_USAGE_;
100 $0: import a batch of staged MARC records into database.
101
102 Use this batch job to complete the import of a batch of
103 MARC records that was staged either by the batch job
104 stage_biblios_file.pl or by the Koha Tools option
105 "Stage MARC Records for Import".
106
107 Parameters:
108     --batch-number <#>   number of the record batch
109                          to import
110     --list-batches       print a list of record batches
111                          available to commit
112     --help or -h            show this message.
113 _USAGE_
114 }