Bug 30477: Add new UNIMARC installer translation files
[koha.git] / misc / commit_file.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Koha::Script;
7 use C4::Context;
8 use C4::ImportBatch qw( GetAllImportBatches GetImportBatch BatchCommitRecords BatchRevertRecords );
9 use Getopt::Long qw( GetOptions );
10
11 $| = 1;
12
13 # command-line parameters
14 my $batch_number = "";
15 my $list_batches = 0;
16 my $revert = 0;
17 my $want_help = 0;
18 my $framework = '';
19
20 my $result = GetOptions(
21     'batch-number:s' => \$batch_number,
22     'list-batches'   => \$list_batches,
23     'framework:s'    => \$framework,
24     'revert'         => \$revert,
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');
41
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     if ($revert) {
48         die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
49             unless $batch->{'import_status'} eq "imported";
50         revert_batch($batch_number);
51     } else {
52         die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
53             unless $batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted";
54         process_batch($batch_number);
55     }
56     $dbh->commit();
57 } else {
58     die "$0: please specify a numeric batch ID\n";
59 }
60
61 exit 0;
62
63 sub list_batches {
64     my $results = GetAllImportBatches();
65     print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n", "#", "File name", "Batch comments", "Status");
66     print '-' x 5, ' ' , '-' x 25, ' ', '-' x 25, ' ', '-' x 10, "\n" ;
67     foreach my $batch (@{ $results}) {
68         if ($batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted") {
69             print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n",
70                           $batch->{'import_batch_id'},
71                           $batch->{'file_name'},
72                           $batch->{'comments'},
73                           $batch->{'import_status'});
74         }
75     }
76 }
77
78 sub process_batch {
79     my ($import_batch_id) = @_;
80
81     print "... importing MARC records -- please wait\n";
82     my ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored) =
83         BatchCommitRecords($import_batch_id, $framework, 100, \&print_progress_and_commit);
84     print "... finished importing MARC records\n";
85
86     print <<_SUMMARY_;
87
88 MARC record import report
89 ----------------------------------------
90 Batch number:                    $import_batch_id
91 Number of new records added:     $num_added
92 Number of records replaced:      $num_updated
93 Number of records ignored:       $num_ignored
94 Number of items added:           $num_items_added
95 Number of items replaced:        $num_items_replaced
96 Number of items ignored:         $num_items_errored
97
98 Note: an item is ignored if its barcode is a
99 duplicate of one already in the database.
100 _SUMMARY_
101 }
102
103 sub revert_batch {
104     my ($import_batch_id) = @_;
105
106     print "... reverting batch -- please wait\n";
107     my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) =
108         BatchRevertRecords($import_batch_id, 100, \&print_progress_and_commit);
109     print "... finished reverting batch\n";
110
111     print <<_SUMMARY_;
112
113 MARC record import report
114 ----------------------------------------
115 Batch number:                    $import_batch_id
116 Number of records deleted:       $num_deleted
117 Number of errors:                $num_errors
118 Number of records reverted:      $num_reverted
119 Number of records ignored:       $num_ignored
120 Number of items added:           $num_items_deleted
121
122 _SUMMARY_
123 }
124
125
126 sub print_progress_and_commit {
127     my $recs = shift;
128     print "... processed $recs records\n";
129     $dbh->commit();
130 }
131
132 sub print_usage {
133     print <<_USAGE_;
134 $0: import a batch of staged MARC records into database.
135
136 Use this batch job to complete the import of a batch of
137 MARC records that was staged either by the batch job
138 stage_file.pl or by the Koha Tools option
139 "Stage MARC Records for Import".
140
141 Parameters:
142     --batch-number <#>   number of the record batch
143                          to import
144     --framework <code>   add new records using this framework.  If
145                          omitted, the default framework is used.
146     --list-batches       print a list of record batches
147                          available to commit
148     --revert             revert a batch instead of importing it
149     --help or -h         show this message.
150 _USAGE_
151 }