3 # Script for handling import of MARC data into Koha db
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
15 # under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 3 of the License, or
17 # (at your option) any later version.
19 # Koha is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 #use warnings; FIXME - Bug 2505
30 # standard or CPAN modules used
33 use MARC
::File
::USMARC
;
43 use C4
::BackgroundJob
;
44 use C4
::MarcModificationTemplates
;
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 $record_type = $input->param('record_type');
58 my $encoding = $input->param('encoding');
59 my $marc_modification_template = $input->param('marc_modification_template_id');
61 my ($template, $loggedinuser, $cookie)
62 = get_template_and_user
({template_name
=> "tools/stage-marc-import.tt",
66 flagsrequired
=> {tools
=> 'stage_marc_import'},
70 $template->param(SCRIPT_NAME
=> $ENV{'SCRIPT_NAME'},
71 uploadmarc
=> $fileID);
73 my %cookies = parse CGI
::Cookie
($cookie);
74 my $sessionID = $cookies{'CGISESSID'}->value;
75 if ($completedJobID) {
76 my $job = C4
::BackgroundJob
->fetch($sessionID, $completedJobID);
77 my $results = $job->results();
78 $template->param(map { $_ => $results->{$_} } keys %{ $results });
80 my $uploaded_file = C4
::UploadedFile
->fetch($sessionID, $fileID);
81 my $fh = $uploaded_file->fh();
90 my $filename = $uploaded_file->name();
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();
101 if (my $pid = fork) {
103 # return job ID as JSON
104 my $reply = CGI
->new("");
105 print $reply->header(-type
=> 'text/html');
106 print '{"jobID":"' . $jobID . '"}';
108 } elsif (defined $pid) {
110 # close STDOUT to signal to Apache that
111 # we're now running in the background
113 # close STDERR; # there is no good reason to close STDERR
115 # fork failed, so exit immediately
116 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job: $!";
120 # if we get here, we're a child that has detached
125 # New handle, as we're a child.
126 $dbh = C4
::Context
->dbh({new
=> 1});
127 $dbh->{AutoCommit
} = 0;
129 my ( $batch_id, $num_valid, $num_items, @import_errors ) =
130 BatchStageMarcRecords
(
131 $record_type, $encoding,
132 $marcrecord, $filename,
133 $marc_modification_template, $comments,
136 staging_progress_callback
( $job, $dbh )
139 my $num_with_matches = 0;
140 my $checked_matches = 0;
141 my $matcher_failed = 0;
142 my $matcher_code = "";
143 if ($matcher_id ne "") {
144 my $matcher = C4
::Matcher
->fetch($matcher_id);
145 if (defined $matcher) {
146 $checked_matches = 1;
147 $matcher_code = $matcher->code();
149 BatchFindDuplicates
( $batch_id, $matcher, 10, 50,
150 matching_progress_callback
( $job, $dbh ) );
151 SetImportBatchMatcher
($batch_id, $matcher_id);
152 SetImportBatchOverlayAction
($batch_id, $overlay_action);
153 SetImportBatchNoMatchAction
($batch_id, $nomatch_action);
154 SetImportBatchItemAction
($batch_id, $item_action);
164 staged
=> $num_valid,
165 matched
=> $num_with_matches,
166 num_items
=> $num_items,
167 import_errors
=> scalar(@import_errors),
168 total
=> $num_valid + scalar(@import_errors),
169 checked_matches
=> $checked_matches,
170 matcher_failed
=> $matcher_failed,
171 matcher_code
=> $matcher_code,
172 import_batch_id
=> $batch_id
174 if ($runinbackground) {
175 $job->finish($results);
177 $template->param(staged
=> $num_valid,
178 matched
=> $num_with_matches,
179 num_items
=> $num_items,
180 import_errors
=> scalar(@import_errors),
181 total
=> $num_valid + scalar(@import_errors),
182 checked_matches
=> $checked_matches,
183 matcher_failed
=> $matcher_failed,
184 matcher_code
=> $matcher_code,
185 import_batch_id
=> $batch_id
191 if (C4
::Context
->preference("marcflavour") eq "UNIMARC") {
192 $template->param("UNIMARC" => 1);
194 my @matchers = C4
::Matcher
::GetMatcherList
();
195 $template->param(available_matchers
=> \
@matchers);
197 my @templates = GetModificationTemplates
();
198 $template->param( MarcModificationTemplatesLoop
=> \
@templates );
202 output_html_with_http_headers
$input, $cookie, $template->output;
206 sub staging_progress_callback
{
210 my $progress = shift;
211 $job->progress($progress);
215 sub matching_progress_callback
{
218 my $start_progress = $job->progress();
220 my $progress = shift;
221 $job->progress($start_progress + $progress);