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

153 lines
4.7 KiB
Perl
Executable file

#!/usr/bin/perl
use strict;
use warnings;
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::ImportBatch qw( GetImportBatch BatchCommitRecords BatchRevertRecords );
use Getopt::Long qw( GetOptions );
$| = 1;
# command-line parameters
my $batch_number = "";
my $list_batches = 0;
my $revert = 0;
my $want_help = 0;
my $result = GetOptions(
'batch-number:s' => \$batch_number,
'list-batches' => \$list_batches,
'revert' => \$revert,
'h|help' => \$want_help
);
if ($want_help or (not $batch_number and not $list_batches)) {
print_usage();
exit 0;
}
if ($list_batches) {
list_batches();
exit 0;
}
# FIXME dummy user so that logging won't fail
# in future, probably should tie to a real user account
C4::Context->set_userenv(0, 'batch', 0, 'batch', 'batch', 'batch', 'batch');
my $dbh = C4::Context->dbh;
$dbh->{AutoCommit} = 0;
if ($batch_number =~ /^\d+$/ and $batch_number > 0) {
my $batch = GetImportBatch($batch_number);
die "$0: import batch $batch_number does not exist in database\n" unless defined $batch;
if ($revert) {
die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
unless $batch->{'import_status'} eq "imported";
revert_batch($batch_number);
} else {
die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n"
unless $batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted";
process_batch($batch_number);
}
$dbh->commit();
} else {
die "$0: please specify a numeric batch ID\n";
}
exit 0;
sub list_batches {
my $results = GetAllImportBatches();
print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n", "#", "File name", "Batch comments", "Status");
print '-' x 5, ' ' , '-' x 25, ' ', '-' x 25, ' ', '-' x 10, "\n" ;
foreach my $batch (@{ $results}) {
if ($batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted") {
print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n",
$batch->{'import_batch_id'},
$batch->{'file_name'},
$batch->{'comments'},
$batch->{'import_status'});
}
}
}
sub process_batch {
my ($import_batch_id) = @_;
print "... importing MARC records -- please wait\n";
my ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored) =
BatchCommitRecords($import_batch_id, '', 100, \&print_progress_and_commit);
print "... finished importing MARC records\n";
print <<_SUMMARY_;
MARC record import report
----------------------------------------
Batch number: $import_batch_id
Number of new records added: $num_added
Number of records replaced: $num_updated
Number of records ignored: $num_ignored
Number of items added: $num_items_added
Number of items replaced: $num_items_replaced
Number of items ignored: $num_items_errored
Note: an item is ignored if its barcode is a
duplicate of one already in the database.
_SUMMARY_
}
sub revert_batch {
my ($import_batch_id) = @_;
print "... reverting batch -- please wait\n";
my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) =
BatchRevertRecords($import_batch_id, 100, \&print_progress_and_commit);
print "... finished reverting batch\n";
print <<_SUMMARY_;
MARC record import report
----------------------------------------
Batch number: $import_batch_id
Number of records deleted: $num_deleted
Number of errors: $num_errors
Number of records reverted: $num_reverted
Number of records ignored: $num_ignored
Number of items added: $num_items_deleted
_SUMMARY_
}
sub print_progress_and_commit {
my $recs = shift;
print "... processed $recs records\n";
$dbh->commit();
}
sub print_usage {
print <<_USAGE_;
$0: import a batch of staged MARC records into database.
Use this batch job to complete the import of a batch of
MARC records that was staged either by the batch job
stage_file.pl or by the Koha Tools option
"Stage MARC Records for Import".
Parameters:
--batch-number <#> number of the record batch
to import
--list-batches print a list of record batches
available to commit
--revert revert a batch instead of importing it
--help or -h show this message.
_USAGE_
}