MT 2116: Addons to the CSV export
[koha.git] / misc / batchCompareMARCvsFrameworks.pl
blobb0ed79736b7d6c93ed96919827c721fce34a2948
1 #!/usr/bin/perl
2 # small script that dumps an iso2709 file.
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" };
13 # Koha modules used
14 use C4::Context;
15 use MARC::File::USMARC;
16 use MARC::Record;
17 use MARC::Batch;
19 use Getopt::Long;
20 use IO::File;
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,
32 $frameworkcode="" unless $frameworkcode;
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.
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 ""
47 SAMPLE : ./compare_iso_and_marc_parameters.pl -file /home/paul/koha.dev/local/npl -n 1
49 EOF
51 die;
52 }#/
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);
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);
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]}++;
79 if ($hash_used{$MARCfield->tag().@$fields[0]}>=1) {
80 $hash_used{$MARCfield->tag().@$fields[0]}++;
83 # foreach my $field (@$fields) {
84 # warn "==>".$MARCfield->tag().@$fields[0];
85 # }
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);
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);
102 # foreach my $x (sort keys %resB) {
103 # print "$x => ".$resB{$x}."\n";
105 print "\n==================\n$i record parsed\n";