MARC import: part 4 of large file support
[koha.git] / tools / stage-marc-import.pl
1 #!/usr/bin/perl
2
3 # Script for handling import of MARC data into Koha db
4 #   and Z39.50 lookups
5
6 # Koha library project  www.koha.org
7
8 # Licensed under the GPL
9
10 # Copyright 2000-2002 Katipo Communications
11 #
12 # This file is part of Koha.
13 #
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
17 # version.
18 #
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.
22 #
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
26
27 use strict;
28
29 # standard or CPAN modules used
30 use CGI;
31 use CGI::Cookie;
32 use MARC::File::USMARC;
33
34 # Koha modules used
35 use C4::Context;
36 use C4::Auth;
37 use C4::Input;
38 use C4::Output;
39 use C4::Biblio;
40 use C4::ImportBatch;
41 use C4::Matcher;
42 use C4::UploadedFile;
43 use C4::BackgroundJob;
44
45 my $input = new CGI;
46 my $dbh = C4::Context->dbh;
47
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",
55                                         query => $input,
56                                         type => "intranet",
57                                         authnotrequired => 0,
58                                         flagsrequired => {tools => 1},
59                                         debug => 1,
60                                         });
61
62 $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'},
63                                                 uploadmarc => $fileID);
64
65 if ($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();
69         my $marcrecord='';
70         while (<$fh>) {
71                 $marcrecord.=$_;
72         }
73
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 "");
77
78     # FIXME branch code
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) {
91             $checked_matches = 1;
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);
95         } else {
96             $matcher_failed = 1;
97         }
98     }
99
100     my $results = {
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
110     };
111     $job->finish($results);
112
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
122                     );
123
124 } else {
125     # initial form
126     my @matchers = C4::Matcher::GetMatcherList();
127     $template->param(available_matchers => \@matchers);
128 }
129
130 output_html_with_http_headers $input, $cookie, $template->output;
131
132 exit 0;
133
134 sub staging_progress_callback {
135     my $job = shift;
136     return sub {
137         my $progress = shift;
138         $job->progress($job->progress() + $progress);
139     }
140 }
141
142 sub matching_progress_callback {
143     my $job = shift;
144     return sub {
145         my $progress = shift;
146         $job->progress($job->progress() + $progress);
147     }
148 }