From 2758cd2a755e239cceac0f6e0c20e47dc9b4cead Mon Sep 17 00:00:00 2001 From: tipaul Date: Tue, 14 Oct 2003 12:36:36 +0000 Subject: [PATCH] script to clean MARC DB when a subfield is removed from the MARC editor : the subfield is NOT removed from DB (to avoid stupid loss of data). This script forever removes unused subfields from the DB. Quite fast script. --- misc/cleanmarcdb.pl | 67 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 misc/cleanmarcdb.pl diff --git a/misc/cleanmarcdb.pl b/misc/cleanmarcdb.pl new file mode 100755 index 0000000000..6f5c0592d7 --- /dev/null +++ b/misc/cleanmarcdb.pl @@ -0,0 +1,67 @@ +#!/usr/bin/perl +# small script that rebuilds the non-MARC DB + +use strict; + +# Koha modules used +# use MARC::File::USMARC; +use MARC::Record; +use MARC::Batch; +use C4::Context; +use C4::Biblio; +use Time::HiRes qw(gettimeofday); + +use Getopt::Long; +my ( $input_marc_file, $number) = ('',0); +my ($version, $confirm,$test_parameter); +GetOptions( + 'c' => \$confirm, + 'h' => \$version, + 't' => \$test_parameter, +); + +if ($version || (!$confirm)) { + print < shows this screen) +\t./cleanmarcdb.pl -c (c like confirm => cleans the marc DB (may be long) +\t-t => test only, change nothing in DB +EOF +;#' +die; +} + +my $dbh = C4::Context->dbh; +my $i=0; +my $starttime = gettimeofday; +my $cleansubfield = $dbh->prepare("delete from marc_subfield_table where tag=? and subfieldcode=?"); +my $cleanword = $dbh->prepare("delete from marc_word where tag=? and subfieldid=?"); + +# get tags structure +my $tags = MARCgettagslib($dbh,1); +foreach my $tag (sort keys(%{$tags})) { + foreach my $subfield (sort keys(%{$tags->{$tag}})) { + next if $subfield eq "lib"; + next if $subfield eq "mandatory"; + next if $subfield eq "tab"; + # DO NOT drop biblionumber, biblioitemnumber and itemnumber. + # they are stored internally, and are mapped to tab -1. This script must keep them or it will completly break Koha DB !!! + next if ($tags->{$tag}->{$subfield}->{kohafield} eq "biblio.biblionumber"); + next if ($tags->{$tag}->{$subfield}->{kohafield} eq "biblioitems.biblioitemnumber"); + next if ($tags->{$tag}->{$subfield}->{kohafield} eq "items.itemnumber"); + # now, test => if field is ignored (in tab -1 or '') => drop everything in the MARC table ! + if ($tags->{$tag}->{$subfield}->{tab} eq -1 || $tags->{$tag}->{$subfield}->{tab} eq '') { + print "dropping $tag \$ $subfield\n"; + $cleansubfield->execute($tag,$subfield) unless $test_parameter; + $cleanword->execute($tag,$subfield) unless $test_parameter; + print "TEST " if $test_parameter; + print "done\n"; + } + } +} +my $timeneeded = gettimeofday - $starttime; +print "done in $timeneeded seconds\n"; -- 2.39.5