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