Bug 19817: Add pref KohaManualLanguage
[koha.git] / misc / commit_file.pl
blobbd29df3b85a89c257623028b8b38117cce15fdb4
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 C4::Context;
13 use C4::ImportBatch;
14 use Getopt::Long;
16 $| = 1;
18 # command-line parameters
19 my $batch_number = "";
20 my $list_batches = 0;
21 my $revert = 0;
22 my $want_help = 0;
24 my $result = GetOptions(
25 'batch-number:s' => \$batch_number,
26 'list-batches' => \$list_batches,
27 'revert' => \$revert,
28 'h|help' => \$want_help
31 if ($want_help or (not $batch_number and not $list_batches)) {
32 print_usage();
33 exit 0;
36 if ($list_batches) {
37 list_batches();
38 exit 0;
41 # FIXME dummy user so that logging won't fail
42 # in future, probably should tie to a real user account
43 C4::Context->set_userenv(0, 'batch', 0, 'batch', 'batch', 'batch', 'batch', 'batch');
45 my $dbh = C4::Context->dbh;
46 $dbh->{AutoCommit} = 0;
47 if ($batch_number =~ /^\d+$/ and $batch_number > 0) {
48 my $batch = GetImportBatch($batch_number);
49 die "$0: import batch $batch_number does not exist in database\n" unless defined $batch;
50 if ($revert) {
51 die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
52 unless $batch->{'import_status'} eq "imported";
53 revert_batch($batch_number);
54 } else {
55 die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
56 unless $batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted";
57 process_batch($batch_number);
59 $dbh->commit();
60 } else {
61 die "$0: please specify a numeric batch ID\n";
64 exit 0;
66 sub list_batches {
67 my $results = GetAllImportBatches();
68 print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n", "#", "File name", "Batch comments", "Status");
69 print '-' x 5, ' ' , '-' x 25, ' ', '-' x 25, ' ', '-' x 10, "\n" ;
70 foreach my $batch (@{ $results}) {
71 if ($batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted") {
72 print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n",
73 $batch->{'import_batch_id'},
74 $batch->{'file_name'},
75 $batch->{'comments'},
76 $batch->{'import_status'});
81 sub process_batch {
82 my ($import_batch_id) = @_;
84 print "... importing MARC records -- please wait\n";
85 my ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored) =
86 BatchCommitRecords($import_batch_id, '', 100, \&print_progress_and_commit);
87 print "... finished importing MARC records\n";
89 print <<_SUMMARY_;
91 MARC record import report
92 ----------------------------------------
93 Batch number: $import_batch_id
94 Number of new records added: $num_added
95 Number of records replaced: $num_updated
96 Number of records ignored: $num_ignored
97 Number of items added: $num_items_added
98 Number of items replaced: $num_items_replaced
99 Number of items ignored: $num_items_errored
101 Note: an item is ignored if its barcode is a
102 duplicate of one already in the database.
103 _SUMMARY_
106 sub revert_batch {
107 my ($import_batch_id) = @_;
109 print "... reverting batch -- please wait\n";
110 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) =
111 BatchRevertRecords($import_batch_id, 100, \&print_progress_and_commit);
112 print "... finished reverting batch\n";
114 print <<_SUMMARY_;
116 MARC record import report
117 ----------------------------------------
118 Batch number: $import_batch_id
119 Number of records deleted: $num_deleted
120 Number of errors: $num_errors
121 Number of records reverted: $num_reverted
122 Number of records ignored: $num_ignored
123 Number of items added: $num_items_deleted
125 _SUMMARY_
129 sub print_progress_and_commit {
130 my $recs = shift;
131 print "... processed $recs records\n";
132 $dbh->commit();
135 sub print_usage {
136 print <<_USAGE_;
137 $0: import a batch of staged MARC records into database.
139 Use this batch job to complete the import of a batch of
140 MARC records that was staged either by the batch job
141 stage_file.pl or by the Koha Tools option
142 "Stage MARC Records for Import".
144 Parameters:
145 --batch-number <#> number of the record batch
146 to import
147 --list-batches print a list of record batches
148 available to commit
149 --revert revert a batch instead of importing it
150 --help or -h show this message.
151 _USAGE_