Bug 16047 [Follow-up] Software error on deleting a group with no category code
[koha.git] / misc / batchDeleteUnusedSubfields.pl
bloba0ecb5cececa09c43f4117c1d802e126b4c9bf8a
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 MARC::Record;
15 use C4::Context;
16 use C4::Biblio;
17 use Time::HiRes qw(gettimeofday);
19 use Getopt::Long;
20 my ( $input_marc_file, $number) = ('',0);
21 my ($version, $confirm,$test_parameter);
22 GetOptions(
23 'c' => \$confirm,
24 'h' => \$version,
25 't' => \$test_parameter,
28 if ($version || (!$confirm)) {
29 print <<EOF
30 This script cleans unused subfields in the MARC DB.
31 If you alter the MARC parameters and remove a subfield (ie : move it to ignore (10) tab), existing subfields are NOT removed.
32 It's not a bug, it prevents deleting useful values in case of erroneous move.
33 This script definetly remove unused subfields in the MARC DB.
34 syntax :
35 \t./cleanmarcdb.pl -h (or without arguments => shows this screen)
36 \t./cleanmarcdb.pl -c (c like confirm => cleans the marc DB (may be long)
37 \t-t => test only, change nothing in DB
38 EOF
39 ;#'
40 die;
43 my $dbh = C4::Context->dbh;
44 my $i=0;
45 my $starttime = gettimeofday;
46 my $cleansubfield = $dbh->prepare("delete from marc_subfield_table where tag=? and subfieldcode=?");
47 my $cleanword = $dbh->prepare("delete from marc_word where tag=? and subfieldid=?");
49 # get tags structure
50 my $tags = GetMarcStructure(1);
51 foreach my $tag (sort keys(%{$tags})) {
52 foreach my $subfield (sort keys(%{$tags->{$tag}})) {
53 next if $subfield eq "lib";
54 next if $subfield eq "mandatory";
55 next if $subfield eq "tab";
56 # DO NOT drop biblionumber, biblioitemnumber and itemnumber.
57 # they are stored internally, and are mapped to tab -1. This script must keep them or it will completly break Koha DB !!!
58 next if ($tags->{$tag}->{$subfield}->{kohafield} eq "biblio.biblionumber");
59 next if ($tags->{$tag}->{$subfield}->{kohafield} eq "biblioitems.biblioitemnumber");
60 next if ($tags->{$tag}->{$subfield}->{kohafield} eq "items.itemnumber");
61 # now, test => if field is ignored (in tab -1 or '') => drop everything in the MARC table !
62 if ($tags->{$tag}->{$subfield}->{tab} eq -1 || $tags->{$tag}->{$subfield}->{tab} eq '') {
63 print "dropping $tag \$ $subfield\n";
64 $cleansubfield->execute($tag,$subfield) unless $test_parameter;
65 $cleanword->execute($tag,$subfield) unless $test_parameter;
66 print "TEST " if $test_parameter;
67 print "done\n";
71 my $timeneeded = gettimeofday - $starttime;
72 print "done in $timeneeded seconds\n";