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

241 lines
6.4 KiB
Perl
Executable file

#!/usr/bin/perl
# This file is part of Koha.
#
# Copyright 2014 BibLibre
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Koha::Script;
use C4::Charset;
use C4::Context;
use C4::Biblio;
use Getopt::Long qw( GetOptions );
use Pod::Usage qw( pod2usage );
my ( $help, $verbose, $confirm, $biblionumbers, $reindex, $filename,
$auto_search, $fix_ampersand );
my $result = GetOptions(
'h|help' => \$help,
'v|verbose' => \$verbose,
'c|confirm' => \$confirm,
'biblionumbers:s' => \$biblionumbers,
'reindex' => \$reindex,
'f|filename:s' => \$filename,
'auto-search' => \$auto_search,
'fix-ampersand' => \$fix_ampersand,
) || pod2usage(1);
# This script only fix ampersand at the moment.
# It is enabled by default.
$fix_ampersand = 1;
if ($help) {
pod2usage(0);
}
unless ( $filename or $biblionumbers or $auto_search ) {
pod2usage(
-exitval => 1,
-message =>
qq{\n\tAt least one record number source should be provided.\n}
);
}
if ( $filename and $biblionumbers
or $filename and $auto_search
or $biblionumbers and $auto_search )
{
pod2usage(
-exitval => 1,
-message => qq{\n\tOnly one record number source should be provided.\n}
);
}
my @biblionumbers;
# We first detect if we have a file or biblos directly entered by command line
#or if we want to use findAmp() sub
if ($auto_search) {
@biblionumbers = biblios_to_sanitize();
}
elsif ($filename) {
if ( -e $filename ) {
open( my $fh, '<', $filename ) || die("Can't open $filename ($!)");
while (<$fh>) {
chomp;
my $line = $_;
push @biblionumbers, split( " |,", $line );
}
close $fh;
}
else {
pod2usage(
-exitval => 1,
-message =>
qq{\n\tThis filename does not exist. Please verify the path is correct.\n}
);
}
}
else {
@biblionumbers = split m|,|, $biblionumbers if $biblionumbers;
}
# We remove spaces
s/(^\s*|\s*$)//g for @biblionumbers;
# Remove empty lines
@biblionumbers = grep { !/^$/ } @biblionumbers;
say @biblionumbers . " records to process" if $verbose;
my @changes;
for my $biblionumber (@biblionumbers) {
print "processing record $biblionumber..." if $verbose;
unless ( $biblionumber =~ m|^\d+$| ) {
say " skipping. ERROR: Invalid biblionumber." if $verbose;
next;
}
my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
unless ($record) {
say " skipping. ERROR: Invalid record." if $verbose;
next;
}
my ( $cleaned_record, $has_been_modified ) =
C4::Charset::SanitizeRecord( $record, $biblionumber );
if ($has_been_modified) {
my $frameworkcode = C4::Biblio::GetFrameworkCode($record);
C4::Biblio::ModBiblio( $cleaned_record, $biblionumber, $frameworkcode )
if $confirm;
push @changes, $biblionumber;
say " Done!" if $verbose;
}
else {
say " Nothing to do." if $verbose;
}
}
if ($verbose) {
say "Total: "
. @changes
. " records "
. ( $confirm ? "cleaned!" : "to clean." );
}
if ( $reindex and $confirm and @changes ) {
say "Now, reindexing using -b -v" if $verbose;
my $kohapath = C4::Context->config('intranetdir');
my $cmd = qq|
$kohapath/misc/migration_tools/rebuild_zebra.pl -b -v -where "biblionumber IN ( |
. join( ',', @changes ) . q| )"
|;
system($cmd);
}
sub biblios_to_sanitize {
my $dbh = C4::Context->dbh;
my $query = q{
SELECT biblionumber
FROM biblio_metadata
WHERE format = 'marcxml'
AND `schema` = ?
AND metadata LIKE "%&amp;amp;%"
};
return @{ $dbh->selectcol_arrayref( $query, { Slice => {} }, C4::Context->preference('marcflavour') ) };
}
=head1 NAME
sanitize_records - This script sanitizes a record.
=head1 SYNOPSIS
sanitize_records.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--biblionumbers=BIBLIONUMBER_LIST] [-f|--filename=FILENAME] [--auto-search] [--reindex] [--fix-ampersand]
You can either give some biblionumbers or a file with biblionumbers or ask for an auto-search.
=head1 OPTIONS
=over
=item B<-h|--help>
Print a brief help message
=item B<-v|--verbose>
Verbose mode.
=item B<-c|--confirm>
This flag must be provided in order for the script to actually
sanitize records. If it is not supplied, the script will
only report on the record list to process.
=item B<--biblionumbers=BIBLIONUMBER_LIST>
Give a biblionumber list using this parameter. They must be separated by
commas.
=item B<-f|--filename=FILENAME>
Give a biblionumber list using a filename. One biblionumber by line or separate them with a whitespace character.
=item B<--auto-search>
Automatically search records containing "&amp;" in biblio_metadata.metadata or in the specified fields.
=item B<--fix-ampersand>
Replace '&amp;' by '&' in the records.
Replace '&amp;amp;amp;etc.' with '&amp;' in the records.
=item B<--reindex>
Reindex the modified records.
=back
=head1 AUTHOR
Alex Arnaud <alex.arnaud@biblibre.com>
Christophe Croullebois <christophe.croullebois@biblibre.com>
Jonathan Druart <jonathan.druart@biblibre.com>
=head1 COPYRIGHT
Copyright 2014 BibLibre
=head1 LICENSE
This file is part of Koha.
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
=cut