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