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

147 lines
4.5 KiB
Perl
Executable file

#!/usr/bin/perl
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" };
}
use Koha::Script;
use C4::Context;
use C4::Biblio qw( GetMarcBiblio ModBiblio );
use Koha::Items;
use Getopt::Long qw( GetOptions );
$| = 1;
# command-line parameters
my $want_help = 0;
my $do_update = 0;
my $wherestrings;
my $result = GetOptions(
'run-update' => \$do_update,
'where=s@' => \$wherestrings,
'h|help' => \$want_help,
);
if (not $result or $want_help or not $do_update) {
print_usage();
exit 0;
}
my $num_bibs_processed = 0;
my $num_bibs_modified = 0;
my $num_noitem_forbarcode = 0;
my $num_nobarcode_inhostfield =0;
my $num_hostfields_unabletomodify =0;
my $num_bad_bibs = 0;
my $dbh = C4::Context->dbh;
$dbh->{AutoCommit} = 0;
process_bibs();
$dbh->commit();
exit 0;
sub process_bibs {
my $sql = "SELECT biblionumber FROM biblio JOIN biblioitems USING (biblionumber)";
$sql.="WHERE ". join(" AND ",@$wherestrings) if ($wherestrings);
$sql.="ORDER BY biblionumber ASC";
my $sth = $dbh->prepare($sql);
eval{$sth->execute();};
if ($@){ die "error $@";};
while (my ($biblionumber) = $sth->fetchrow_array()) {
$num_bibs_processed++;
process_bib($biblionumber);
if (($num_bibs_processed % 100) == 0) {
print_progress_and_commit($num_bibs_processed);
}
}
$dbh->commit;
print <<_SUMMARY_;
Create Analytical records relationships report
-----------------------------------------------
Number of bibs checked: $num_bibs_processed
Number of bibs modified: $num_bibs_modified
Number of hostfields with no barcodes: $num_nobarcode_inhostfield
Number of barcodes not found: $num_noitem_forbarcode
Number of hostfields unable to modify: $num_hostfields_unabletomodify
Number of bibs with errors: $num_bad_bibs
_SUMMARY_
}
sub process_bib {
my $biblionumber = shift;
my $bib = GetMarcBiblio({ biblionumber => $biblionumber });
unless (defined $bib) {
print "\nCould not retrieve bib $biblionumber from the database - record is corrupt.\n";
$num_bad_bibs++;
return;
}
#loop through each host field and populate subfield 0 and 9
my $analyticfield = '773';
foreach my $hostfield ( $bib->field($analyticfield) ) {
if(my $barcode = $hostfield->subfield('o')){
my $item = Koha::Items->find({ barcode => $barcode });
if ($item) {
my $modif;
if ( $hostfield->subfield('0') ne $biblionumber ) {
$hostfield->update( '0', $biblionumber );
$modif = 1;
}
if ( $hostfield->subfield('9') ne $item->itemnumber ) {
$hostfield->update( '9', $item->itemnumber );
$modif = 1;
}
if ($modif) {
$num_bibs_modified++;
my $modresult = ModBiblio( $bib, $biblionumber, '' );
warn "Modifying biblio $biblionumber";
if ( !$modresult ) {
warn "Unable to modify biblio $biblionumber with update host field";
$num_hostfields_unabletomodify++;
}
}
}
else {
warn "No item record found for barcode $barcode";
$num_noitem_forbarcode++;
}
} else{
warn "No barcode in host field for biblionumber $biblionumber";
$num_nobarcode_inhostfield++;
}
}
}
sub print_progress_and_commit {
my $recs = shift;
$dbh->commit();
print "... processed $recs records\n";
}
sub print_usage {
print <<_USAGE_;
$0: establish relationship to host items
Based on barcode in host field populates subfield 0 with host biblionumber and subfield 9 with host itemnumber.
Subfield 0 and 9 are used in Koha screns to display relationships between analytical records and host bibs and items.
NOT usable with UNIMARC data. You can use it only if you have tag 461 with also an items id (like barcode or item numbers). In UNIMARC this situation is very rare. If you have data coded in this way, send a mail to koha-dev mailing list and ask for the feature.
Parameters:
--run-update run the synchronization
--where condition selects the biblios on a criterium (Repeatable)
--help or -h show this message.
_USAGE_
}