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