Bug 23339: Reindent addbooks.tt
[koha.git] / misc / commit_file.pl
blobbb7dd037a613863b3f39d14eee1d5388d99d98ab
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
5 BEGIN {
6 # find Koha's Perl modules
7 # test carefully before changing this
8 use FindBin;
9 eval { require "$FindBin::Bin/kohalib.pl" };
12 use Koha::Script;
13 use C4::Context;
14 use C4::ImportBatch;
15 use Getopt::Long;
17 $| = 1;
19 # command-line parameters
20 my $batch_number = "";
21 my $list_batches = 0;
22 my $revert = 0;
23 my $want_help = 0;
25 my $result = GetOptions(
26 'batch-number:s' => \$batch_number,
27 'list-batches' => \$list_batches,
28 'revert' => \$revert,
29 'h|help' => \$want_help
32 if ($want_help or (not $batch_number and not $list_batches)) {
33 print_usage();
34 exit 0;
37 if ($list_batches) {
38 list_batches();
39 exit 0;
42 # FIXME dummy user so that logging won't fail
43 # in future, probably should tie to a real user account
44 C4::Context->set_userenv(0, 'batch', 0, 'batch', 'batch', 'batch', 'batch', 'batch');
46 my $dbh = C4::Context->dbh;
47 $dbh->{AutoCommit} = 0;
48 if ($batch_number =~ /^\d+$/ and $batch_number > 0) {
49 my $batch = GetImportBatch($batch_number);
50 die "$0: import batch $batch_number does not exist in database\n" unless defined $batch;
51 if ($revert) {
52 die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
53 unless $batch->{'import_status'} eq "imported";
54 revert_batch($batch_number);
55 } else {
56 die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
57 unless $batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted";
58 process_batch($batch_number);
60 $dbh->commit();
61 } else {
62 die "$0: please specify a numeric batch ID\n";
65 exit 0;
67 sub list_batches {
68 my $results = GetAllImportBatches();
69 print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n", "#", "File name", "Batch comments", "Status");
70 print '-' x 5, ' ' , '-' x 25, ' ', '-' x 25, ' ', '-' x 10, "\n" ;
71 foreach my $batch (@{ $results}) {
72 if ($batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted") {
73 print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n",
74 $batch->{'import_batch_id'},
75 $batch->{'file_name'},
76 $batch->{'comments'},
77 $batch->{'import_status'});
82 sub process_batch {
83 my ($import_batch_id) = @_;
85 print "... importing MARC records -- please wait\n";
86 my ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored) =
87 BatchCommitRecords($import_batch_id, '', 100, \&print_progress_and_commit);
88 print "... finished importing MARC records\n";
90 print <<_SUMMARY_;
92 MARC record import report
93 ----------------------------------------
94 Batch number: $import_batch_id
95 Number of new records added: $num_added
96 Number of records replaced: $num_updated
97 Number of records ignored: $num_ignored
98 Number of items added: $num_items_added
99 Number of items replaced: $num_items_replaced
100 Number of items ignored: $num_items_errored
102 Note: an item is ignored if its barcode is a
103 duplicate of one already in the database.
104 _SUMMARY_
107 sub revert_batch {
108 my ($import_batch_id) = @_;
110 print "... reverting batch -- please wait\n";
111 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) =
112 BatchRevertRecords($import_batch_id, 100, \&print_progress_and_commit);
113 print "... finished reverting batch\n";
115 print <<_SUMMARY_;
117 MARC record import report
118 ----------------------------------------
119 Batch number: $import_batch_id
120 Number of records deleted: $num_deleted
121 Number of errors: $num_errors
122 Number of records reverted: $num_reverted
123 Number of records ignored: $num_ignored
124 Number of items added: $num_items_deleted
126 _SUMMARY_
130 sub print_progress_and_commit {
131 my $recs = shift;
132 print "... processed $recs records\n";
133 $dbh->commit();
136 sub print_usage {
137 print <<_USAGE_;
138 $0: import a batch of staged MARC records into database.
140 Use this batch job to complete the import of a batch of
141 MARC records that was staged either by the batch job
142 stage_file.pl or by the Koha Tools option
143 "Stage MARC Records for Import".
145 Parameters:
146 --batch-number <#> number of the record batch
147 to import
148 --list-batches print a list of record batches
149 available to commit
150 --revert revert a batch instead of importing it
151 --help or -h show this message.
152 _USAGE_