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