bulk MARC record import - speed improved
[koha.git] / misc / compare_iso_and_marc_parameters.pl
1 #!/usr/bin/perl
2 # small script that dumps an iso2709 file.
3
4
5 use strict;
6 BEGIN {
7     # find Koha's Perl modules
8     # test carefully before changing this
9     use FindBin;
10     eval { require "$FindBin::Bin/kohalib.pl" };
11 }
12
13 # Koha modules used
14 use C4::Context;
15 use MARC::File::USMARC;
16 use MARC::Record;
17 use MARC::Batch;
18
19 use Getopt::Long;
20 my ( $input_marc_file,$number,$nowarning,$frameworkcode) = ('',0);
21 my $version;
22 GetOptions(
23     'file:s'    => \$input_marc_file,
24     'n:s' => \$number,
25     'v' => \$version,
26     'w' => \$nowarning,
27         'c' => \$frameworkcode,
28 );
29
30 $frameworkcode="" unless $frameworkcode;
31
32 if ($version || ($input_marc_file eq '')) {
33         print <<EOF
34 This script compare an iso2709 file and the MARC parameters
35 It will show the marc fields/subfields used in Koha, and that are not anywhere in the iso2709 file and which fields/subfields that are used in the iso2709 file and not in Koha.
36 to solve this, just modify Koha parameters (change TAB)
37 parameters :
38 \tv : this version/help screen
39 \tfile /path/to/file/to/dump : the file to dump
40 \tw : warning and strict off. If your dump fail, try -w option. It it works, then, the file is iso2709, but a buggy one !
41 \tc : the frameworkcode. If omitted, set to ""
42 SAMPLE : ./compare_iso_and_marc_parameters.pl -file /home/paul/koha.dev/local/npl -n 1 
43 EOF
44 ;
45 die;
46 }#/
47
48 my $batch = MARC::Batch->new( 'USMARC', $input_marc_file );
49 $batch->warnings_off() unless $nowarning;
50 $batch->strict_off() unless $nowarning;
51 my $dbh=C4::Context->dbh;
52 my $sth = $dbh->prepare("select tagfield,tagsubfield,tab from marc_subfield_structure where frameworkcode=?");
53 $sth->execute($frameworkcode);
54
55 my %hash_unused;
56 my %hash_used;
57 while (my ($tagfield,$tagsubfield,$tab) = $sth->fetchrow) {
58         $hash_unused{"$tagfield$tagsubfield"} = 1 if ($tab eq -1);
59         $hash_used{"$tagfield$tagsubfield"} = 1 if ($tab ne -1);
60 }
61 my $i=0;
62 while ( my $record = $batch->next() ) {
63         $i++;
64         foreach my $MARCfield ($record->fields()) {
65         next if $MARCfield->tag()<=010;
66                 if ($MARCfield) {
67                         foreach my $fields ($MARCfield->subfields()) {
68                                 if ($fields) {
69                                         if ($hash_unused{$MARCfield->tag().@$fields[0]}>=1) {
70                                                 $hash_unused{$MARCfield->tag().@$fields[0]}++;
71                                         }
72                                         if ($hash_used{$MARCfield->tag().@$fields[0]}>=1) {
73                                                 $hash_used{$MARCfield->tag().@$fields[0]}++;
74                                         }
75                                 }
76         #                       foreach my $field (@$fields) {
77         #                               warn "==>".$MARCfield->tag().@$fields[0];
78         #                       }
79                         }
80                 }
81         }
82 }
83 print "Undeclared tag/subfields that exists in the file\n";
84 print "================================================\n";
85 foreach my $key (sort keys %hash_unused) {
86         print "$key => ".($hash_unused{$key}-1)."\n" unless ($hash_unused{$key}==1);
87 }
88
89 print "Declared tag/subfields unused in the iso2709 file\n";
90 print "=================================================\n";
91 foreach my $key (sort keys %hash_used) {
92         print "$key => ".($hash_used{$key}-1)."\n" if ($hash_used{$key}==1);
93 }
94
95 # foreach my $x (sort keys %resB) {
96 #       print "$x => ".$resB{$x}."\n";
97 # }
98 print "\n==================\n$i record parsed\n";