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
;
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 $to_marc_plugin = $input->param('to_marc_plugin');
61 my $marc_modification_template = $input->param('marc_modification_template_id');
63 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
65 template_name
=> "tools/stage-marc-import.tt",
69 flagsrequired
=> { tools
=> 'stage_marc_import' },
75 SCRIPT_NAME
=> $ENV{'SCRIPT_NAME'},
76 uploadmarc
=> $fileID,
77 record_type
=> $record_type,
80 my %cookies = parse CGI
::Cookie
($cookie);
81 my $sessionID = $cookies{'CGISESSID'}->value;
82 if ($completedJobID) {
83 my $job = C4
::BackgroundJob
->fetch($sessionID, $completedJobID);
84 my $results = $job->results();
85 $template->param(map { $_ => $results->{$_} } keys %{ $results });
87 my $uploaded_file = C4
::UploadedFile
->fetch($sessionID, $fileID);
88 my $fh = $uploaded_file->fh();
97 my $filename = $uploaded_file->name();
100 if ($runinbackground) {
101 my $job_size = () = $marcrecord =~ /\035/g;
102 # if we're matching, job size is doubled
103 $job_size *= 2 if ($matcher_id ne "");
104 $job = C4
::BackgroundJob
->new($sessionID, $filename, $ENV{'SCRIPT_NAME'}, $job_size);
105 my $jobID = $job->id();
108 if (my $pid = fork) {
110 # return job ID as JSON
111 my $reply = CGI
->new("");
112 print $reply->header(-type
=> 'text/html');
113 print '{"jobID":"' . $jobID . '"}';
115 } elsif (defined $pid) {
117 # close STDOUT to signal to Apache that
118 # we're now running in the background
120 # close STDERR; # there is no good reason to close STDERR
122 # fork failed, so exit immediately
123 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job: $!";
127 # if we get here, we're a child that has detached
132 # New handle, as we're a child.
133 $dbh = C4
::Context
->dbh({new
=> 1});
134 $dbh->{AutoCommit
} = 0;
136 my ( $batch_id, $num_valid, $num_items, @import_errors ) =
137 BatchStageMarcRecords
(
138 $record_type, $encoding,
139 $marcrecord, $filename,
140 $to_marc_plugin, $marc_modification_template,
143 50, staging_progress_callback
( $job, $dbh )
146 my $num_with_matches = 0;
147 my $checked_matches = 0;
148 my $matcher_failed = 0;
149 my $matcher_code = "";
150 if ($matcher_id ne "") {
151 my $matcher = C4
::Matcher
->fetch($matcher_id);
152 if (defined $matcher) {
153 $checked_matches = 1;
154 $matcher_code = $matcher->code();
156 BatchFindDuplicates
( $batch_id, $matcher, 10, 50,
157 matching_progress_callback
( $job, $dbh ) );
158 SetImportBatchMatcher
($batch_id, $matcher_id);
159 SetImportBatchOverlayAction
($batch_id, $overlay_action);
160 SetImportBatchNoMatchAction
($batch_id, $nomatch_action);
161 SetImportBatchItemAction
($batch_id, $item_action);
171 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
181 if ($runinbackground) {
182 $job->finish($results);
184 $template->param(staged
=> $num_valid,
185 matched
=> $num_with_matches,
186 num_items
=> $num_items,
187 import_errors
=> scalar(@import_errors),
188 total
=> $num_valid + scalar(@import_errors),
189 checked_matches
=> $checked_matches,
190 matcher_failed
=> $matcher_failed,
191 matcher_code
=> $matcher_code,
192 import_batch_id
=> $batch_id
198 if ( C4
::Context
->preference("marcflavour") eq "UNIMARC" ) {
199 $template->param( "UNIMARC" => 1 );
201 my @matchers = C4
::Matcher
::GetMatcherList
();
202 $template->param( available_matchers
=> \
@matchers );
204 my @templates = GetModificationTemplates
();
205 $template->param( MarcModificationTemplatesLoop
=> \
@templates );
207 if ( C4
::Context
->preference('UseKohaPlugins') &&
208 C4
::Context
->config('enable_plugins') ) {
210 my @plugins = Koha
::Plugins
->new()->GetPlugins('to_marc');
211 $template->param( plugins
=> \
@plugins );
215 output_html_with_http_headers
$input, $cookie, $template->output;
219 sub staging_progress_callback
{
223 my $progress = shift;
224 $job->progress($progress);
228 sub matching_progress_callback
{
231 my $start_progress = $job->progress();
233 my $progress = shift;
234 $job->progress($start_progress + $progress);