Arabic, Bengali and Bulgarian opac updates
[koha.git] / misc / batchCompareMARCvsFrameworks.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 use IO::File;
21
22 my ( $input_marc_file,$number,$nowarning,$frameworkcode) = ('',0);
23 my $version;
24 GetOptions(
25     'file:s'    => \$input_marc_file,
26     'n:s' => \$number,
27     'v' => \$version,
28     'w' => \$nowarning,
29     'c' => \$frameworkcode,
30 );
31
32 $frameworkcode="" unless $frameworkcode;
33
34 if ($version || ($input_marc_file eq '')) {
35     print <<EOF
36 This script compares an iso2709 file and Koha's MARC frameworks
37 It will show the marc fields/subfields used in Koha, and that 
38 are not in the iso2709 file and which fields/subfields that are
39 used in the iso2709 file and not in Koha.
40
41 parameters :
42 \tv : this version/help screen
43 \tfile /path/to/file/to/dump : the file to dump
44 \tw : warning and strict off. If your dump fails, try -w option. It it works, then, the file is iso2709, but a buggy one !
45 \tc : the frameworkcode. If omitted, set to ""
46
47 SAMPLE : ./compare_iso_and_marc_parameters.pl -file /home/paul/koha.dev/local/npl -n 1 
48
49 EOF
50 ;
51 die;
52 }#/
53
54 my $fh = IO::File->new($input_marc_file); # don't let MARC::Batch open the file, as it applies the ':utf8' IO layer
55 my $batch = MARC::Batch->new( 'USMARC', $fh );
56 $batch->warnings_off() unless $nowarning;
57 $batch->strict_off() unless $nowarning;
58 my $dbh=C4::Context->dbh;
59 my $sth = $dbh->prepare("select tagfield,tagsubfield,tab from marc_subfield_structure where frameworkcode=?");
60 $sth->execute($frameworkcode);
61
62 my %hash_unused;
63 my %hash_used;
64 while (my ($tagfield,$tagsubfield,$tab) = $sth->fetchrow) {
65     $hash_unused{"$tagfield$tagsubfield"} = 1 if ($tab eq -1);
66     $hash_used{"$tagfield$tagsubfield"} = 1 if ($tab ne -1);
67 }
68 my $i=0;
69 while ( my $record = $batch->next() ) {
70     $i++;
71     foreach my $MARCfield ($record->fields()) {
72     next if $MARCfield->is_control_field(); # tag num < 10
73         if ($MARCfield) {
74             foreach my $fields ($MARCfield->subfields()) {
75                 if ($fields) {
76                     if ($hash_unused{$MARCfield->tag().@$fields[0]}>=1) {
77                         $hash_unused{$MARCfield->tag().@$fields[0]}++;
78                     }
79                     if ($hash_used{$MARCfield->tag().@$fields[0]}>=1) {
80                         $hash_used{$MARCfield->tag().@$fields[0]}++;
81                     }
82                 }
83     #           foreach my $field (@$fields) {
84     #               warn "==>".$MARCfield->tag().@$fields[0];
85     #           }
86             }
87         }
88     }
89 }
90 print "Undeclared tag/subfields that exists in the file\n";
91 print "================================================\n";
92 foreach my $key (sort keys %hash_unused) {
93     print "$key => ".($hash_unused{$key}-1)."\n" unless ($hash_unused{$key}==1);
94 }
95
96 print "Declared tag/subfields unused in the iso2709 file\n";
97 print "=================================================\n";
98 foreach my $key (sort keys %hash_used) {
99     print "$key => ".($hash_used{$key}-1)."\n" if ($hash_used{$key}==1);
100 }
101
102 # foreach my $x (sort keys %resB) {
103 #   print "$x => ".$resB{$x}."\n";
104 # }
105 print "\n==================\n$i record parsed\n";