Koha/misc/batchCompareMARCvsFrameworks.pl
Jonathan Druart 9d6d641d1f Bug 17600: Standardize our EXPORT_OK
On bug 17591 we discovered that there was something weird going on with
the way we export and use subroutines/modules.
This patch tries to standardize our EXPORT to use EXPORT_OK only.

That way we will need to explicitely define the subroutine we want to
use from a module.

This patch is a squashed version of:
Bug 17600: After export.pl
Bug 17600: After perlimport
Bug 17600: Manual changes
Bug 17600: Other manual changes after second perlimports run
Bug 17600: Fix tests

And a lot of other manual changes.

export.pl is a dirty script that can be found on bug 17600.

"perlimport" is:
git clone https://github.com/oalders/App-perlimports.git
cd App-perlimports/
cpanm --installdeps .
export PERL5LIB="$PERL5LIB:/kohadevbox/koha/App-perlimports/lib"
find . \( -name "*.pl" -o -name "*.pm" \) -exec perl App-perlimports/script/perlimports --inplace-edit --no-preserve-unused --filename {} \;

The ideas of this patch are to:
* use EXPORT_OK instead of EXPORT
* perltidy the EXPORT_OK list
* remove '&' before the subroutine names
* remove some uneeded use statements
* explicitely import the subroutines we need within the controllers or
modules

Note that the private subroutines (starting with _) should not be
exported (and not used from outside of the module except from tests).

EXPORT vs EXPORT_OK (from
https://www.thegeekstuff.com/2010/06/perl-exporter-examples/)
"""
Export allows to export the functions and variables of modules to user’s namespace using the standard import method. This way, we don’t need to create the objects for the modules to access it’s members.

@EXPORT and @EXPORT_OK are the two main variables used during export operation.

@EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace.

@EXPORT_OK does export of symbols on demand basis.
"""

If this patch caused a conflict with a patch you wrote prior to its
push:
* Make sure you are not reintroducing a "use" statement that has been
removed
* "$subroutine" is not exported by the C4::$MODULE module
means that you need to add the subroutine to the @EXPORT_OK list
* Bareword "$subroutine" not allowed while "strict subs"
means that you didn't imported the subroutine from the module:
  - use $MODULE qw( $subroutine list );
You can also use the fully qualified namespace: C4::$MODULE::$subroutine

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-07-16 08:58:47 +02:00

106 lines
3.3 KiB
Perl
Executable file

#!/usr/bin/perl
# small script that dumps an iso2709 file.
use strict;
#use warnings; FIXME - Bug 2505
BEGIN {
# find Koha's Perl modules
# test carefully before changing this
use FindBin ();
eval { require "$FindBin::Bin/kohalib.pl" };
}
# Koha modules used
use Koha::Script;
use C4::Context;
use MARC::File::USMARC;
use MARC::Batch;
use Getopt::Long qw( GetOptions );
use IO::File;
my ( $input_marc_file,$number,$nowarning,$frameworkcode) = ('',0);
my $version;
GetOptions(
'file:s' => \$input_marc_file,
'n:s' => \$number,
'v' => \$version,
'w' => \$nowarning,
'c' => \$frameworkcode,
);
$frameworkcode="" unless $frameworkcode;
if ($version || ($input_marc_file eq '')) {
print <<EOF
This script compares an iso2709 file and Koha's MARC frameworks
It will show the marc fields/subfields used in Koha, and that
are not in the iso2709 file and which fields/subfields that are
used in the iso2709 file and not in Koha.
parameters :
\tv : this version/help screen
\tfile /path/to/file/to/dump : the file to dump
\tw : warning and strict off. If your dump fails, try -w option. It it works, then, the file is iso2709, but a buggy one !
\tc : the frameworkcode. If omitted, set to ""
SAMPLE : ./compare_iso_and_marc_parameters.pl -file /home/paul/koha.dev/local/npl -n 1
EOF
;
die;
}#/
my $fh = IO::File->new($input_marc_file); # don't let MARC::Batch open the file, as it applies the ':utf8' IO layer
my $batch = MARC::Batch->new( 'USMARC', $fh );
$batch->warnings_off() unless $nowarning;
$batch->strict_off() unless $nowarning;
my $dbh=C4::Context->dbh;
my $sth = $dbh->prepare("select tagfield,tagsubfield,tab from marc_subfield_structure where frameworkcode=?");
$sth->execute($frameworkcode);
my %hash_unused;
my %hash_used;
while (my ($tagfield,$tagsubfield,$tab) = $sth->fetchrow) {
$hash_unused{"$tagfield$tagsubfield"} = 1 if ($tab eq -1);
$hash_used{"$tagfield$tagsubfield"} = 1 if ($tab ne -1);
}
my $i=0;
while ( my $record = $batch->next() ) {
$i++;
foreach my $MARCfield ($record->fields()) {
next if $MARCfield->is_control_field(); # tag num < 10
if ($MARCfield) {
foreach my $fields ($MARCfield->subfields()) {
if ($fields) {
if ($hash_unused{$MARCfield->tag().@$fields[0]}>=1) {
$hash_unused{$MARCfield->tag().@$fields[0]}++;
}
if ($hash_used{$MARCfield->tag().@$fields[0]}>=1) {
$hash_used{$MARCfield->tag().@$fields[0]}++;
}
}
# foreach my $field (@$fields) {
# warn "==>".$MARCfield->tag().@$fields[0];
# }
}
}
}
}
print "Undeclared tag/subfields that exists in the file\n";
print "================================================\n";
foreach my $key (sort keys %hash_unused) {
print "$key => ".($hash_unused{$key}-1)."\n" unless ($hash_unused{$key}==1);
}
print "Declared tag/subfields unused in the iso2709 file\n";
print "=================================================\n";
foreach my $key (sort keys %hash_used) {
print "$key => ".($hash_used{$key}-1)."\n" if ($hash_used{$key}==1);
}
# foreach my $x (sort keys %resB) {
# print "$x => ".$resB{$x}."\n";
# }
print "\n==================\n$i record parsed\n";