CleanBorrowers fixing.
[koha.git] / tools / stage-marc-import.pl
1 #!/usr/bin/perl
2
3 # Script for handling import of MARC data into Koha db
4 #   and Z39.50 lookups
5
6 # Koha library project  www.koha.org
7
8 # Licensed under the GPL
9
10 # Copyright 2000-2002 Katipo Communications
11 #
12 # This file is part of Koha.
13 #
14 # Koha is free software; you can redistribute it and/or modify it under the
15 # terms of the GNU General Public License as published by the Free Software
16 # Foundation; either version 2 of the License, or (at your op) any later
17 # version.
18 #
19 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
20 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License along with
24 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
25 # Suite 330, Boston, MA  02111-1307 USA
26
27 use strict;
28
29 # standard or CPAN modules used
30 use CGI;
31 use CGI::Cookie;
32 use MARC::File::USMARC;
33
34 # Koha modules used
35 use C4::Context;
36 use C4::Auth;
37 use C4::Input;
38 use C4::Output;
39 use C4::Biblio;
40 use C4::ImportBatch;
41 use C4::Matcher;
42 use C4::UploadedFile;
43 use C4::BackgroundJob;
44
45 my $input = new CGI;
46 my $dbh = C4::Context->dbh;
47 $dbh->{AutoCommit} = 0;
48
49 my $fileID=$input->param('uploadedfileid');
50 my $runinbackground = $input->param('runinbackground');
51 my $completedJobID = $input->param('completedJobID');
52 my $matcher_id = $input->param('matcher');
53 my $parse_items = $input->param('parse_items');
54 my $comments = $input->param('comments');
55 my $syntax = $input->param('syntax');
56 my ($template, $loggedinuser, $cookie)
57         = get_template_and_user({template_name => "tools/stage-marc-import.tmpl",
58                                         query => $input,
59                                         type => "intranet",
60                                         authnotrequired => 0,
61                                         flagsrequired => {tools => 1},
62                                         debug => 1,
63                                         });
64
65 $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'},
66                                                 uploadmarc => $fileID);
67
68 my %cookies = parse CGI::Cookie($cookie);
69 my $sessionID = $cookies{'CGISESSID'}->value;
70 if ($completedJobID) {
71     my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
72     my $results = $job->results();
73     $template->param(map { $_ => $results->{$_} } keys %{ $results });
74 } elsif ($fileID) {
75     my $uploaded_file = C4::UploadedFile->fetch($sessionID, $fileID);
76     my $fh = $uploaded_file->fh();
77         my $marcrecord='';
78     $/ = "\035";
79         while (<$fh>) {
80         s/^\s+//;
81         s/\s+$//;
82                 $marcrecord.=$_;
83         }
84
85     my $filename = $uploaded_file->name();
86     my $job = undef;
87     my $staging_callback = sub { };
88     my $matching_callback = sub { };
89     if ($runinbackground) {
90         my $job_size = () = $marcrecord =~ /\035/g;
91         # if we're matching, job size is doubled
92         $job_size *= 2 if ($matcher_id ne "");
93         $job = C4::BackgroundJob->new($sessionID, $filename, $ENV{'SCRIPT_NAME'}, $job_size);
94         my $jobID = $job->id();
95
96         # fork off
97         if (my $pid = fork) {
98             # parent
99             # return job ID as JSON
100             
101             # prevent parent exiting from
102             # destroying the kid's database handle
103             # FIXME: according to DBI doc, this may not work for Oracle
104             $dbh->{InactiveDestroy}  = 1;
105
106             my $reply = CGI->new("");
107             print $reply->header(-type => 'text/html');
108             print "{ jobID: '$jobID' }";
109             exit 0;
110         } elsif (defined $pid) {
111             # child
112             # close STDOUT to signal to Apache that
113             # we're now running in the background
114             close STDOUT;
115             close STDERR;
116         } else {
117             # fork failed, so exit immediately
118             warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
119             exit 0;
120         }
121
122         # if we get here, we're a child that has detached
123         # itself from Apache
124         $staging_callback = staging_progress_callback($job, $dbh);
125         $matching_callback = matching_progress_callback($job, $dbh);
126
127     }
128
129     # FIXME branch code
130     my ($batch_id, $num_valid, $num_items, @import_errors) = BatchStageMarcRecords($syntax, $marcrecord, $filename, 
131                                                                                    $comments, '', $parse_items, 0,
132                                                                                    50, staging_progress_callback($job, $dbh));
133     $dbh->commit();
134     my $num_with_matches = 0;
135     my $checked_matches = 0;
136     my $matcher_failed = 0;
137     my $matcher_code = "";
138     if ($matcher_id ne "") {
139         my $matcher = C4::Matcher->fetch($matcher_id);
140         if (defined $matcher) {
141             $checked_matches = 1;
142             $matcher_code = $matcher->code();
143             $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, 10, 50, matching_progress_callback($job, $dbh));
144             SetImportBatchMatcher($batch_id, $matcher_id);
145             $dbh->commit();
146         } else {
147             $matcher_failed = 1;
148         }
149     }
150
151     my $results = {
152             staged => $num_valid,
153             matched => $num_with_matches,
154         num_items => $num_items,
155         import_errors => scalar(@import_errors),
156         total => $num_valid + scalar(@import_errors),
157         checked_matches => $checked_matches,
158         matcher_failed => $matcher_failed,
159         matcher_code => $matcher_code,
160         import_batch_id => $batch_id
161     };
162     if ($runinbackground) {
163         $job->finish($results);
164     } else {
165             $template->param(staged => $num_valid,
166                              matched => $num_with_matches,
167                          num_items => $num_items,
168                          import_errors => scalar(@import_errors),
169                          total => $num_valid + scalar(@import_errors),
170                          checked_matches => $checked_matches,
171                          matcher_failed => $matcher_failed,
172                          matcher_code => $matcher_code,
173                          import_batch_id => $batch_id
174                         );
175     }
176
177 } else {
178     # initial form
179     my @matchers = C4::Matcher::GetMatcherList();
180     $template->param(available_matchers => \@matchers);
181 }
182
183 output_html_with_http_headers $input, $cookie, $template->output;
184
185 exit 0;
186
187 sub staging_progress_callback {
188     my $job = shift;
189     my $dbh = shift;
190     return sub {
191         my $progress = shift;
192         $job->progress($progress);
193         $dbh->commit();
194     }
195 }
196
197 sub matching_progress_callback {
198     my $job = shift;
199     my $dbh = shift;
200     my $start_progress = $job->progress();
201     return sub {
202         my $progress = shift;
203         $job->progress($start_progress + $progress);
204         $dbh->commit();
205     }
206 }