Bug 17866: [QA Follow-up] Reword message for missing email address
[koha.git] / tools / stage-marc-import.pl
blob63dd4e51305d4db6cd6ce6aaa36b6eb72a4dee5a
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
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>.
27 use strict;
28 #use warnings; FIXME - Bug 2505
30 # standard or CPAN modules used
31 use CGI qw ( -utf8 );
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 Koha::UploadedFiles;
43 use C4::BackgroundJob;
44 use C4::MarcModificationTemplates;
45 use Koha::Plugins;
47 my $input = new CGI;
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') || 'UTF-8';
60 my $format = $input->param('format') || 'ISO2709';
61 my $to_marc_plugin = $input->param('to_marc_plugin');
62 my $marc_modification_template = $input->param('marc_modification_template_id');
64 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
66 template_name => "tools/stage-marc-import.tt",
67 query => $input,
68 type => "intranet",
69 authnotrequired => 0,
70 flagsrequired => { tools => 'stage_marc_import' },
71 debug => 1,
75 $template->param(
76 SCRIPT_NAME => '/cgi-bin/koha/tools/stage-marc-import.pl',
77 uploadmarc => $fileID,
78 record_type => $record_type,
81 my %cookies = parse CGI::Cookie($cookie);
82 my $sessionID = $cookies{'CGISESSID'}->value;
83 if ($completedJobID) {
84 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
85 my $results = $job->results();
86 $template->param(map { $_ => $results->{$_} } keys %{ $results });
87 } elsif ($fileID) {
88 my $upload = Koha::UploadedFiles->find( $fileID );
89 my $file = $upload->full_path;
90 my $filename = $upload->filename;
92 my ( $errors, $marcrecords );
93 if( $format eq 'MARCXML' ) {
94 ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding);
95 } else {
96 ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( $file, $record_type, $encoding );
98 warn "$filename: " . ( join ',', @$errors ) if @$errors;
99 # no need to exit if we have no records (or only errors) here
100 # BatchStageMarcRecords can handle that
102 my $job = undef;
103 my $dbh;
104 if ($runinbackground) {
105 my $job_size = scalar(@$marcrecords);
106 # if we're matching, job size is doubled
107 $job_size *= 2 if ($matcher_id ne "");
108 $job = C4::BackgroundJob->new($sessionID, $filename, '/cgi-bin/koha/tools/stage-marc-import.pl', $job_size);
109 my $jobID = $job->id();
111 # fork off
112 if (my $pid = fork) {
113 # parent
114 # return job ID as JSON
115 my $reply = CGI->new("");
116 print $reply->header(-type => 'text/html');
117 print '{"jobID":"' . $jobID . '"}';
118 exit 0;
119 } elsif (defined $pid) {
120 # child
121 # close STDOUT to signal to Apache that
122 # we're now running in the background
123 close STDOUT;
124 # close STDERR; # there is no good reason to close STDERR
125 } else {
126 # fork failed, so exit immediately
127 warn "fork failed while attempting to run tools/stage-marc-import.pl as a background job: $!";
128 exit 0;
131 # if we get here, we're a child that has detached
132 # itself from Apache
136 # New handle, as we're a child.
137 $dbh = C4::Context->dbh({new => 1});
138 $dbh->{AutoCommit} = 0;
139 # FIXME branch code
140 my ( $batch_id, $num_valid, $num_items, @import_errors ) =
141 BatchStageMarcRecords(
142 $record_type, $encoding,
143 $marcrecords, $filename,
144 $to_marc_plugin, $marc_modification_template,
145 $comments, '',
146 $parse_items, 0,
147 50, staging_progress_callback( $job, $dbh )
150 my $num_with_matches = 0;
151 my $checked_matches = 0;
152 my $matcher_failed = 0;
153 my $matcher_code = "";
154 if ($matcher_id ne "") {
155 my $matcher = C4::Matcher->fetch($matcher_id);
156 if (defined $matcher) {
157 $checked_matches = 1;
158 $matcher_code = $matcher->code();
159 $num_with_matches =
160 BatchFindDuplicates( $batch_id, $matcher, 10, 50,
161 matching_progress_callback( $job, $dbh ) );
162 SetImportBatchMatcher($batch_id, $matcher_id);
163 SetImportBatchOverlayAction($batch_id, $overlay_action);
164 SetImportBatchNoMatchAction($batch_id, $nomatch_action);
165 SetImportBatchItemAction($batch_id, $item_action);
166 $dbh->commit();
167 } else {
168 $matcher_failed = 1;
170 } else {
171 $dbh->commit();
174 my $results = {
175 staged => $num_valid,
176 matched => $num_with_matches,
177 num_items => $num_items,
178 import_errors => scalar(@import_errors),
179 total => $num_valid + scalar(@import_errors),
180 checked_matches => $checked_matches,
181 matcher_failed => $matcher_failed,
182 matcher_code => $matcher_code,
183 import_batch_id => $batch_id
185 if ($runinbackground) {
186 $job->finish($results);
187 exit 0;
188 } else {
189 $template->param(staged => $num_valid,
190 matched => $num_with_matches,
191 num_items => $num_items,
192 import_errors => scalar(@import_errors),
193 total => $num_valid + scalar(@import_errors),
194 checked_matches => $checked_matches,
195 matcher_failed => $matcher_failed,
196 matcher_code => $matcher_code,
197 import_batch_id => $batch_id
201 } else {
202 # initial form
203 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
204 $template->param( "UNIMARC" => 1 );
206 my @matchers = C4::Matcher::GetMatcherList();
207 $template->param( available_matchers => \@matchers );
209 my @templates = GetModificationTemplates();
210 $template->param( MarcModificationTemplatesLoop => \@templates );
212 if ( C4::Context->preference('UseKohaPlugins') &&
213 C4::Context->config('enable_plugins') ) {
215 my @plugins = Koha::Plugins->new()->GetPlugins({
216 method => 'to_marc',
218 $template->param( plugins => \@plugins );
222 output_html_with_http_headers $input, $cookie, $template->output;
224 exit 0;
226 sub staging_progress_callback {
227 my $job = shift;
228 my $dbh = shift;
229 return sub {
230 my $progress = shift;
231 $job->progress($progress);
235 sub matching_progress_callback {
236 my $job = shift;
237 my $dbh = shift;
238 my $start_progress = $job->progress();
239 return sub {
240 my $progress = shift;
241 $job->progress($start_progress + $progress);