From 71134a7c9617a3003ea46b46d3778fbb1a04f9ee Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Wed, 9 Oct 2019 15:18:15 +0000 Subject: [PATCH] Bug 14620: Patron contact validation script Goes through patron database and validates their email and phonenumber fields. Only reports issues, does not modify anything. Usage: ./misc/maintenance/validate_patron_contact_data.pl --help Example output: =============== =============== ======================================== Borrowernumber Field Value =============== =============== ======================================== 29 phone 1NV4L1D 30 smsalertnumber lol 30 phone 1NV4L1D 39 smsalertnumber lol 39 phone 1NV4L1D 40 smsalertnumber lol 40 phone 1NV4L1D 41 smsalertnumber lol 41 phone 1NV4L1D 42 smsalertnumber lol =============== =============== ======================================== Borrowernumber Field Value =============== =============== ======================================== --- .../validate_patron_contact_data.pl | 199 ++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 misc/maintenance/validate_patron_contact_data.pl diff --git a/misc/maintenance/validate_patron_contact_data.pl b/misc/maintenance/validate_patron_contact_data.pl new file mode 100644 index 0000000000..e25ea181b5 --- /dev/null +++ b/misc/maintenance/validate_patron_contact_data.pl @@ -0,0 +1,199 @@ +#!/usr/bin/perl +# +# Copyright (C) 2019 Koha-Suomi Oy +# +# 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 . + +use Modern::Perl; +use Getopt::Long; +use Pod::Usage; + +use Koha::Patrons; +use Koha::Script; +use Koha::Validation; + +sub usage { + pod2usage( -verbose => 2 ); + exit; +} + +sub validate_patrons { + my ($not_expired, $category, $no_email, $no_emailpro, + $no_B_email, $no_phone, $no_phonepro, $no_B_phone, + $no_mobile, $no_smsalertnumber) = @_; + + my $search = {}; + + if ($not_expired) { + $search->{dateexpiry} = + dt_from_string(undef, undef, 'floating')->truncate( to => 'day' ); + } + + if ($category) { + $search->{categorycode} = $category; + } + + my $emailvalidator = 'email'; + my $phonevalidator = 'phone'; + + my $validatephones = C4::Context->preference("ValidatePhoneNumber"); + $no_phone = 1 if !$validatephones; + $no_phonepro = 1 if !$validatephones; + $no_B_phone = 1 if !$validatephones; + $no_mobile = 1 if !$validatephones; + $no_smsalertnumber = 1 if !$validatephones; + + my $validate = { + 'email' => !$no_email ? $emailvalidator : undef, + 'emailpro' => !$no_emailpro ? $emailvalidator : undef, + 'B_email' => !$no_email ? $emailvalidator : undef, + 'phone' => !$no_phone ? $phonevalidator : undef, + 'phonepro' => !$no_phonepro ? $phonevalidator : undef, + 'B_phone' => !$no_B_phone ? $phonevalidator : undef, + 'mobile' => !$no_mobile ? $phonevalidator : undef, + 'smsalertnumber' => !$no_smsalertnumber ? $phonevalidator : undef, + }; + + my $patrons = Koha::Patrons->search($search)->as_list; +format STDOUT_TOP = +=============== =============== ======================================== +Borrowernumber Field Value +=============== =============== ======================================== +. + + foreach my $patron (@$patrons) { + my $bn = $patron->borrowernumber; + foreach my $field (keys %{$validate}) { + my $validator = $validate->{$field}; + my $value = $patron->$field; + next unless $value; + unless (Koha::Validation->$validator($value)) { +format STDOUT = +@<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +$bn, $field, $value +. + + write; + } + } + } +my $footer = <<"END_FOOTER" +=============== =============== ======================================== +Borrowernumber Field Value +=============== =============== ======================================== +END_FOOTER +; +print "$footer\nDONE.\n"; + +} + + +my ($confirm, $since, $help, $not_expired, $category,$no_email, + $no_emailpro, $no_B_email, $no_phone, $no_phonepro, $no_B_phone, + $no_mobile, $no_smsalertnumber); +my $result = GetOptions( + 'confirm' => \$confirm, + 'not-expired' => \$not_expired, + 'category:s' => \$category, + 'no-email' => \$no_email, + 'no-emailpro' => \$no_emailpro, + 'no-B_email' => \$no_B_email, + 'no-phone' => \$no_phone, + 'no-phonepro' => \$no_phonepro, + 'no-B_phone' => \$no_B_phone, + 'no-mobile' => \$no_mobile, + 'no-smsalertnumber' => \$no_smsalertnumber, + 'help|h' => \$help, +); + +usage() if $help; + +validate_patrons($not_expired, $category, $no_email, $no_emailpro, + $no_B_email, $no_phone, $no_phonepro, $no_B_phone, + $no_mobile, $no_smsalertnumber) if $confirm; + +=head1 NAME + +validate_patron_contact_data.pl + +=head1 SYNOPSIS + + validate_patron_contact_data.pl + validate_patron_contact_data.pl --help + validate_patron_contact_data.pl --confirm + validate_patron_contact_data.pl --confirm --not-expired + validate_patron_contact_data.pl --confirm --category PT + validate_patron_contact_data.pl --confirm --no-email --no-emailpro --no-B_email + validate_patron_contact_data.pl --confirm --no-phone --no-phonepro --no-B_phone --no-smsalertnumber + +=head1 DESCRIPTION + +This script iterates patron database and reports invalid emails and phone +numbers to STDOUT. + +=over 8 + +=item B<--help> + +Prints this help + +=item B<--confirm> + +Actually start the script. + +=item B<--not-expired> + +Will only check active patrons (patrons who didn't pass their expiration date). + +=item B<--category> + +Will only update patrons in the category specified. + +=item B<--no-email> + +Exclude field email from validation. + +=item B<--no-emailpro> + +Exclude field emailpro from validation. + +=item B<--no-B_email> + +Exclude field B_email from validation. + +=item B<--no-phone> + +Exclude field phone from validation. + +=item B<--no-phonepro> + +Exclude field phonepro from validation. + +=item B<--no-B_phone> + +Exclude field B_phone from validation. + +=item B<--no-mobile> + +Exclude field mobile from validation. + +=item B<--no-smsalertnumber> + +Exclude field smsalertnumber from validation. + +=back + +=cut -- 2.17.1