removed incorrect module import
[koha.git] / tools / stage-marc-import.pl
blobd8de9055f81b6b494200cc4650a60e420fb9a674
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.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
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 with
24 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
25 # Suite 330, Boston, MA 02111-1307 USA
27 use strict;
29 # standard or CPAN modules used
30 use CGI;
31 use CGI::Cookie;
32 use MARC::File::USMARC;
34 # Koha modules used
35 use C4::Context;
36 use C4::Auth;
37 use C4::Output;
38 use C4::Biblio;
39 use C4::ImportBatch;
40 use C4::Matcher;
41 use C4::UploadedFile;
42 use C4::BackgroundJob;
44 my $input = new CGI;
45 my $dbh = C4::Context->dbh;
46 $dbh->{AutoCommit} = 0;
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 $overlay_action = $input->param('overlay_action');
53 my $nomatch_action = $input->param('nomatch_action');
54 my $parse_items = $input->param('parse_items');
55 my $item_action = $input->param('item_action');
56 my $comments = $input->param('comments');
57 my $syntax = $input->param('syntax');
58 my ($template, $loggedinuser, $cookie)
59 = get_template_and_user({template_name => "tools/stage-marc-import.tmpl",
60 query => $input,
61 type => "intranet",
62 authnotrequired => 0,
63 flagsrequired => {tools => 'stage_marc_import'},
64 debug => 1,
65 });
67 $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'},
68 uploadmarc => $fileID);
70 my %cookies = parse CGI::Cookie($cookie);
71 my $sessionID = $cookies{'CGISESSID'}->value;
72 if ($completedJobID) {
73 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
74 my $results = $job->results();
75 $template->param(map { $_ => $results->{$_} } keys %{ $results });
76 } elsif ($fileID) {
77 my $uploaded_file = C4::UploadedFile->fetch($sessionID, $fileID);
78 my $fh = $uploaded_file->fh();
79 my $marcrecord='';
80 $/ = "\035";
81 while (<$fh>) {
82 s/^\s+//;
83 s/\s+$//;
84 $marcrecord.=$_;
87 my $filename = $uploaded_file->name();
88 my $job = undef;
89 my $staging_callback = sub { };
90 my $matching_callback = sub { };
91 if ($runinbackground) {
92 my $job_size = () = $marcrecord =~ /\035/g;
93 # if we're matching, job size is doubled
94 $job_size *= 2 if ($matcher_id ne "");
95 $job = C4::BackgroundJob->new($sessionID, $filename, $ENV{'SCRIPT_NAME'}, $job_size);
96 my $jobID = $job->id();
98 # fork off
99 if (my $pid = fork) {
100 # parent
101 # return job ID as JSON
103 # prevent parent exiting from
104 # destroying the kid's database handle
105 # FIXME: according to DBI doc, this may not work for Oracle
106 $dbh->{InactiveDestroy} = 1;
108 my $reply = CGI->new("");
109 print $reply->header(-type => 'text/html');
110 print "{ jobID: '$jobID' }";
111 exit 0;
112 } elsif (defined $pid) {
113 # child
114 # close STDOUT to signal to Apache that
115 # we're now running in the background
116 close STDOUT;
117 close STDERR;
118 } else {
119 # fork failed, so exit immediately
120 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
121 exit 0;
124 # if we get here, we're a child that has detached
125 # itself from Apache
126 $staging_callback = staging_progress_callback($job, $dbh);
127 $matching_callback = matching_progress_callback($job, $dbh);
131 # FIXME branch code
132 my ($batch_id, $num_valid, $num_items, @import_errors) = BatchStageMarcRecords($syntax, $marcrecord, $filename,
133 $comments, '', $parse_items, 0,
134 50, staging_progress_callback($job, $dbh));
135 $dbh->commit();
136 my $num_with_matches = 0;
137 my $checked_matches = 0;
138 my $matcher_failed = 0;
139 my $matcher_code = "";
140 if ($matcher_id ne "") {
141 my $matcher = C4::Matcher->fetch($matcher_id);
142 if (defined $matcher) {
143 $checked_matches = 1;
144 $matcher_code = $matcher->code();
145 $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher,
146 10, 50, matching_progress_callback($job, $dbh));
147 SetImportBatchMatcher($batch_id, $matcher_id);
148 SetImportBatchOverlayAction($batch_id, $overlay_action);
149 SetImportBatchNoMatchAction($batch_id, $nomatch_action);
150 SetImportBatchItemAction($batch_id, $item_action);
151 $dbh->commit();
152 } else {
153 $matcher_failed = 1;
157 my $results = {
158 staged => $num_valid,
159 matched => $num_with_matches,
160 num_items => $num_items,
161 import_errors => scalar(@import_errors),
162 total => $num_valid + scalar(@import_errors),
163 checked_matches => $checked_matches,
164 matcher_failed => $matcher_failed,
165 matcher_code => $matcher_code,
166 import_batch_id => $batch_id
168 if ($runinbackground) {
169 $job->finish($results);
170 } else {
171 $template->param(staged => $num_valid,
172 matched => $num_with_matches,
173 num_items => $num_items,
174 import_errors => scalar(@import_errors),
175 total => $num_valid + scalar(@import_errors),
176 checked_matches => $checked_matches,
177 matcher_failed => $matcher_failed,
178 matcher_code => $matcher_code,
179 import_batch_id => $batch_id
183 } else {
184 # initial form
185 if (C4::Context->preference("marcflavour") eq "UNIMARC") {
186 $template->param("UNIMARC" => 1);
188 my @matchers = C4::Matcher::GetMatcherList();
189 $template->param(available_matchers => \@matchers);
192 output_html_with_http_headers $input, $cookie, $template->output;
194 exit 0;
196 sub staging_progress_callback {
197 my $job = shift;
198 my $dbh = shift;
199 return sub {
200 my $progress = shift;
201 $job->progress($progress);
202 $dbh->commit();
206 sub matching_progress_callback {
207 my $job = shift;
208 my $dbh = shift;
209 my $start_progress = $job->progress();
210 return sub {
211 my $progress = shift;
212 $job->progress($start_progress + $progress);
213 $dbh->commit();