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