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