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