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>.
29 # standard or CPAN modules used
32 use MARC::File::USMARC;
36 use C4::Auth qw( get_template_and_user );
37 use C4::Output qw( output_html_with_http_headers );
38 use C4::ImportBatch qw( RecordsFromMARCXMLFile RecordsFromISO2709File RecordsFromMarcPlugin BatchStageMarcRecords BatchFindDuplicates SetImportBatchMatcher SetImportBatchOverlayAction SetImportBatchNoMatchAction SetImportBatchItemAction );
40 use Koha::UploadedFiles;
41 use C4::BackgroundJob;
42 use C4::MarcModificationTemplates qw( GetModificationTemplates );
44 use Koha::ImportBatches;
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') || 'UTF-8';
59 my $format = $input->param('format') || 'ISO2709';
60 my $marc_modification_template = $input->param('marc_modification_template_id');
61 my $basketno = $input->param('basketno');
62 my $booksellerid = $input->param('booksellerid');
63 my $profile_id = $input->param('profile_id');
65 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
67 template_name => "tools/stage-marc-import.tt",
70 flagsrequired => { tools => 'stage_marc_import' },
75 SCRIPT_NAME => '/cgi-bin/koha/tools/stage-marc-import.pl',
76 uploadmarc => $fileID,
77 record_type => $record_type,
78 basketno => $basketno,
79 booksellerid => $booksellerid,
82 my %cookies = parse CGI::Cookie($cookie);
83 my $sessionID = $cookies{'CGISESSID'}->value;
84 if ($completedJobID) {
85 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
86 my $results = $job->results();
87 $template->param(map { $_ => $results->{$_} } keys %{ $results });
89 my $upload = Koha::UploadedFiles->find( $fileID );
90 my $file = $upload->full_path;
91 my $filename = $upload->filename;
93 my ( $errors, $marcrecords );
94 if( $format eq 'MARCXML' ) {
95 ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding);
96 } elsif( $format eq 'ISO2709' ) {
97 ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( $file, $record_type, $encoding );
98 } else { # plugin based
100 $marcrecords = C4::ImportBatch::RecordsFromMarcPlugin( $file, $format, $encoding );
102 warn "$filename: " . ( join ',', @$errors ) if @$errors;
103 # no need to exit if we have no records (or only errors) here
104 # BatchStageMarcRecords can handle that
107 if ($runinbackground) {
108 my $job_size = scalar(@$marcrecords);
109 # if we're matching, job size is doubled
110 $job_size *= 2 if ($matcher_id ne "");
111 $job = C4::BackgroundJob->new($sessionID, $filename, '/cgi-bin/koha/tools/stage-marc-import.pl', $job_size);
112 my $jobID = $job->id();
115 if (my $pid = fork) {
117 # return job ID as JSON
118 my $reply = CGI->new("");
119 print $reply->header(-type => 'text/html');
120 print '{"jobID":"' . $jobID . '"}';
122 } elsif (defined $pid) {
124 # close STDOUT/STDERR to signal to end CGI session with Apache
125 # Otherwise, the AJAX request to this script won't return properly
129 # fork failed, so exit immediately
130 warn "fork failed while attempting to run tools/stage-marc-import.pl as a background job: $!";
134 # if we get here, we're a child that has detached
139 my $schema = Koha::Database->new->schema;
140 $schema->storage->txn_begin;
143 my ( $batch_id, $num_valid, $num_items, @import_errors ) =
144 BatchStageMarcRecords(
145 $record_type, $encoding,
146 $marcrecords, $filename,
147 $marc_modification_template,
150 50, staging_progress_callback( $job )
154 my $ibatch = Koha::ImportBatches->find($batch_id);
155 $ibatch->set({profile_id => $profile_id})->store;
158 my $num_with_matches = 0;
159 my $checked_matches = 0;
160 my $matcher_failed = 0;
161 my $matcher_code = "";
162 if ($matcher_id ne "") {
163 my $matcher = C4::Matcher->fetch($matcher_id);
164 if (defined $matcher) {
165 $checked_matches = 1;
166 $matcher_code = $matcher->code();
168 BatchFindDuplicates( $batch_id, $matcher, 10, 50,
169 matching_progress_callback($job) );
170 SetImportBatchMatcher($batch_id, $matcher_id);
171 SetImportBatchOverlayAction($batch_id, $overlay_action);
172 SetImportBatchNoMatchAction($batch_id, $nomatch_action);
173 SetImportBatchItemAction($batch_id, $item_action);
174 $schema->storage->txn_commit;
177 $schema->storage->txn_rollback;
180 $schema->storage->txn_commit;
184 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,
193 booksellerid => $booksellerid,
194 basketno => $basketno
196 if ($runinbackground) {
197 $job->finish($results);
200 $template->param(staged => $num_valid,
201 matched => $num_with_matches,
202 num_items => $num_items,
203 import_errors => scalar(@import_errors),
204 total => $num_valid + scalar(@import_errors),
205 checked_matches => $checked_matches,
206 matcher_failed => $matcher_failed,
207 matcher_code => $matcher_code,
208 import_batch_id => $batch_id,
209 booksellerid => $booksellerid,
210 basketno => $basketno
216 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
217 $template->param( "UNIMARC" => 1 );
219 my @matchers = C4::Matcher::GetMatcherList();
220 $template->param( available_matchers => \@matchers );
222 my @templates = GetModificationTemplates();
223 $template->param( MarcModificationTemplatesLoop => \@templates );
225 if ( C4::Context->config('enable_plugins') ) {
227 my @plugins = Koha::Plugins->new()->GetPlugins({
230 $template->param( plugins => \@plugins );
234 output_html_with_http_headers $input, $cookie, $template->output;
238 sub staging_progress_callback {
241 my $progress = shift;
242 $job->progress($progress);
246 sub matching_progress_callback {
248 my $start_progress = $job->progress();
250 my $progress = shift;
251 $job->progress($start_progress + $progress);