X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=misc%2Fstage_biblios_file.pl;h=6e2b30cca873b9966b32a5cb484ed805dade5282;hb=25eeaf0f8e67b065559218c78a15bcb3700a0f0f;hp=bfe26ce972721f701497f9d427316e750a35d81e;hpb=de04e990f269ef00fe24cb2861b6fc48b6407779;p=koha.git diff --git a/misc/stage_biblios_file.pl b/misc/stage_biblios_file.pl index bfe26ce972..6e2b30cca8 100755 --- a/misc/stage_biblios_file.pl +++ b/misc/stage_biblios_file.pl @@ -1,6 +1,25 @@ #!/usr/bin/perl +# This file is part of Koha. +# +# Copyright (C) 2007 LibLime +# Parts Copyright BSZ 2011 +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + use strict; +use warnings; BEGIN { # find Koha's Perl modules # test carefully before changing this @@ -16,20 +35,28 @@ use Getopt::Long; $| = 1; # command-line parameters +my $encoding = ""; my $match_bibs = 0; my $add_items = 0; my $input_file = ""; my $batch_comment = ""; my $want_help = 0; +my $no_replace ; my $result = GetOptions( + 'encoding:s' => \$encoding, 'file:s' => \$input_file, - 'match-bibs' => \$match_bibs, + 'match-bibs:s' => \$match_bibs, 'add-items' => \$add_items, + 'no-replace' => \$no_replace, 'comment:s' => \$batch_comment, 'h|help' => \$want_help ); +if ($encoding eq "") { + $encoding = "utf8"; +} + if (not $result or $input_file eq "" or $want_help) { print_usage(); exit 0; @@ -39,7 +66,10 @@ unless (-r $input_file) { die "$0: cannot open input file $input_file: $!\n"; } +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; process_batch($input_file, $match_bibs, $add_items, $batch_comment); +$dbh->commit(); exit 0; @@ -51,28 +81,39 @@ sub process_batch { $/ = "\035"; my $num_input_records = 0; while () { + s/^\s+//; + s/\s+$//; + next unless $_; # skip if record has only whitespace, as might occur + # if file includes newlines between each MARC record $marc_records .= $_; # FIXME - this sort of string concatenation # is probably rather inefficient $num_input_records++; } close IN; - my $marc_flavor = C4::Context->preference('marcflavour'); - print "... staging MARC records -- please wait\n"; my ($batch_id, $num_valid, $num_items, @import_errors) = - BatchStageMarcRecords($marc_flavor, $marc_records, $input_file, $batch_comment, '', $add_items, 0, - 100, \&print_progress); + BatchStageMarcRecords($encoding, $marc_records, $input_file, $batch_comment, '', $add_items, 0, + 100, \&print_progress_and_commit); print "... finished staging MARC records\n"; my $num_with_matches = 0; if ($match_bibs) { - my $matcher = C4::Matcher->new('biblio'); - $matcher->add_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, ''); - $matcher->add_simple_required_check('245', 'a', -1, 0, '', + my $matcher = C4::Matcher->fetch($match_bibs) ; + if (! defined $matcher) { + $matcher = C4::Matcher->new('biblio'); + $matcher->add_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, ''); + $matcher->add_simple_required_check('245', 'a', -1, 0, '', '245', 'a', -1, 0, ''); + } else { + SetImportBatchMatcher($batch_id, $match_bibs); + } + # set default record overlay behavior + SetImportBatchOverlayAction($batch_id, ($no_replace) ? 'ignore' : 'replace'); + SetImportBatchNoMatchAction($batch_id, 'create_new'); + SetImportBatchItemAction($batch_id, 'always_add'); print "... looking for matches with records already in database\n"; - $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, 10, 100, \&print_progress); + $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, 10, 100, \&print_progress_and_commit); print "... finished looking for matches\n"; } @@ -102,8 +143,9 @@ _SUMMARY_ print "\n"; } -sub print_progress { +sub print_progress_and_commit { my $recs = shift; + $dbh->commit(); print "... processed $recs records\n"; } @@ -121,12 +163,19 @@ records into the main Koha database. Parameters: --file name of input MARC bib file - --match-bibs use this option to match bibs + --encoding encoding of MARC records, default is utf8. + Other possible options are: MARC-8, + ISO_5426, ISO_6937, ISO_8859-1, EUC-KR + --match-bibs use this option to match bibs in the file with bibs already in the database for future overlay. + If isn't defined, a default + MARC21 ISBN & title match rule will be applied. --add-items use this option to specify that item data is embedded in the MARC bibs and should be parsed. + --no-replace overlay action for bib record: default is to + replace extant bib with the imported record. --comment optional comment to describe the record batch; if the comment has spaces in it, surround the