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