Merge remote-tracking branch 'origin/new/bug_5184'
[koha.git] / tools / stage-marc-import.pl
blob0de73d46ec1adae2da13d14d1fc5b53eed4ac03a
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;
45 my $input = new CGI;
46 my $dbh = C4::Context->dbh;
47 $dbh->{AutoCommit} = 0;
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 $overlay_action = $input->param('overlay_action');
54 my $nomatch_action = $input->param('nomatch_action');
55 my $parse_items = $input->param('parse_items');
56 my $item_action = $input->param('item_action');
57 my $comments = $input->param('comments');
58 my $encoding = $input->param('encoding');
59 my ($template, $loggedinuser, $cookie)
60 = get_template_and_user({template_name => "tools/stage-marc-import.tmpl",
61 query => $input,
62 type => "intranet",
63 authnotrequired => 0,
64 flagsrequired => {tools => 'stage_marc_import'},
65 debug => 1,
66 });
68 $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'},
69 uploadmarc => $fileID);
71 my %cookies = parse CGI::Cookie($cookie);
72 my $sessionID = $cookies{'CGISESSID'}->value;
73 if ($completedJobID) {
74 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
75 my $results = $job->results();
76 $template->param(map { $_ => $results->{$_} } keys %{ $results });
77 } elsif ($fileID) {
78 my $uploaded_file = C4::UploadedFile->fetch($sessionID, $fileID);
79 my $fh = $uploaded_file->fh();
80 my $marcrecord='';
81 $/ = "\035";
82 while (<$fh>) {
83 s/^\s+//;
84 s/\s+$//;
85 $marcrecord.=$_;
88 my $filename = $uploaded_file->name();
89 my $job = undef;
90 my $staging_callback = sub { };
91 my $matching_callback = sub { };
92 if ($runinbackground) {
93 my $job_size = () = $marcrecord =~ /\035/g;
94 # if we're matching, job size is doubled
95 $job_size *= 2 if ($matcher_id ne "");
96 $job = C4::BackgroundJob->new($sessionID, $filename, $ENV{'SCRIPT_NAME'}, $job_size);
97 my $jobID = $job->id();
99 # fork off
100 if (my $pid = fork) {
101 # parent
102 # return job ID as JSON
104 # prevent parent exiting from
105 # destroying the kid's database handle
106 # FIXME: according to DBI doc, this may not work for Oracle
107 $dbh->{InactiveDestroy} = 1;
109 my $reply = CGI->new("");
110 print $reply->header(-type => 'text/html');
111 print '{"jobID":"' . $jobID . '"}';
112 exit 0;
113 } elsif (defined $pid) {
114 # child
115 # close STDOUT to signal to Apache that
116 # we're now running in the background
117 close STDOUT;
118 close STDERR;
119 } else {
120 # fork failed, so exit immediately
121 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
122 exit 0;
125 # if we get here, we're a child that has detached
126 # itself from Apache
127 $staging_callback = staging_progress_callback($job, $dbh);
128 $matching_callback = matching_progress_callback($job, $dbh);
132 # FIXME branch code
133 my ($batch_id, $num_valid, $num_items, @import_errors) = BatchStageMarcRecords($encoding, $marcrecord, $filename, $comments, '', $parse_items, 0, 50, staging_progress_callback($job, $dbh));
135 $dbh->commit();
137 my $num_with_matches = 0;
138 my $checked_matches = 0;
139 my $matcher_failed = 0;
140 my $matcher_code = "";
141 if ($matcher_id ne "") {
142 my $matcher = C4::Matcher->fetch($matcher_id);
143 if (defined $matcher) {
144 $checked_matches = 1;
145 $matcher_code = $matcher->code();
146 $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher,
147 10, 50, matching_progress_callback($job, $dbh));
148 SetImportBatchMatcher($batch_id, $matcher_id);
149 SetImportBatchOverlayAction($batch_id, $overlay_action);
150 SetImportBatchNoMatchAction($batch_id, $nomatch_action);
151 SetImportBatchItemAction($batch_id, $item_action);
152 $dbh->commit();
153 } else {
154 $matcher_failed = 1;
158 my $results = {
159 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
169 if ($runinbackground) {
170 $job->finish($results);
171 } else {
172 $template->param(staged => $num_valid,
173 matched => $num_with_matches,
174 num_items => $num_items,
175 import_errors => scalar(@import_errors),
176 total => $num_valid + scalar(@import_errors),
177 checked_matches => $checked_matches,
178 matcher_failed => $matcher_failed,
179 matcher_code => $matcher_code,
180 import_batch_id => $batch_id
184 } else {
185 # initial form
186 if (C4::Context->preference("marcflavour") eq "UNIMARC") {
187 $template->param("UNIMARC" => 1);
189 my @matchers = C4::Matcher::GetMatcherList();
190 $template->param(available_matchers => \@matchers);
193 output_html_with_http_headers $input, $cookie, $template->output;
195 exit 0;
197 sub staging_progress_callback {
198 my $job = shift;
199 my $dbh = shift;
200 return sub {
201 my $progress = shift;
202 $job->progress($progress);
203 $dbh->commit();
207 sub matching_progress_callback {
208 my $job = shift;
209 my $dbh = shift;
210 my $start_progress = $job->progress();
211 return sub {
212 my $progress = shift;
213 $job->progress($start_progress + $progress);
214 $dbh->commit();