Updates to go along with jquery tabs plugin update
[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 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 compares an iso2709 file and Koha's MARC frameworks
35 It will show the marc fields/subfields used in Koha, and that 
36 are not in the iso2709 file and which fields/subfields that are
37 used in the iso2709 file and not in Koha.
38
39 parameters :
40 \tv : this version/help screen
41 \tfile /path/to/file/to/dump : the file to dump
42 \tw : warning and strict off. If your dump fails, try -w option. It it works, then, the file is iso2709, but a buggy one !
43 \tc : the frameworkcode. If omitted, set to ""
44
45 SAMPLE : ./compare_iso_and_marc_parameters.pl -file /home/paul/koha.dev/local/npl -n 1 
46
47 EOF
48 ;
49 die;
50 }#/
51
52 my $batch = MARC::Batch->new( 'USMARC', $input_marc_file );
53 $batch->warnings_off() unless $nowarning;
54 $batch->strict_off() unless $nowarning;
55 my $dbh=C4::Context->dbh;
56 my $sth = $dbh->prepare("select tagfield,tagsubfield,tab from marc_subfield_structure where frameworkcode=?");
57 $sth->execute($frameworkcode);
58
59 my %hash_unused;
60 my %hash_used;
61 while (my ($tagfield,$tagsubfield,$tab) = $sth->fetchrow) {
62     $hash_unused{"$tagfield$tagsubfield"} = 1 if ($tab eq -1);
63     $hash_used{"$tagfield$tagsubfield"} = 1 if ($tab ne -1);
64 }
65 my $i=0;
66 while ( my $record = $batch->next() ) {
67     $i++;
68     foreach my $MARCfield ($record->fields()) {
69     next if $MARCfield->tag()<=010;
70         if ($MARCfield) {
71             foreach my $fields ($MARCfield->subfields()) {
72                 if ($fields) {
73                     if ($hash_unused{$MARCfield->tag().@$fields[0]}>=1) {
74                         $hash_unused{$MARCfield->tag().@$fields[0]}++;
75                     }
76                     if ($hash_used{$MARCfield->tag().@$fields[0]}>=1) {
77                         $hash_used{$MARCfield->tag().@$fields[0]}++;
78                     }
79                 }
80     #           foreach my $field (@$fields) {
81     #               warn "==>".$MARCfield->tag().@$fields[0];
82     #           }
83             }
84         }
85     }
86 }
87 print "Undeclared tag/subfields that exists in the file\n";
88 print "================================================\n";
89 foreach my $key (sort keys %hash_unused) {
90     print "$key => ".($hash_unused{$key}-1)."\n" unless ($hash_unused{$key}==1);
91 }
92
93 print "Declared tag/subfields unused in the iso2709 file\n";
94 print "=================================================\n";
95 foreach my $key (sort keys %hash_used) {
96     print "$key => ".($hash_used{$key}-1)."\n" if ($hash_used{$key}==1);
97 }
98
99 # foreach my $x (sort keys %resB) {
100 #   print "$x => ".$resB{$x}."\n";
101 # }
102 print "\n==================\n$i record parsed\n";