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

171 lines
5 KiB
Perl
Executable file

#!/usr/bin/perl
#
# Copyright (C) 2008 LibLime
#
# 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>.
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 Getopt::Long qw( GetOptions );
use Koha::DateUtils qw( dt_from_string output_pref );
=head1 NAME
fix_accountlines_date.pl - Fix date code in the description of fines
=head1 SYNOPSIS
fix_accountlines_date.pl -m date_format [ -n fines_to_process ] [ -d ] [ --help or -h ]
Options:
--help or -h Brief usage message
--man Full documentation
-n fines_to_process How many fines to process; if left off will
process all
-m date_format What format the dates are currently in; 'us'
or 'metric' (REQUIRED)
-d Run in debugging mode
=head1 DESCRIPTION
This script fixes the date code in the description of fines. Previously, the
format of this was determined by which script you were using to update fines (see the -m option)
=over 8
=item B<--help>
Prints a brief usage message and exits.
=item B<--man>
Prints a full manual page and exits.
=item B<-n>
Process only a certain amount of fines. If this option is left off, this script
will process everything.
=item B<-m>
This required option tells the script what format your dates are currently in.
If you were previously using the fines2.pl or fines-sanop.pl script to update
your fines, they will be in 'metric' format. If you were using the fines-ll.pl
script, they will be in 'us' format. After this script is finished, they will
be in whatever format your 'dateformat' system preference specifies.
=item B<-d>
Run in debugging mode; this prints out a lot of information and should be used
only if there is a problem and with the '-n' option.
=back
=cut
my $mode = '';
my $want_help = 0;
my $limit = -1;
my $done = 0;
my $DEBUG = 0;
# Regexes for the two date formats
our $US_DATE = '((0\d|1[0-2])\/([0-2]\d|3[01])\/(\d{4}))';
our $METRIC_DATE = '(([0-2]\d|3[01])\/(0\d|1[0-2])\/(\d{4}))';
sub print_usage {
print <<_USAGE_
$0: Fix the date code in the description of fines
Due to the multiple scripts used to update fines in earlier versions of Koha,
this script should be used to change the format of the date codes in the
accountlines table before you start using Koha 3.0.
Parameters:
--mode or -m This should be 'us' or 'metric', and tells the script
what format your old dates are in.
--debug or -d Run this script in debug mode.
--limit or -n How many accountlines rows to fix; useful for testing.
--help or -h Print out this help message.
_USAGE_
}
my $result = GetOptions(
'm=s' => \$mode,
'd' => \$DEBUG,
'n=i' => \$limit,
'help|h' => \$want_help,
);
if (not $result or $want_help or ($mode ne 'us' and $mode ne 'metric')) {
print_usage();
exit 0;
}
our $dbh = C4::Context->dbh;
$dbh->{AutoCommit} = 0;
my $sth = $dbh->prepare("
SELECT accountlines_id, description
FROM accountlines
WHERE accounttype in ('FU', 'F', 'M')
;");
$sth->execute();
my $update_sth = $dbh->prepare('
UPDATE accountlines
SET description = ?
WHERE accountlines_id = ?
;');
while (my $accountline = $sth->fetchrow_hashref) {
my $description = $accountline->{'description'};
my $updated = 0;
if ($mode eq 'us') {
if ($description =~ /$US_DATE/) { # mm/dd/yyyy
my $date = output_pref( { dt => dt_from_string( $1, 'us' ), dateonly => 1} );
print "Converting $1 (us) to " . $date . "\n" if $DEBUG;i
$description =~ s/$US_DATE/$date/;
$updated = 1;
}
} elsif ($mode eq 'metric') {
if ($description =~ /$METRIC_DATE/) { # dd/mm/yyyy
my $date = output_pref( { dt => dt_from_string( $1, 'metric' ), dateonly => 1} );
print "Converting $1 (metric) to " . $date . "\n" if $DEBUG;
$description =~ s/$METRIC_DATE/$date/;
$updated = 2;
}
}
print "Changing description from '" . $accountline->{'description'} . "' to '" . $description . "'\n" if $DEBUG;
$update_sth->execute($description, $accountline->{'accountlines_id'});
$done++;
last if ($done == $limit); # $done can't be -1, so this works
}
$dbh->commit();