batch import rework -- implement stage-commit-undo for batch import
[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 MARC::File::USMARC;
32
33 # Koha modules used
34 use C4::Context;
35 use C4::Auth;
36 use C4::Input;
37 use C4::Output;
38 use C4::Biblio;
39 use C4::ImportBatch;
40 use C4::Matcher;
41
42 #------------------
43 # Constants
44
45 my $includes = C4::Context->config('includes') ||
46         "/usr/local/www/hdl/htdocs/includes";
47
48 # HTML colors for alternating lines
49 my $lc1='#dddddd';
50 my $lc2='#ddaaaa';
51
52 #-------------
53 #-------------
54 # Initialize
55
56 my $userid=$ENV{'REMOTE_USER'};
57
58 my $input = new CGI;
59 my $dbh = C4::Context->dbh;
60
61 my $uploadmarc=$input->param('uploadmarc');
62 my $check_for_matches = $input->param('check_for_matches');
63 my $comments = $input->param('comments');
64 my $syntax = $input->param('syntax');
65 my ($template, $loggedinuser, $cookie)
66         = get_template_and_user({template_name => "tools/stage-marc-import.tmpl",
67                                         query => $input,
68                                         type => "intranet",
69                                         authnotrequired => 0,
70                                         flagsrequired => {tools => 1},
71                                         debug => 1,
72                                         });
73
74 $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'},
75                                                 uploadmarc => $uploadmarc);
76 my $filename = $uploadmarc;
77 if ($uploadmarc && length($uploadmarc)>0) {
78         my $marcrecord='';
79         while (<$uploadmarc>) {
80                 $marcrecord.=$_;
81         }
82
83     # FIXME branch code
84     my ($batch_id, $num_valid, @import_errors) = BatchStageMarcRecords($syntax, $marcrecord, $filename, $comments, '', 0);
85     my $matcher = C4::Matcher->new('biblio');
86     $matcher->add_matchpoint("020", "a", '', 'isbn', 1000);
87     my $num_with_matches = 0;
88     my $checked_matches = 0;
89     if ($check_for_matches) {
90         $checked_matches = 1;
91         $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher);
92     }
93     # FIXME we're not committing now
94     # my ($num_added, $num_updated, $num_ignored) = BatchCommitBibRecords($batch_id);
95
96         $template->param(staged => $num_valid,
97                          matched => $num_with_matches,
98                      import_errors => scalar(@import_errors),
99                      total => $num_valid + scalar(@import_errors),
100                      checked_matches => $checked_matches,
101                      import_batch_id => $batch_id
102                     );
103
104 }
105
106 output_html_with_http_headers $input, $cookie, $template->output;
107