Koha/misc/migration_tools/buildEDITORS.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

250 lines
8.7 KiB
Perl
Executable file
Raw Blame History

#!/usr/bin/perl
# script that rebuild EDITORS
use strict;
#use warnings; FIXME - Bug 2505
# Koha modules used
use MARC::File::USMARC;
use MARC::Record;
use MARC::Batch;
use Koha::Script;
use C4::Context;
use C4::Biblio qw( GetMarcBiblio );
use C4::AuthoritiesMarc;
use Time::HiRes qw( gettimeofday );
use Getopt::Long qw( GetOptions );
my ( $input_marc_file, $number) = ('',0);
my ($version, $verbose, $test_parameter, $confirm,$delete);
GetOptions(
'h' => \$version,
'd' => \$delete,
't' => \$test_parameter,
'v' => \$verbose,
'c' => \$confirm,
);
if ($version or !$confirm) {
print <<EOF
small script to recreate a authority table into Koha.
This will parse all your biblios to recreate isbn / editor / collections for the unimarc_210c and unimarc_225a plugins.
Remember those plugins will work only if you have an EDITORS authority type, with
\t200a being the first 2 parts of an ISBN
\t200b being the editor name
\t200c (repeatable) being the series title
parameters :
\t-c : confirmation flag. the script will run only with this flag. Otherwise, it will just show this help screen.
\t-d : delete existing EDITORS before rebuilding them
\t-t : test parameters : run the script but don't create really the EDITORS
EOF
;#'
exit;
}
my $dbh = C4::Context->dbh;
if ($delete) {
print "deleting EDITORS\n";
my $del1 = $dbh->prepare("delete from auth_subfield_table where authid=?");
my $del2 = $dbh->prepare("delete from auth_word where authid=?");
my $sth = $dbh->prepare("select authid from auth_header where authtypecode='EDITORS'");
$sth->execute;
while (my ($authid) = $sth->fetchrow) {
$del1->execute($authid);
$del2->execute($authid);
}
$dbh->do("delete from auth_header where authtypecode='EDITORS'");
}
if ($test_parameter) {
print "TESTING MODE ONLY\n DOING NOTHING\n===============\n";
}
$|=1; # flushes output
my $starttime = gettimeofday;
my $sth = $dbh->prepare("select bibid from marc_biblio");
$sth->execute;
my $i=1;
my $counter;
my %hash;
while (my ($bibid) = $sth->fetchrow) {
my $record = GetMarcBiblio({ biblionumber => $bibid });
my $isbnField = $record->field('010');
next unless $isbnField;
my $isbn=$isbnField->subfield('a');
my $seg1;
if(substr($isbn, 0, 1) <=7) {
$seg1 = substr($isbn, 0, 1);
} elsif(substr($isbn, 0, 2) <= 94) {
$seg1 = substr($isbn, 0, 2);
} elsif(substr($isbn, 0, 3) <= 995) {
$seg1 = substr($isbn, 0, 3);
} elsif(substr($isbn, 0, 4) <= 9989) {
$seg1 = substr($isbn, 0, 4);
} else {
$seg1 = substr($isbn, 0, 5);
}
my $x = substr($isbn, length($seg1));
my $seg2;
if(substr($x, 0, 2) <= 19) {
# if(sTmp2 < 10) sTmp2 = "0" sTmp2;
$seg2 = substr($x, 0, 2);
} elsif(substr($x, 0, 3) <= 699) {
$seg2 = substr($x, 0, 3);
} elsif(substr($x, 0, 4) <= 8399) {
$seg2 = substr($x, 0, 4);
} elsif(substr($x, 0, 5) <= 89999) {
$seg2 = substr($x, 0, 5);
} elsif(substr($x, 0, 6) <= 9499999) {
$seg2 = substr($x, 0, 6);
} else {
$seg2 = substr($x, 0, 7);
}
$counter++;
print ".";
my $timeneeded = gettimeofday - $starttime;
print "$counter in $timeneeded s\n" unless ($counter % 100);
my $field = $record->field('210');
my $editor;
$editor=$field->subfield('c') if $field;
$field = $record->field('225');
my $collection;
$collection=$field->subfield('a') if $field;
# print "WARNING : editor empty for ".$record->as_formatted unless $editor and !$verbose;
$hash{$seg1.$seg2}->{editors} = $editor unless ($hash{$seg1.$seg2}->{editors});
$hash{$seg1.$seg2}->{collections}->{$collection}++ if $collection;
}
foreach my $isbnstart (sort keys %hash) {
print "$isbnstart -- ".$hash{$isbnstart}->{editors} if $verbose;
my $collections = $hash{$isbnstart}->{collections};
my $seriestitlelist;
foreach my $collection (sort keys %$collections) {
print " CC $collection : ".$collections->{$collection} if $verbose;
$seriestitlelist.=$collection."|";
}
my $authorityRecord = MARC::Record->new();
my $newfield = MARC::Field->new(200,'','','a' => "".$isbnstart,
'b' => "".$hash{$isbnstart}->{editors},
'c' => "".$seriestitlelist);
$authorityRecord->insert_fields_ordered($newfield);
my $authid=AUTHaddauthority($dbh,$authorityRecord,'','EDITORS');
# print $authorityRecord->as_formatted."\n";
print "\n" if $verbose;
}
exit;
# my $timeneeded = gettimeofday - $starttime;
# print "$i in $timeneeded s\n" unless ($i % 50);
# foreach my $field ($record->field(995)) {
# $record->delete_field($field);
# }
# my $totdone=0;
# my $authid;
# foreach my $fieldnumber (('710','711','712')) {
# foreach my $field ($record->field($fieldnumber)) {
# # print "=>".$field->as_formatted."\n";
# foreach my $authentry ($field->subfield("a")) {
# my $hashentry = $authentry;
# # la particularit<69>de ce script l<> c'est que l'entr<74> dans la table d'autorit<69>est $a -- $b (et pas $x -- $x -- $x -- $a comme pour les autorit<69> NC)
# # si n<>essaire, compl<70>er avec le $c (n'existe pas dans le fichier que j'ai migr<67>avec cette moulinette
# # supprimer les accents, certaines entr<74>s sont sans, d'autres avec !
# # mysql ne diff<66>encie pas, mais les hash perl oui !
# $hashentry =~ s/<2F><><EFBFBD>e/g;
# $hashentry =~ s/<2F><>a/g;
# $hashentry =~ s/<2F>i/g;
# $hashentry =~ s/<2F>o/g;
# $hashentry =~ s/|/u/g;
# $hashentry = uc($hashentry);
# print "==>$hashentry" if $hashentry =~ /.*ETATS.*/;
# $totdone++;
# if ($alreadydone{$hashentry}) {
# $authid = $alreadydone{$hashentry};
# print ".";
# } else {
# print "*";
# #create authority.
# my $authorityRecord = MARC::Record->new();
# my $newfield = MARC::Field->new(210,'','','a' => "".$authentry,
# 'b' => "".$field->subfield('b'),
# 'c' => "".$field->subfield('c'),
# );
# $authorityRecord->insert_fields_ordered($newfield);
# $authid=AUTHaddauthority($dbh,$authorityRecord,'','CO');
# $alreadydone{$hashentry} = $authid;
# # OK, on garde la notice d'autorit<69> on cherche les notices biblio et on les met <20>jour...
# if ($fieldnumber eq '710') {
# $sthBIBLIOS710->execute($authentry);
# while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS710->fetchrow) {
# my $inbiblio = GetMarcBiblio({ biblionumber => $bibid });
# my $isOK = 0;
# foreach my $in7xx ($inbiblio->field($fieldnumber)) {
# # !!!!! ici, il faut reconstruire l'entr<74> de la table de hachage comme ci dessus
# # sinon,
# my $inEntry = $in7xx->subfield('a');
# $inEntry =~ s/<2F><><EFBFBD>e/g;
# $inEntry =~ s/<2F><>a/g;
# $inEntry =~ s/<2F>i/g;
# $inEntry =~ s/<2F>o/g;
# $inEntry =~ s/|/u/g;
# $inEntry = uc($inEntry);
# $isOK=1 if $inEntry eq $hashentry;
# }
# C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
# }
# }
# if ($fieldnumber eq '711') {
# $sthBIBLIOS711->execute($authentry);
# while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS711->fetchrow) {
# my $inbiblio = GetMarcBiblio({ biblionumber => $bibid });
# my $isOK = 0;
# foreach my $in7xx ($inbiblio->field($fieldnumber)) {
# # !!!!! ici, il faut reconstruire l'entr<74> de la table de hachage comme ci dessus
# # sinon,
# my $inEntry = $in7xx->subfield('a');
# $inEntry =~ s/<2F><><EFBFBD>e/g;
# $inEntry =~ s/<2F><>a/g;
# $inEntry =~ s/<2F>i/g;
# $inEntry =~ s/<2F>o/g;
# $inEntry =~ s/|/u/g;
# $inEntry = uc($inEntry);
# $isOK=1 if $inEntry eq $hashentry;
# }
# C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
# }
# }
# if ($fieldnumber eq '712') {
# $sthBIBLIOS712->execute($authentry);
# while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS712->fetchrow) {
# my $inbiblio = GetMarcBiblio({ biblionumber => $bibid });
# my $isOK = 0;
# foreach my $in7xx ($inbiblio->field($fieldnumber)) {
# # !!!!! ici, il faut reconstruire l'entr<74> de la table de hachage comme ci dessus
# # sinon,
# my $inEntry = $in7xx->subfield('a');
# $inEntry =~ s/<2F><><EFBFBD>e/g;
# $inEntry =~ s/<2F><>a/g;
# $inEntry =~ s/<2F>i/g;
# $inEntry =~ s/<2F>o/g;
# $inEntry =~ s/|/u/g;
# $inEntry = uc($inEntry);
# $isOK=1 if $inEntry eq $hashentry;
# }
# C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
# }
# }
# }
# }
# }
# }
# $i++;
# }
# my $timeneeded = gettimeofday - $starttime;
# print "$i entries done in $timeneeded seconds (".($i/$timeneeded)." per second)\n";