3 # Script for handling import of MARC data into Koha db
6 # Koha library project www.koha.org
8 # Licensed under the GPL
10 # Copyright 2000-2002 Katipo Communications
12 # This file is part of Koha.
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
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.
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
29 # standard or CPAN modules used
32 use MARC::File::USMARC;
43 use C4::BackgroundJob;
46 my $dbh = C4::Context->dbh;
48 my $fileID=$input->param('uploadedfileid');
49 my $runinbackground = $input->param('runinbackground');
50 my $completedJobID = $input->param('completedJobID');
51 my $matcher_id = $input->param('matcher');
52 my $parse_items = $input->param('parse_items');
53 my $comments = $input->param('comments');
54 my $syntax = $input->param('syntax');
55 my ($template, $loggedinuser, $cookie)
56 = get_template_and_user({template_name => "tools/stage-marc-import.tmpl",
60 flagsrequired => {tools => 1},
64 $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'},
65 uploadmarc => $fileID);
67 my %cookies = parse CGI::Cookie($cookie);
68 my $sessionID = $cookies{'CGISESSID'}->value;
69 if ($completedJobID) {
70 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
71 my $results = $job->results();
72 $template->param(map { $_ => $results->{$_} } keys %{ $results });
74 my $uploaded_file = C4::UploadedFile->fetch($sessionID, $fileID);
75 my $fh = $uploaded_file->fh();
81 my $filename = $uploaded_file->name();
83 my $staging_callback = sub { };
84 my $matching_callback = sub { };
85 if ($runinbackground) {
86 my $job_size = () = $marcrecord =~ /\035/g;
87 # if we're matching, job size is doubled
88 $job_size *= 2 if ($matcher_id ne "");
89 $job = C4::BackgroundJob->new($sessionID, $filename, $ENV{'SCRIPT_NAME'}, $job_size);
90 my $jobID = $job->id();
95 # return job ID as JSON
97 # prevent parent exiting from
98 # destroying the kid's database handle
99 # FIXME: according to DBI doc, this may not work for Oracle
100 $dbh->{InactiveDestroy} = 1;
102 my $reply = CGI->new("");
103 print $reply->header(-type => 'text/html');
104 print "{ jobID: '$jobID' }";
106 } elsif (defined $pid) {
108 # close STDOUT to signal to Apache that
109 # we're now running in the background
113 # fork failed, so exit immediately
114 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
118 # if we get here, we're a child that has detached
120 $staging_callback = staging_progress_callback($job);
121 $matching_callback = matching_progress_callback($job);
126 my ($batch_id, $num_valid, $num_items, @import_errors) = BatchStageMarcRecords($syntax, $marcrecord, $filename,
127 $comments, '', $parse_items, 0,
128 50, staging_progress_callback($job));
129 my $num_with_matches = 0;
130 my $checked_matches = 0;
131 my $matcher_failed = 0;
132 my $matcher_code = "";
133 if ($matcher_id ne "") {
134 my $matcher = C4::Matcher->fetch($matcher_id);
135 if (defined $matcher) {
136 $checked_matches = 1;
137 $matcher_code = $matcher->code();
138 $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, 10, 50, matching_progress_callback($job));
139 SetImportBatchMatcher($batch_id, $matcher_id);
146 staged => $num_valid,
147 matched => $num_with_matches,
148 num_items => $num_items,
149 import_errors => scalar(@import_errors),
150 total => $num_valid + scalar(@import_errors),
151 checked_matches => $checked_matches,
152 matcher_failed => $matcher_failed,
153 matcher_code => $matcher_code,
154 import_batch_id => $batch_id
156 if ($runinbackground) {
157 $job->finish($results);
159 $template->param(staged => $num_valid,
160 matched => $num_with_matches,
161 num_items => $num_items,
162 import_errors => scalar(@import_errors),
163 total => $num_valid + scalar(@import_errors),
164 checked_matches => $checked_matches,
165 matcher_failed => $matcher_failed,
166 matcher_code => $matcher_code,
167 import_batch_id => $batch_id
173 my @matchers = C4::Matcher::GetMatcherList();
174 $template->param(available_matchers => \@matchers);
177 output_html_with_http_headers $input, $cookie, $template->output;
181 sub staging_progress_callback {
184 my $progress = shift;
185 $job->progress($progress);
189 sub matching_progress_callback {
191 my $start_progress = $job->progress();
193 my $progress = shift;
194 $job->progress($start_progress + $progress);