Koha/misc/import_patrons.pl
Kyle M Hall d3005a2a4c Bug 27586: Import patrons script has a confirm switch that doesn't nothing!
If the script misc/import_patrons.pl is run without "--confirm" it reports
that it is "running in dry-run mode" but it is not!

Test Plan:
1) Import a CSV of patrons using import_patrons.pm *without* the --confirm parameter
2) Note that it does actually import the patrons
3) Apply this patch
4) Note that this time the patrons are not actually imported
5) Import again with --confirm
6) Note that the patrons were actually imported this time!

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-02-08 14:55:59 +01:00

153 lines
4 KiB
Perl
Executable file

#!/usr/bin/perl
# Parts copyright 2014 ByWater Solutions
#
# 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 Modern::Perl;
use Getopt::Long;
use Pod::Usage;
use Koha::Script;
use Koha::Patrons::Import;
my $Import = Koha::Patrons::Import->new();
my $csv_file;
my $matchpoint;
my $overwrite_cardnumber;
my $overwrite_passwords;
my %defaults;
my $ext_preserve = 0;
my $confirm;
my $verbose = 0;
my $help;
GetOptions(
'c|confirm' => \$confirm,
'f|file=s' => \$csv_file,
'm|matchpoint=s' => \$matchpoint,
'd|default=s' => \%defaults,
'o|overwrite' => \$overwrite_cardnumber,
'op|overwrite_passwords' => \$overwrite_passwords,
'p|preserve-extended-attributes' => \$ext_preserve,
'v|verbose+' => \$verbose,
'h|help|?' => \$help,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(q|--file is required|) unless $csv_file;
pod2usage(q|--matchpoint is required|) unless $matchpoint;
warn "Running in dry-run mode, provide --confirm to apply the changes\n" unless $confirm;
my $handle;
open( $handle, "<", $csv_file ) or die $!;
my $return = $Import->import_patrons(
{
file => $handle,
defaults => \%defaults,
matchpoint => $matchpoint,
overwrite_cardnumber => $overwrite_cardnumber,
overwrite_passwords => $overwrite_passwords,
preserve_extended_attributes => $ext_preserve,
dry_run => !$confirm,
}
);
my $feedback = $return->{feedback};
my $errors = $return->{errors};
my $imported = $return->{imported};
my $overwritten = $return->{overwritten};
my $alreadyindb = $return->{already_in_db};
my $invalid = $return->{invalid};
if ($verbose) {
my $total = $imported + $alreadyindb + $invalid + $overwritten;
say q{};
say "Import complete:";
say "Imported: $imported";
say "Overwritten: $overwritten";
say "Skipped: $alreadyindb";
say "Invalid: $invalid";
say "Total: $total";
say q{};
}
if ($verbose > 1 ) {
say "Errors:";
say Data::Dumper::Dumper( $errors );
}
if ($verbose > 2 ) {
say "Feedback:";
say Data::Dumper::Dumper( $feedback );
}
=head1 NAME
import_patrons.pl - CLI script to import patrons data into Koha
=head1 SYNOPSIS
import_patrons.pl --file /path/to/patrons.csv --matchpoint cardnumber --confirm [--default branchcode=MPL] [--overwrite] [--preserve-extended-attributes] [--verbose]
=head1 OPTIONS
=over 8
=item B<-h|--help>
Prints a brief help message and exits
=item B<-c|--confirm>
Confirms you really want to import these patrons, otherwise prints this help
=item B<-f|--file>
Path to the CSV file of patrons to import
=item B<-c|--matchpoint>
Field on which to match incoming patrons to existing patrons
=item B<-d|--default>
Set defaults to patron fields, repeatable e.g. --default branchcode=MPL --default categorycode=PT
=item B<-o|--overwrite>
Overwrite existing patrons with new data if a match is found
=item B<-p|--preserve-extended-attributes>
Retain extended patron attributes for existing patrons being overwritten
=item B<-v|--verbose>
Be verbose
Multiple -v options increase the verbosity
2 repetitions or above will report lines in error
3 repetitions or above will report feedback
=back
=cut