Bug 19502: (follow-up) Pass parameters to avoid making templates depend on search...
[koha.git] / tools / stage-marc-import.pl
blobf2acc58d9c3a4cf4fa8292a5f979fd3326876076
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 Modern::Perl;
29 # standard or CPAN modules used
30 use CGI qw ( -utf8 );
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 Koha::UploadedFiles;
42 use C4::BackgroundJob;
43 use C4::MarcModificationTemplates;
44 use Koha::Plugins;
46 my $input = new CGI;
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');
62 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
64 template_name => "tools/stage-marc-import.tt",
65 query => $input,
66 type => "intranet",
67 authnotrequired => 0,
68 flagsrequired => { tools => 'stage_marc_import' },
69 debug => 1,
73 $template->param(
74 SCRIPT_NAME => '/cgi-bin/koha/tools/stage-marc-import.pl',
75 uploadmarc => $fileID,
76 record_type => $record_type,
79 my %cookies = parse CGI::Cookie($cookie);
80 my $sessionID = $cookies{'CGISESSID'}->value;
81 if ($completedJobID) {
82 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
83 my $results = $job->results();
84 $template->param(map { $_ => $results->{$_} } keys %{ $results });
85 } elsif ($fileID) {
86 my $upload = Koha::UploadedFiles->find( $fileID );
87 my $file = $upload->full_path;
88 my $filename = $upload->filename;
90 my ( $errors, $marcrecords );
91 if( $format eq 'MARCXML' ) {
92 ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding);
93 } elsif( $format eq 'ISO2709' ) {
94 ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( $file, $record_type, $encoding );
95 } else { # plugin based
96 $errors = [];
97 $marcrecords = C4::ImportBatch::RecordsFromMarcPlugin( $file, $format, $encoding );
99 warn "$filename: " . ( join ',', @$errors ) if @$errors;
100 # no need to exit if we have no records (or only errors) here
101 # BatchStageMarcRecords can handle that
103 my $job = undef;
104 my $dbh;
105 if ($runinbackground) {
106 my $job_size = scalar(@$marcrecords);
107 # if we're matching, job size is doubled
108 $job_size *= 2 if ($matcher_id ne "");
109 $job = C4::BackgroundJob->new($sessionID, $filename, '/cgi-bin/koha/tools/stage-marc-import.pl', $job_size);
110 my $jobID = $job->id();
112 # fork off
113 if (my $pid = fork) {
114 # parent
115 # return job ID as JSON
116 my $reply = CGI->new("");
117 print $reply->header(-type => 'text/html');
118 print '{"jobID":"' . $jobID . '"}';
119 exit 0;
120 } elsif (defined $pid) {
121 # child
122 # close STDOUT to signal to Apache that
123 # we're now running in the background
124 close STDOUT;
125 # close STDERR; # there is no good reason to close STDERR
126 } else {
127 # fork failed, so exit immediately
128 warn "fork failed while attempting to run tools/stage-marc-import.pl as a background job: $!";
129 exit 0;
132 # if we get here, we're a child that has detached
133 # itself from Apache
137 # New handle, as we're a child.
138 $dbh = C4::Context->dbh({new => 1});
139 $dbh->{AutoCommit} = 0;
140 # FIXME branch code
141 my ( $batch_id, $num_valid, $num_items, @import_errors ) =
142 BatchStageMarcRecords(
143 $record_type, $encoding,
144 $marcrecords, $filename,
145 $marc_modification_template,
146 $comments, '',
147 $parse_items, 0,
148 50, staging_progress_callback( $job, $dbh )
151 my $num_with_matches = 0;
152 my $checked_matches = 0;
153 my $matcher_failed = 0;
154 my $matcher_code = "";
155 if ($matcher_id ne "") {
156 my $matcher = C4::Matcher->fetch($matcher_id);
157 if (defined $matcher) {
158 $checked_matches = 1;
159 $matcher_code = $matcher->code();
160 $num_with_matches =
161 BatchFindDuplicates( $batch_id, $matcher, 10, 50,
162 matching_progress_callback( $job, $dbh ) );
163 SetImportBatchMatcher($batch_id, $matcher_id);
164 SetImportBatchOverlayAction($batch_id, $overlay_action);
165 SetImportBatchNoMatchAction($batch_id, $nomatch_action);
166 SetImportBatchItemAction($batch_id, $item_action);
167 $dbh->commit();
168 } else {
169 $matcher_failed = 1;
171 } else {
172 $dbh->commit();
175 my $results = {
176 staged => $num_valid,
177 matched => $num_with_matches,
178 num_items => $num_items,
179 import_errors => scalar(@import_errors),
180 total => $num_valid + scalar(@import_errors),
181 checked_matches => $checked_matches,
182 matcher_failed => $matcher_failed,
183 matcher_code => $matcher_code,
184 import_batch_id => $batch_id
186 if ($runinbackground) {
187 $job->finish($results);
188 exit 0;
189 } else {
190 $template->param(staged => $num_valid,
191 matched => $num_with_matches,
192 num_items => $num_items,
193 import_errors => scalar(@import_errors),
194 total => $num_valid + scalar(@import_errors),
195 checked_matches => $checked_matches,
196 matcher_failed => $matcher_failed,
197 matcher_code => $matcher_code,
198 import_batch_id => $batch_id
202 } else {
203 # initial form
204 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
205 $template->param( "UNIMARC" => 1 );
207 my @matchers = C4::Matcher::GetMatcherList();
208 $template->param( available_matchers => \@matchers );
210 my @templates = GetModificationTemplates();
211 $template->param( MarcModificationTemplatesLoop => \@templates );
213 if ( C4::Context->preference('UseKohaPlugins') &&
214 C4::Context->config('enable_plugins') ) {
216 my @plugins = Koha::Plugins->new()->GetPlugins({
217 method => 'to_marc',
219 $template->param( plugins => \@plugins );
223 output_html_with_http_headers $input, $cookie, $template->output;
225 exit 0;
227 sub staging_progress_callback {
228 my $job = shift;
229 my $dbh = shift;
230 return sub {
231 my $progress = shift;
232 $job->progress($progress);
236 sub matching_progress_callback {
237 my $job = shift;
238 my $dbh = shift;
239 my $start_progress = $job->progress();
240 return sub {
241 my $progress = shift;
242 $job->progress($start_progress + $progress);