Bug 19422: Missing DROP TABLES in kohastructure.sql
[koha.git] / tools / stage-marc-import.pl
blob07a921fdc4ca1f9c9071f969678c76b4152ce0ba
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 $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",
66 query => $input,
67 type => "intranet",
68 authnotrequired => 0,
69 flagsrequired => { tools => 'stage_marc_import' },
70 debug => 1,
74 $template->param(
75 SCRIPT_NAME => '/cgi-bin/koha/tools/stage-marc-import.pl',
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 });
86 } elsif ($fileID) {
87 my $upload = Koha::UploadedFiles->find( $fileID );
88 my $file = $upload->full_path;
89 my $filename = $upload->filename;
91 my ( $errors, $marcrecords );
92 if( $format eq 'MARCXML' ) {
93 ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding);
94 } elsif( $format eq 'ISO2709' ) {
95 ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( $file, $record_type, $encoding );
96 } else { # plugin based
97 $errors = [];
98 $marcrecords = C4::ImportBatch::RecordsFromMarcPlugin( $file, $format, $encoding );
100 warn "$filename: " . ( join ',', @$errors ) if @$errors;
101 # no need to exit if we have no records (or only errors) here
102 # BatchStageMarcRecords can handle that
104 my $job = undef;
105 my $dbh;
106 if ($runinbackground) {
107 my $job_size = scalar(@$marcrecords);
108 # if we're matching, job size is doubled
109 $job_size *= 2 if ($matcher_id ne "");
110 $job = C4::BackgroundJob->new($sessionID, $filename, '/cgi-bin/koha/tools/stage-marc-import.pl', $job_size);
111 my $jobID = $job->id();
113 # fork off
114 if (my $pid = fork) {
115 # parent
116 # return job ID as JSON
117 my $reply = CGI->new("");
118 print $reply->header(-type => 'text/html');
119 print '{"jobID":"' . $jobID . '"}';
120 exit 0;
121 } elsif (defined $pid) {
122 # child
123 # close STDOUT to signal to Apache that
124 # we're now running in the background
125 close STDOUT;
126 # close STDERR; # there is no good reason to close STDERR
127 } else {
128 # fork failed, so exit immediately
129 warn "fork failed while attempting to run tools/stage-marc-import.pl as a background job: $!";
130 exit 0;
133 # if we get here, we're a child that has detached
134 # itself from Apache
138 # New handle, as we're a child.
139 $dbh = C4::Context->dbh({new => 1});
140 $dbh->{AutoCommit} = 0;
141 # FIXME branch code
142 my ( $batch_id, $num_valid, $num_items, @import_errors ) =
143 BatchStageMarcRecords(
144 $record_type, $encoding,
145 $marcrecords, $filename,
146 $marc_modification_template,
147 $comments, '',
148 $parse_items, 0,
149 50, staging_progress_callback( $job, $dbh )
152 my $num_with_matches = 0;
153 my $checked_matches = 0;
154 my $matcher_failed = 0;
155 my $matcher_code = "";
156 if ($matcher_id ne "") {
157 my $matcher = C4::Matcher->fetch($matcher_id);
158 if (defined $matcher) {
159 $checked_matches = 1;
160 $matcher_code = $matcher->code();
161 $num_with_matches =
162 BatchFindDuplicates( $batch_id, $matcher, 10, 50,
163 matching_progress_callback( $job, $dbh ) );
164 SetImportBatchMatcher($batch_id, $matcher_id);
165 SetImportBatchOverlayAction($batch_id, $overlay_action);
166 SetImportBatchNoMatchAction($batch_id, $nomatch_action);
167 SetImportBatchItemAction($batch_id, $item_action);
168 $dbh->commit();
169 } else {
170 $matcher_failed = 1;
172 } else {
173 $dbh->commit();
176 my $results = {
177 staged => $num_valid,
178 matched => $num_with_matches,
179 num_items => $num_items,
180 import_errors => scalar(@import_errors),
181 total => $num_valid + scalar(@import_errors),
182 checked_matches => $checked_matches,
183 matcher_failed => $matcher_failed,
184 matcher_code => $matcher_code,
185 import_batch_id => $batch_id
187 if ($runinbackground) {
188 $job->finish($results);
189 exit 0;
190 } else {
191 $template->param(staged => $num_valid,
192 matched => $num_with_matches,
193 num_items => $num_items,
194 import_errors => scalar(@import_errors),
195 total => $num_valid + scalar(@import_errors),
196 checked_matches => $checked_matches,
197 matcher_failed => $matcher_failed,
198 matcher_code => $matcher_code,
199 import_batch_id => $batch_id
203 } else {
204 # initial form
205 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
206 $template->param( "UNIMARC" => 1 );
208 my @matchers = C4::Matcher::GetMatcherList();
209 $template->param( available_matchers => \@matchers );
211 my @templates = GetModificationTemplates();
212 $template->param( MarcModificationTemplatesLoop => \@templates );
214 if ( C4::Context->preference('UseKohaPlugins') &&
215 C4::Context->config('enable_plugins') ) {
217 my @plugins = Koha::Plugins->new()->GetPlugins({
218 method => 'to_marc',
220 $template->param( plugins => \@plugins );
224 output_html_with_http_headers $input, $cookie, $template->output;
226 exit 0;
228 sub staging_progress_callback {
229 my $job = shift;
230 my $dbh = shift;
231 return sub {
232 my $progress = shift;
233 $job->progress($progress);
237 sub matching_progress_callback {
238 my $job = shift;
239 my $dbh = shift;
240 my $start_progress = $job->progress();
241 return sub {
242 my $progress = shift;
243 $job->progress($start_progress + $progress);