Bug 6362: fixes display to re-embed items in OPAC MARC view
[koha.git] / misc / commit_biblios_file.pl
1 #!/usr/bin/perl
2
3 use strict;
4 #use warnings; FIXME - Bug 2505
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 C4::Context;
13 use C4::ImportBatch;
14 use Getopt::Long;
15
16 $| = 1;
17
18 # command-line parameters
19 my $batch_number = "";
20 my $list_batches = 0;
21 my $want_help = 0;
22
23 my $result = GetOptions(
24     'batch-number:s' => \$batch_number,
25     'list-batches'   => \$list_batches,
26     'h|help'         => \$want_help
27 );
28
29 if ($want_help or (not $batch_number and not $list_batches)) {
30     print_usage();
31     exit 0;
32 }
33
34 if ($list_batches) {
35     list_batches();
36     exit 0;
37 }
38
39 # FIXME dummy user so that logging won't fail
40 # in future, probably should tie to a real user account
41 C4::Context->set_userenv(0, 'batch', 0, 'batch', 'batch', 'batch', 'batch', 'batch');
42
43 my $dbh = C4::Context->dbh;
44 $dbh->{AutoCommit} = 0;
45 if ($batch_number =~ /^\d+$/ and $batch_number > 0) {
46     my $batch = GetImportBatch($batch_number);
47     die "$0: import batch $batch_number does not exist in database\n" unless defined $batch;
48     die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
49         unless $batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted";
50     process_batch($batch_number);
51     $dbh->commit();
52 } else {
53     die "$0: please specify a numeric batch ID\n";
54 }
55
56 exit 0;
57
58 sub list_batches {
59     my $results = GetAllImportBatches();
60     print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n", "#", "File name", "Batch comments", "Status");
61     print '-' x 5, ' ' , '-' x 25, ' ', '-' x 25, ' ', '-' x 10, "\n" ;
62     foreach my $batch (@{ $results}) {
63         if ($batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted") {
64             print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n",
65                           $batch->{'import_batch_id'},
66                           $batch->{'file_name'},
67                           $batch->{'comments'},
68                           $batch->{'import_status'});
69         }
70     }
71 }
72
73 sub process_batch {
74     my ($import_batch_id) = @_;
75
76     print "... importing MARC records -- please wait\n";
77     my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) = 
78         BatchCommitBibRecords($import_batch_id, '', 100, \&print_progress_and_commit);
79     print "... finished importing MARC records\n";
80
81     print <<_SUMMARY_;
82
83 MARC record import report
84 ----------------------------------------
85 Batch number:                    $import_batch_id
86 Number of new bibs added:        $num_added
87 Number of bibs replaced:         $num_updated
88 Number of bibs ignored:          $num_ignored
89 Number of items added:           $num_items_added
90 Number of items ignored:         $num_items_errored
91
92 Note: an item is ignored if its barcode is a 
93 duplicate of one already in the database.
94 _SUMMARY_
95 }
96
97 sub print_progress_and_commit {
98     my $recs = shift;
99     print "... processed $recs records\n";
100     $dbh->commit();
101 }
102
103 sub print_usage {
104     print <<_USAGE_;
105 $0: import a batch of staged MARC records into database.
106
107 Use this batch job to complete the import of a batch of
108 MARC records that was staged either by the batch job
109 stage_biblios_file.pl or by the Koha Tools option
110 "Stage MARC Records for Import".
111
112 Parameters:
113     --batch-number <#>   number of the record batch
114                          to import
115     --list-batches       print a list of record batches
116                          available to commit
117     --help or -h            show this message.
118 _USAGE_
119 }