Bug 25974: Remove inline style from table settings administration page
[koha.git] / tools / stage-marc-import.pl
blob78424bbc6f64097b71c9e90a23454aed8c2d94d3
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');
61 my $basketno = $input->param('basketno');
62 my $booksellerid = $input->param('booksellerid');
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,
79 basketno => $basketno,
80 booksellerid => $booksellerid,
83 my %cookies = parse CGI::Cookie($cookie);
84 my $sessionID = $cookies{'CGISESSID'}->value;
85 if ($completedJobID) {
86 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
87 my $results = $job->results();
88 $template->param(map { $_ => $results->{$_} } keys %{ $results });
89 } elsif ($fileID) {
90 my $upload = Koha::UploadedFiles->find( $fileID );
91 my $file = $upload->full_path;
92 my $filename = $upload->filename;
94 my ( $errors, $marcrecords );
95 if( $format eq 'MARCXML' ) {
96 ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding);
97 } elsif( $format eq 'ISO2709' ) {
98 ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( $file, $record_type, $encoding );
99 } else { # plugin based
100 $errors = [];
101 $marcrecords = C4::ImportBatch::RecordsFromMarcPlugin( $file, $format, $encoding );
103 warn "$filename: " . ( join ',', @$errors ) if @$errors;
104 # no need to exit if we have no records (or only errors) here
105 # BatchStageMarcRecords can handle that
107 my $job = undef;
108 my $dbh;
109 if ($runinbackground) {
110 my $job_size = scalar(@$marcrecords);
111 # if we're matching, job size is doubled
112 $job_size *= 2 if ($matcher_id ne "");
113 $job = C4::BackgroundJob->new($sessionID, $filename, '/cgi-bin/koha/tools/stage-marc-import.pl', $job_size);
114 my $jobID = $job->id();
116 # fork off
117 if (my $pid = fork) {
118 # parent
119 # return job ID as JSON
120 my $reply = CGI->new("");
121 print $reply->header(-type => 'text/html');
122 print '{"jobID":"' . $jobID . '"}';
123 exit 0;
124 } elsif (defined $pid) {
125 # child
126 # close STDOUT to signal to Apache that
127 # we're now running in the background
128 close STDOUT;
129 # close STDERR; # there is no good reason to close STDERR
130 } else {
131 # fork failed, so exit immediately
132 warn "fork failed while attempting to run tools/stage-marc-import.pl as a background job: $!";
133 exit 0;
136 # if we get here, we're a child that has detached
137 # itself from Apache
141 # New handle, as we're a child.
142 $dbh = C4::Context->dbh({new => 1});
143 $dbh->{AutoCommit} = 0;
144 # FIXME branch code
145 my ( $batch_id, $num_valid, $num_items, @import_errors ) =
146 BatchStageMarcRecords(
147 $record_type, $encoding,
148 $marcrecords, $filename,
149 $marc_modification_template,
150 $comments, '',
151 $parse_items, 0,
152 50, staging_progress_callback( $job, $dbh )
155 my $num_with_matches = 0;
156 my $checked_matches = 0;
157 my $matcher_failed = 0;
158 my $matcher_code = "";
159 if ($matcher_id ne "") {
160 my $matcher = C4::Matcher->fetch($matcher_id);
161 if (defined $matcher) {
162 $checked_matches = 1;
163 $matcher_code = $matcher->code();
164 $num_with_matches =
165 BatchFindDuplicates( $batch_id, $matcher, 10, 50,
166 matching_progress_callback( $job, $dbh ) );
167 SetImportBatchMatcher($batch_id, $matcher_id);
168 SetImportBatchOverlayAction($batch_id, $overlay_action);
169 SetImportBatchNoMatchAction($batch_id, $nomatch_action);
170 SetImportBatchItemAction($batch_id, $item_action);
171 $dbh->commit();
172 } else {
173 $matcher_failed = 1;
175 } else {
176 $dbh->commit();
179 my $results = {
180 staged => $num_valid,
181 matched => $num_with_matches,
182 num_items => $num_items,
183 import_errors => scalar(@import_errors),
184 total => $num_valid + scalar(@import_errors),
185 checked_matches => $checked_matches,
186 matcher_failed => $matcher_failed,
187 matcher_code => $matcher_code,
188 import_batch_id => $batch_id,
189 booksellerid => $booksellerid,
190 basketno => $basketno
192 if ($runinbackground) {
193 $job->finish($results);
194 exit 0;
195 } else {
196 $template->param(staged => $num_valid,
197 matched => $num_with_matches,
198 num_items => $num_items,
199 import_errors => scalar(@import_errors),
200 total => $num_valid + scalar(@import_errors),
201 checked_matches => $checked_matches,
202 matcher_failed => $matcher_failed,
203 matcher_code => $matcher_code,
204 import_batch_id => $batch_id,
205 booksellerid => $booksellerid,
206 basketno => $basketno
210 } else {
211 # initial form
212 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
213 $template->param( "UNIMARC" => 1 );
215 my @matchers = C4::Matcher::GetMatcherList();
216 $template->param( available_matchers => \@matchers );
218 my @templates = GetModificationTemplates();
219 $template->param( MarcModificationTemplatesLoop => \@templates );
221 if ( C4::Context->config('enable_plugins') ) {
223 my @plugins = Koha::Plugins->new()->GetPlugins({
224 method => 'to_marc',
226 $template->param( plugins => \@plugins );
230 output_html_with_http_headers $input, $cookie, $template->output;
232 exit 0;
234 sub staging_progress_callback {
235 my $job = shift;
236 my $dbh = shift;
237 return sub {
238 my $progress = shift;
239 $job->progress($progress);
243 sub matching_progress_callback {
244 my $job = shift;
245 my $dbh = shift;
246 my $start_progress = $job->progress();
247 return sub {
248 my $progress = shift;
249 $job->progress($start_progress + $progress);