MARC import: part 3 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
44 my $input = new CGI;
45 my $dbh = C4::Context->dbh;
46
47 my $fileID=$input->param('uploadedfileid');
48 my $matcher_id = $input->param('matcher');
49 my $parse_items = $input->param('parse_items');
50 my $comments = $input->param('comments');
51 my $syntax = $input->param('syntax');
52 my ($template, $loggedinuser, $cookie)
53         = get_template_and_user({template_name => "tools/stage-marc-import.tmpl",
54                                         query => $input,
55                                         type => "intranet",
56                                         authnotrequired => 0,
57                                         flagsrequired => {tools => 1},
58                                         debug => 1,
59                                         });
60
61 $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'},
62                                                 uploadmarc => $fileID);
63
64 if ($fileID) {
65     my %cookies = parse CGI::Cookie($cookie);
66     my $uploaded_file = C4::UploadedFile->fetch($cookies{'CGISESSID'}->value, $fileID);
67     my $fh = $uploaded_file->fh();
68         my $marcrecord='';
69         while (<$fh>) {
70                 $marcrecord.=$_;
71         }
72
73     # FIXME branch code
74     my $filename = $uploaded_file->name();
75     my ($batch_id, $num_valid, $num_items, @import_errors) = BatchStageMarcRecords($syntax, $marcrecord, $filename, 
76                                                                                    $comments, '', $parse_items, 0);
77     my $num_with_matches = 0;
78     my $checked_matches = 0;
79     my $matcher_failed = 0;
80     my $matcher_code = "";
81     if ($matcher_id ne "") {
82         my $matcher = C4::Matcher->fetch($matcher_id);
83         if (defined $matcher) {
84             $checked_matches = 1;
85             $matcher_code = $matcher->code();
86             $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher);
87             SetImportBatchMatcher($batch_id, $matcher_id);
88         } else {
89             $matcher_failed = 1;
90         }
91     }
92
93         $template->param(staged => $num_valid,
94                          matched => $num_with_matches,
95                      num_items => $num_items,
96                      import_errors => scalar(@import_errors),
97                      total => $num_valid + scalar(@import_errors),
98                      checked_matches => $checked_matches,
99                      matcher_failed => $matcher_failed,
100                      matcher_code => $matcher_code,
101                      import_batch_id => $batch_id
102                     );
103
104 } else {
105     # initial form
106     my @matchers = C4::Matcher::GetMatcherList();
107     $template->param(available_matchers => \@matchers);
108 }
109
110 output_html_with_http_headers $input, $cookie, $template->output;
111