Bug 9032: (follow-up) loop variable and Cancel button
[koha.git] / tools / stage-marc-import.pl
blob8d6ab1a00e6960a3630a21cfa4c7da9343923377
1 #!/usr/bin/perl
3 # Script for handling import of MARC data into Koha db
4 # and Z39.50 lookups
6 # Koha library project www.koha-community.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 option) any later
17 # version.
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
24 # with Koha; if not, write to the Free Software Foundation, Inc.,
25 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 use strict;
28 #use warnings; FIXME - Bug 2505
30 # standard or CPAN modules used
31 use CGI;
32 use CGI::Cookie;
33 use MARC::File::USMARC;
35 # Koha modules used
36 use C4::Context;
37 use C4::Auth;
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 use C4::MarcModificationTemplates;
46 my $input = new CGI;
47 my $dbh = C4::Context->dbh;
48 $dbh->{AutoCommit} = 0;
50 my $fileID=$input->param('uploadedfileid');
51 my $runinbackground = $input->param('runinbackground');
52 my $completedJobID = $input->param('completedJobID');
53 my $matcher_id = $input->param('matcher');
54 my $overlay_action = $input->param('overlay_action');
55 my $nomatch_action = $input->param('nomatch_action');
56 my $parse_items = $input->param('parse_items');
57 my $item_action = $input->param('item_action');
58 my $comments = $input->param('comments');
59 my $record_type = $input->param('record_type');
60 my $encoding = $input->param('encoding');
61 my $marc_modification_template = $input->param('marc_modification_template_id');
63 my ($template, $loggedinuser, $cookie)
64 = get_template_and_user({template_name => "tools/stage-marc-import.tmpl",
65 query => $input,
66 type => "intranet",
67 authnotrequired => 0,
68 flagsrequired => {tools => 'stage_marc_import'},
69 debug => 1,
70 });
72 $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'},
73 uploadmarc => $fileID);
75 my %cookies = parse CGI::Cookie($cookie);
76 my $sessionID = $cookies{'CGISESSID'}->value;
77 if ($completedJobID) {
78 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
79 my $results = $job->results();
80 $template->param(map { $_ => $results->{$_} } keys %{ $results });
81 } elsif ($fileID) {
82 my $uploaded_file = C4::UploadedFile->fetch($sessionID, $fileID);
83 my $fh = $uploaded_file->fh();
84 my $marcrecord='';
85 $/ = "\035";
86 while (<$fh>) {
87 s/^\s+//;
88 s/\s+$//;
89 $marcrecord.=$_;
92 my $filename = $uploaded_file->name();
93 my $job = undef;
94 my $staging_callback = sub { };
95 my $matching_callback = sub { };
96 if ($runinbackground) {
97 my $job_size = () = $marcrecord =~ /\035/g;
98 # if we're matching, job size is doubled
99 $job_size *= 2 if ($matcher_id ne "");
100 $job = C4::BackgroundJob->new($sessionID, $filename, $ENV{'SCRIPT_NAME'}, $job_size);
101 my $jobID = $job->id();
103 # fork off
104 if (my $pid = fork) {
105 # parent
106 # return job ID as JSON
108 # prevent parent exiting from
109 # destroying the kid's database handle
110 # FIXME: according to DBI doc, this may not work for Oracle
111 $dbh->{InactiveDestroy} = 1;
113 my $reply = CGI->new("");
114 print $reply->header(-type => 'text/html');
115 print '{"jobID":"' . $jobID . '"}';
116 exit 0;
117 } elsif (defined $pid) {
118 # child
119 # close STDOUT to signal to Apache that
120 # we're now running in the background
121 close STDOUT;
122 # close STDERR; # there is no good reason to close STDERR
123 } else {
124 # fork failed, so exit immediately
125 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
126 exit 0;
129 # if we get here, we're a child that has detached
130 # itself from Apache
131 $staging_callback = staging_progress_callback($job, $dbh);
132 $matching_callback = matching_progress_callback($job, $dbh);
136 # FIXME branch code
137 my ($batch_id, $num_valid, $num_items, @import_errors) = BatchStageMarcRecords($record_type, $encoding, $marcrecord, $filename, $marc_modification_template, $comments, '', $parse_items, 0, 50, staging_progress_callback($job, $dbh));
139 $dbh->commit();
141 my $num_with_matches = 0;
142 my $checked_matches = 0;
143 my $matcher_failed = 0;
144 my $matcher_code = "";
145 if ($matcher_id ne "") {
146 my $matcher = C4::Matcher->fetch($matcher_id);
147 if (defined $matcher) {
148 $checked_matches = 1;
149 $matcher_code = $matcher->code();
150 $num_with_matches = BatchFindDuplicates($batch_id, $matcher,
151 10, 50, matching_progress_callback($job, $dbh));
152 SetImportBatchMatcher($batch_id, $matcher_id);
153 SetImportBatchOverlayAction($batch_id, $overlay_action);
154 SetImportBatchNoMatchAction($batch_id, $nomatch_action);
155 SetImportBatchItemAction($batch_id, $item_action);
156 $dbh->commit();
157 } else {
158 $matcher_failed = 1;
162 my $results = {
163 staged => $num_valid,
164 matched => $num_with_matches,
165 num_items => $num_items,
166 import_errors => scalar(@import_errors),
167 total => $num_valid + scalar(@import_errors),
168 checked_matches => $checked_matches,
169 matcher_failed => $matcher_failed,
170 matcher_code => $matcher_code,
171 import_batch_id => $batch_id
173 if ($runinbackground) {
174 $job->finish($results);
175 } else {
176 $template->param(staged => $num_valid,
177 matched => $num_with_matches,
178 num_items => $num_items,
179 import_errors => scalar(@import_errors),
180 total => $num_valid + scalar(@import_errors),
181 checked_matches => $checked_matches,
182 matcher_failed => $matcher_failed,
183 matcher_code => $matcher_code,
184 import_batch_id => $batch_id
190 } else {
191 # initial form
192 if (C4::Context->preference("marcflavour") eq "UNIMARC") {
193 $template->param("UNIMARC" => 1);
195 my @matchers = C4::Matcher::GetMatcherList();
196 $template->param(available_matchers => \@matchers);
198 my @templates = GetModificationTemplates();
199 $template->param( MarcModificationTemplatesLoop => \@templates );
203 output_html_with_http_headers $input, $cookie, $template->output;
205 exit 0;
207 sub staging_progress_callback {
208 my $job = shift;
209 my $dbh = shift;
210 return sub {
211 my $progress = shift;
212 $job->progress($progress);
213 $dbh->commit();
217 sub matching_progress_callback {
218 my $job = shift;
219 my $dbh = shift;
220 my $start_progress = $job->progress();
221 return sub {
222 my $progress = shift;
223 $job->progress($start_progress + $progress);
224 $dbh->commit();