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