3 # Script for handling import of MARC data into Koha db
6 # Koha library project www.koha.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 under the
15 # terms of the GNU General Public License as published by the Free Software
16 # Foundation; either version 2 of the License, or (at your option) any later
19 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
20 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License along with
24 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
25 # Suite 330, Boston, MA 02111-1307 USA
29 # standard or CPAN modules used
32 use MARC::File::USMARC;
43 use C4::BackgroundJob;
46 my $dbh = C4::Context->dbh;
48 my $fileID=$input->param('uploadedfileid');
49 my $matcher_id = $input->param('matcher');
50 my $parse_items = $input->param('parse_items');
51 my $comments = $input->param('comments');
52 my $syntax = $input->param('syntax');
53 my ($template, $loggedinuser, $cookie)
54 = get_template_and_user({template_name => "tools/stage-marc-import.tmpl",
58 flagsrequired => {tools => 1},
62 $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'},
63 uploadmarc => $fileID);
66 my %cookies = parse CGI::Cookie($cookie);
67 my $uploaded_file = C4::UploadedFile->fetch($cookies{'CGISESSID'}->value, $fileID);
68 my $fh = $uploaded_file->fh();
74 my $job_size = scalar($marcrecord =~ /\035/g);
75 # if we're matching, job size is doubled
76 $job_size *= 2 if ($matcher_id ne "");
79 my $filename = $uploaded_file->name();
80 my $job = C4::BackgroundJob->new($cookies{'CGISESSID'}->value, $filename, $ENV{'SCRIPT_NAME'}, $job_size);
81 my ($batch_id, $num_valid, $num_items, @import_errors) = BatchStageMarcRecords($syntax, $marcrecord, $filename,
82 $comments, '', $parse_items, 0,
83 100, staging_progress_callback($job));
84 my $num_with_matches = 0;
85 my $checked_matches = 0;
86 my $matcher_failed = 0;
87 my $matcher_code = "";
88 if ($matcher_id ne "") {
89 my $matcher = C4::Matcher->fetch($matcher_id);
90 if (defined $matcher) {
92 $matcher_code = $matcher->code();
93 $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, 10, 100, matching_progress_callback($job));
94 SetImportBatchMatcher($batch_id, $matcher_id);
101 staged => $num_valid,
102 matched => $num_with_matches,
103 num_items => $num_items,
104 import_errors => scalar(@import_errors),
105 total => $num_valid + scalar(@import_errors),
106 checked_matches => $checked_matches,
107 matcher_failed => $matcher_failed,
108 matcher_code => $matcher_code,
109 import_batch_id => $batch_id
111 $job->finish($results);
113 $template->param(staged => $num_valid,
114 matched => $num_with_matches,
115 num_items => $num_items,
116 import_errors => scalar(@import_errors),
117 total => $num_valid + scalar(@import_errors),
118 checked_matches => $checked_matches,
119 matcher_failed => $matcher_failed,
120 matcher_code => $matcher_code,
121 import_batch_id => $batch_id
126 my @matchers = C4::Matcher::GetMatcherList();
127 $template->param(available_matchers => \@matchers);
130 output_html_with_http_headers $input, $cookie, $template->output;
134 sub staging_progress_callback {
137 my $progress = shift;
138 $job->progress($job->progress() + $progress);
142 sub matching_progress_callback {
145 my $progress = shift;
146 $job->progress($job->progress() + $progress);