Bug 20434: Update UNIMARC framework - auth (NP)
[koha.git] / misc / batchDeleteUnusedSubfields.pl
bloba80d4fdd6f4cbac0729392f42ec612a94922e3bf
1 #!/usr/bin/perl
2 # small script that rebuilds the non-MARC DB
4 use strict;
5 #use warnings; FIXME - Bug 2505
6 BEGIN {
7 # find Koha's Perl modules
8 # test carefully before changing this
9 use FindBin;
10 eval { require "$FindBin::Bin/kohalib.pl" };
13 # Koha modules used
14 use Koha::Script;
15 use MARC::Record;
16 use C4::Context;
17 use C4::Biblio;
18 use Time::HiRes qw(gettimeofday);
20 use Getopt::Long;
21 my ( $input_marc_file, $number) = ('',0);
22 my ($version, $confirm,$test_parameter);
23 GetOptions(
24 'c' => \$confirm,
25 'h' => \$version,
26 't' => \$test_parameter,
29 if ($version || (!$confirm)) {
30 print <<EOF
31 This script cleans unused subfields in the MARC DB.
32 If you alter the MARC parameters and remove a subfield (ie : move it to ignore (10) tab), existing subfields are NOT removed.
33 It's not a bug, it prevents deleting useful values in case of erroneous move.
34 This script definitely remove unused subfields in the MARC DB.
35 syntax :
36 \t./cleanmarcdb.pl -h (or without arguments => shows this screen)
37 \t./cleanmarcdb.pl -c (c like confirm => cleans the marc DB (may be long)
38 \t-t => test only, change nothing in DB
39 EOF
40 ;#'
41 die;
44 my $dbh = C4::Context->dbh;
45 my $i=0;
46 my $starttime = gettimeofday;
47 my $cleansubfield = $dbh->prepare("delete from marc_subfield_table where tag=? and subfieldcode=?");
48 my $cleanword = $dbh->prepare("delete from marc_word where tag=? and subfieldid=?");
50 # get tags structure
51 my $tags = GetMarcStructure(1);
52 foreach my $tag (sort keys(%{$tags})) {
53 foreach my $subfield (sort keys(%{$tags->{$tag}})) {
54 next if $subfield eq "lib";
55 next if $subfield eq "mandatory";
56 next if $subfield eq "tab";
57 # DO NOT drop biblionumber, biblioitemnumber and itemnumber.
58 # they are stored internally, and are mapped to tab -1. This script must keep them or it will completly break Koha DB !!!
59 next if ($tags->{$tag}->{$subfield}->{kohafield} eq "biblio.biblionumber");
60 next if ($tags->{$tag}->{$subfield}->{kohafield} eq "biblioitems.biblioitemnumber");
61 next if ($tags->{$tag}->{$subfield}->{kohafield} eq "items.itemnumber");
62 # now, test => if field is ignored (in tab -1 or '') => drop everything in the MARC table !
63 if ($tags->{$tag}->{$subfield}->{tab} eq -1 || $tags->{$tag}->{$subfield}->{tab} eq '') {
64 print "dropping $tag \$ $subfield\n";
65 $cleansubfield->execute($tag,$subfield) unless $test_parameter;
66 $cleanword->execute($tag,$subfield) unless $test_parameter;
67 print "TEST " if $test_parameter;
68 print "done\n";
72 my $timeneeded = gettimeofday - $starttime;
73 print "done in $timeneeded seconds\n";