3 # Copyright (C) 2007 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
22 # standard or CPAN modules used
25 use MARC
::File
::USMARC
;
35 use C4
::BackgroundJob
;
37 my $script_name = "/cgi-bin/koha/tools/manage-marc-import.pl";
40 my $op = $input->param('op');
41 my $completedJobID = $input->param('completedJobID');
42 my $runinbackground = $input->param('runinbackground');
43 my $import_batch_id = $input->param('import_batch_id');
45 # record list displays
46 my $offset = $input->param('offset') || 0;
47 my $results_per_page = $input->param('results_per_page') || 10;
49 my ($template, $loggedinuser, $cookie)
50 = get_template_and_user
({template_name
=> "tools/manage-marc-import.tmpl",
54 flagsrequired
=> {parameters
=> 1},
58 my %cookies = parse CGI
::Cookie
($cookie);
59 my $sessionID = $cookies{'CGISESSID'}->value;
60 my $dbh = C4
::Context
->dbh;
63 $template->param(script_name
=> $script_name, $op => 1);
65 $template->param(script_name
=> $script_name);
70 if ($import_batch_id eq "") {
71 import_batches_list
($template, $offset, $results_per_page);
73 import_biblios_list
($template, $import_batch_id, $offset, $results_per_page);
75 } elsif ($op eq "commit-batch") {
76 if ($completedJobID) {
77 add_saved_job_results_to_template
($template, $completedJobID);
79 commit_batch
($template, $import_batch_id);
81 import_biblios_list
($template, $import_batch_id, $offset, $results_per_page);
82 } elsif ($op eq "revert-batch") {
83 if ($completedJobID) {
84 add_saved_job_results_to_template
($template, $completedJobID);
86 revert_batch
($template, $import_batch_id);
88 import_biblios_list
($template, $import_batch_id, $offset, $results_per_page);
89 } elsif ($op eq "clean-batch") {
91 } elsif ($op eq "redo-matching") {
92 my $new_matcher_id = $input->param('new_matcher_id');
93 my $current_matcher_id = $input->param('current_matcher_id');
94 redo_matching
($template, $import_batch_id, $new_matcher_id, $current_matcher_id);
95 import_biblios_list
($template, $import_batch_id, $offset, $results_per_page);
98 output_html_with_http_headers
$input, $cookie, $template->output;
103 my ($template, $import_batch_id, $new_matcher_id, $current_matcher_id) = @_;
104 my $rematch_failed = 0;
105 return if not defined $new_matcher_id and not defined $current_matcher_id;
106 return if $new_matcher_id == $current_matcher_id;
107 my $num_with_matches = 0;
108 if (defined $new_matcher_id and $new_matcher_id ne "") {
109 my $matcher = C4
::Matcher
->fetch($new_matcher_id);
110 if (defined $matcher) {
111 $num_with_matches = BatchFindBibDuplicates
($import_batch_id, $matcher);
112 SetImportBatchMatcher
($import_batch_id, $new_matcher_id);
117 $num_with_matches = BatchFindBibDuplicates
($import_batch_id, undef);
118 SetImportBatchMatcher
($import_batch_id, undef);
120 $template->param(rematch_failed
=> $rematch_failed);
121 $template->param(rematch_attempted
=> 1);
122 $template->param(num_with_matches
=> $num_with_matches);
125 sub import_batches_list
{
126 my ($template, $offset, $results_per_page) = @_;
127 my $batches = GetImportBatchRangeDesc
($offset, $results_per_page);
130 foreach my $batch (@
$batches) {
132 import_batch_id
=> $batch->{'import_batch_id'},
133 num_biblios
=> $batch->{'num_biblios'},
134 num_items
=> $batch->{'num_items'},
135 upload_timestamp
=> $batch->{'upload_timestamp'},
136 import_status
=> $batch->{'import_status'},
137 file_name
=> $batch->{'file_name'},
138 comments
=> $batch->{'comments'}
141 $template->param(batch_list
=> \
@list);
142 my $num_batches = GetNumberOfNonZ3950ImportBatches
();
143 add_page_numbers
($template, $offset, $results_per_page, $num_batches);
144 $template->param(offset
=> $offset);
145 $template->param(range_top
=> $offset + $results_per_page - 1);
146 $template->param(num_results
=> $num_batches);
147 $template->param(results_per_page
=> $results_per_page);
152 my ($template, $import_batch_id) = @_;
155 $dbh->{AutoCommit
} = 0;
156 my $callback = sub {};
157 if ($runinbackground) {
158 $job = put_in_background
($import_batch_id);
159 $callback = progress_callback
($job, $dbh);
161 my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) =
162 BatchCommitBibRecords
($import_batch_id, 50, $callback);
167 num_added
=> $num_added,
168 num_updated
=> $num_updated,
169 num_items_added
=> $num_items_added,
170 num_items_errored
=> $num_items_errored,
171 num_ignored
=> $num_ignored
173 if ($runinbackground) {
174 $job->finish($results);
176 add_results_to_template
($template, $results);
181 my ($template, $import_batch_id) = @_;
183 $dbh->{AutoCommit
} = 0;
185 my $callback = sub {};
186 if ($runinbackground) {
187 $job = put_in_background
($import_batch_id);
188 $callback = progress_callback
($job, $dbh);
190 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) =
191 BatchRevertBibRecords
($import_batch_id, 50, $callback);
196 num_deleted
=> $num_deleted,
197 num_items_deleted
=> $num_items_deleted,
198 num_errors
=> $num_errors,
199 num_reverted
=> $num_reverted,
200 num_ignored
=> $num_ignored,
202 if ($runinbackground) {
203 $job->finish($results);
205 add_results_to_template
($template, $results);
209 sub put_in_background
{
210 my $import_batch_id = shift;
212 my $batch = GetImportBatch
($import_batch_id);
213 my $job = C4
::BackgroundJob
->new($sessionID, $batch->{'file_name'}, $ENV{'SCRIPT_NAME'}, $batch->{'num_biblios'});
214 my $jobID = $job->id();
217 if (my $pid = fork) {
219 # return job ID as JSON
221 # prevent parent exiting from
222 # destroying the kid's database handle
223 # FIXME: according to DBI doc, this may not work for Oracle
224 $dbh->{InactiveDestroy
} = 1;
226 my $reply = CGI
->new("");
227 print $reply->header(-type
=> 'text/html');
228 print "{ jobID: '$jobID' }";
230 } elsif (defined $pid) {
232 # close STDOUT to signal to Apache that
233 # we're now running in the background
237 # fork failed, so exit immediately
238 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
244 sub progress_callback
{
248 my $progress = shift;
249 $job->progress($progress);
254 sub add_results_to_template
{
255 my $template = shift;
257 $template->param(map { $_ => $results->{$_} } keys %{ $results });
260 sub add_saved_job_results_to_template
{
261 my $template = shift;
262 my $completedJobID = shift;
263 my $job = C4
::BackgroundJob
->fetch($sessionID, $completedJobID);
264 my $results = $job->results();
265 add_results_to_template
($template, $results);
268 sub import_biblios_list
{
269 my ($template, $import_batch_id, $offset, $results_per_page) = @_;
271 my $batch = GetImportBatch
($import_batch_id);
272 my $biblios = GetImportBibliosRange
($import_batch_id, $offset, $results_per_page);
274 foreach my $biblio (@
$biblios) {
275 my $citation = $biblio->{'title'};
276 $citation .= " $biblio->{'author'}" if $biblio->{'author'};
277 $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
278 $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
279 $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
280 $citation .= $biblio->{'issn'} if $biblio->{'issn'};
281 $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
282 my $match = GetImportRecordMatches
($biblio->{'import_record_id'}, 1);
284 import_record_id
=> $biblio->{'import_record_id'},
285 citation
=> $citation,
286 status
=> $biblio->{'status'},
287 record_sequence
=> $biblio->{'record_sequence'},
288 overlay_status
=> $biblio->{'overlay_status'},
289 match_biblionumber
=> $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
290 match_citation
=> $#$match > -1 ? $match->[0]->{'title'} . ' ' . $match->[0]->{'author'} : '',
291 match_score
=> $#$match > -1 ? $match->[0]->{'score'} : 0,
294 my $num_biblios = $batch->{'num_biblios'};
295 $template->param(biblio_list
=> \
@list);
296 add_page_numbers
($template, $offset, $results_per_page, $num_biblios);
297 $template->param(offset
=> $offset);
298 $template->param(range_top
=> $offset + $results_per_page - 1);
299 $template->param(num_results
=> $num_biblios);
300 $template->param(results_per_page
=> $results_per_page);
301 $template->param(import_batch_id
=> $import_batch_id);
302 batch_info
($template, $batch);
307 my ($template, $batch) = @_;
308 $template->param(batch_info
=> 1);
309 $template->param(file_name
=> $batch->{'file_name'});
310 $template->param(comments
=> $batch->{'comments'});
311 $template->param(import_status
=> $batch->{'import_status'});
312 $template->param(upload_timestamp
=> $batch->{'upload_timestamp'});
313 $template->param(num_biblios
=> $batch->{'num_biblios'});
314 $template->param(num_items
=> $batch->{'num_biblios'});
315 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
316 $template->param(can_commit
=> 1);
318 if ($batch->{'import_status'} eq 'imported') {
319 $template->param(can_revert
=> 1);
321 if (defined $batch->{'matcher_id'}) {
322 my $matcher = C4
::Matcher
->fetch($batch->{'matcher_id'});
323 if (defined $matcher) {
324 $template->param('current_matcher_id' => $batch->{'matcher_id'});
325 $template->param('current_matcher_code' => $matcher->code());
326 $template->param('current_matcher_description' => $matcher->description());
329 add_matcher_list
($batch->{'matcher_id'});
332 sub add_matcher_list
{
333 my $current_matcher_id = shift;
334 my @matchers = C4
::Matcher
::GetMatcherList
();
335 if (defined $current_matcher_id) {
336 for (my $i = 0; $i <= $#matchers; $i++) {
337 if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
338 $matchers[$i]->{'selected'} = 1;
342 $template->param(available_matchers
=> \
@matchers);
345 sub add_page_numbers
{
346 my ($template, $offset, $results_per_page, $total_results) = @_;
347 my $max_pages = POSIX
::ceil
($total_results / $results_per_page);
348 return if $max_pages < 2;
349 my $current_page = int($offset / $results_per_page) + 1;
351 for (my $i = 1; $i <= $max_pages; $i++) {
354 current_page
=> ($current_page == $i) ?
1 : 0,
355 offset
=> ($i - 1) * $results_per_page
358 $template->param(pages
=> \
@pages);