Bug 26922: Regression tests
[koha.git] / tools / stage-marc-import.pl
blob97767bfa1b833fb7a5d72af90ea723f477c293b9
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;
45 use Koha::ImportBatches;
47 my $input = CGI->new;
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');
62 my $basketno = $input->param('basketno');
63 my $booksellerid = $input->param('booksellerid');
64 my $profile_id = $input->param('profile_id');
66 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
68 template_name => "tools/stage-marc-import.tt",
69 query => $input,
70 type => "intranet",
71 flagsrequired => { tools => 'stage_marc_import' },
72 debug => 1,
76 $template->param(
77 SCRIPT_NAME => '/cgi-bin/koha/tools/stage-marc-import.pl',
78 uploadmarc => $fileID,
79 record_type => $record_type,
80 basketno => $basketno,
81 booksellerid => $booksellerid,
84 my %cookies = parse CGI::Cookie($cookie);
85 my $sessionID = $cookies{'CGISESSID'}->value;
86 if ($completedJobID) {
87 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
88 my $results = $job->results();
89 $template->param(map { $_ => $results->{$_} } keys %{ $results });
90 } elsif ($fileID) {
91 my $upload = Koha::UploadedFiles->find( $fileID );
92 my $file = $upload->full_path;
93 my $filename = $upload->filename;
95 my ( $errors, $marcrecords );
96 if( $format eq 'MARCXML' ) {
97 ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding);
98 } elsif( $format eq 'ISO2709' ) {
99 ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( $file, $record_type, $encoding );
100 } else { # plugin based
101 $errors = [];
102 $marcrecords = C4::ImportBatch::RecordsFromMarcPlugin( $file, $format, $encoding );
104 warn "$filename: " . ( join ',', @$errors ) if @$errors;
105 # no need to exit if we have no records (or only errors) here
106 # BatchStageMarcRecords can handle that
108 my $job = undef;
109 my $dbh;
110 if ($runinbackground) {
111 my $job_size = scalar(@$marcrecords);
112 # if we're matching, job size is doubled
113 $job_size *= 2 if ($matcher_id ne "");
114 $job = C4::BackgroundJob->new($sessionID, $filename, '/cgi-bin/koha/tools/stage-marc-import.pl', $job_size);
115 my $jobID = $job->id();
117 # fork off
118 if (my $pid = fork) {
119 # parent
120 # return job ID as JSON
121 my $reply = CGI->new("");
122 print $reply->header(-type => 'text/html');
123 print '{"jobID":"' . $jobID . '"}';
124 exit 0;
125 } elsif (defined $pid) {
126 # child
127 # close STDOUT/STDERR to signal to end CGI session with Apache
128 # Otherwise, the AJAX request to this script won't return properly
129 close STDOUT;
130 close STDERR;
131 } else {
132 # fork failed, so exit immediately
133 warn "fork failed while attempting to run tools/stage-marc-import.pl as a background job: $!";
134 exit 0;
137 # if we get here, we're a child that has detached
138 # itself from Apache
142 # New handle, as we're a child.
143 $dbh = C4::Context->dbh({new => 1});
144 $dbh->{AutoCommit} = 0;
145 # FIXME branch code
146 my ( $batch_id, $num_valid, $num_items, @import_errors ) =
147 BatchStageMarcRecords(
148 $record_type, $encoding,
149 $marcrecords, $filename,
150 $marc_modification_template,
151 $comments, '',
152 $parse_items, 0,
153 50, staging_progress_callback( $job, $dbh )
156 if($profile_id) {
157 my $ibatch = Koha::ImportBatches->find($batch_id);
158 $ibatch->set({profile_id => $profile_id})->store;
161 my $num_with_matches = 0;
162 my $checked_matches = 0;
163 my $matcher_failed = 0;
164 my $matcher_code = "";
165 if ($matcher_id ne "") {
166 my $matcher = C4::Matcher->fetch($matcher_id);
167 if (defined $matcher) {
168 $checked_matches = 1;
169 $matcher_code = $matcher->code();
170 $num_with_matches =
171 BatchFindDuplicates( $batch_id, $matcher, 10, 50,
172 matching_progress_callback( $job, $dbh ) );
173 SetImportBatchMatcher($batch_id, $matcher_id);
174 SetImportBatchOverlayAction($batch_id, $overlay_action);
175 SetImportBatchNoMatchAction($batch_id, $nomatch_action);
176 SetImportBatchItemAction($batch_id, $item_action);
177 $dbh->commit();
178 } else {
179 $matcher_failed = 1;
181 } else {
182 $dbh->commit();
185 my $results = {
186 staged => $num_valid,
187 matched => $num_with_matches,
188 num_items => $num_items,
189 import_errors => scalar(@import_errors),
190 total => $num_valid + scalar(@import_errors),
191 checked_matches => $checked_matches,
192 matcher_failed => $matcher_failed,
193 matcher_code => $matcher_code,
194 import_batch_id => $batch_id,
195 booksellerid => $booksellerid,
196 basketno => $basketno
198 if ($runinbackground) {
199 $job->finish($results);
200 exit 0;
201 } else {
202 $template->param(staged => $num_valid,
203 matched => $num_with_matches,
204 num_items => $num_items,
205 import_errors => scalar(@import_errors),
206 total => $num_valid + scalar(@import_errors),
207 checked_matches => $checked_matches,
208 matcher_failed => $matcher_failed,
209 matcher_code => $matcher_code,
210 import_batch_id => $batch_id,
211 booksellerid => $booksellerid,
212 basketno => $basketno
216 } else {
217 # initial form
218 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
219 $template->param( "UNIMARC" => 1 );
221 my @matchers = C4::Matcher::GetMatcherList();
222 $template->param( available_matchers => \@matchers );
224 my @templates = GetModificationTemplates();
225 $template->param( MarcModificationTemplatesLoop => \@templates );
227 if ( C4::Context->config('enable_plugins') ) {
229 my @plugins = Koha::Plugins->new()->GetPlugins({
230 method => 'to_marc',
232 $template->param( plugins => \@plugins );
236 output_html_with_http_headers $input, $cookie, $template->output;
238 exit 0;
240 sub staging_progress_callback {
241 my $job = shift;
242 my $dbh = shift;
243 return sub {
244 my $progress = shift;
245 $job->progress($progress);
249 sub matching_progress_callback {
250 my $job = shift;
251 my $dbh = shift;
252 my $start_progress = $job->progress();
253 return sub {
254 my $progress = shift;
255 $job->progress($start_progress + $progress);