6 # find Koha's Perl modules
7 # test carefully before changing this
9 eval { require "$FindBin::Bin/kohalib.pl" };
14 use C4::ImportBatch qw( GetAllImportBatches GetImportBatch BatchCommitRecords BatchRevertRecords );
15 use Getopt::Long qw( GetOptions );
19 # command-line parameters
20 my $batch_number = "";
26 my $result = GetOptions(
27 'batch-number:s' => \$batch_number,
28 'list-batches' => \$list_batches,
29 'framework:s' => \$framework,
31 'h|help' => \$want_help
34 if ($want_help or (not $batch_number and not $list_batches)) {
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');
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;
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);
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);
64 die "$0: please specify a numeric batch ID\n";
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'},
79 $batch->{'import_status'});
85 my ($import_batch_id) = @_;
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";
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
104 Note: an item is ignored if its barcode is a
105 duplicate of one already in the database.
110 my ($import_batch_id) = @_;
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";
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
132 sub print_progress_and_commit {
134 print "... processed $recs records\n";
140 $0: import a batch of staged MARC records into database.
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".
148 --batch-number <#> number of the record batch
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
154 --revert revert a batch instead of importing it
155 --help or -h show this message.