Koha/misc/cronjobs/nl-sync-from-koha.pl
Magnus Enger 290341d8db Bug 11401: Add support for Norwegian national library card
This patch makes it possible to sync patron data between Koha and the
Norwegian national patron database, in both directions.

In order to use this, the following information is necessary:
- a username/password from the Norwegian national database of libraries
  ("Base Bibliotek"), available to all Norwegian libraries
- a special key in order to decrypt and encrypt PIN-codes/passwords,
  which is only available to Norwegian library system vendors
- a norwegian library vendor username/password

See http://www.lanekortet.no/ for more information (in Norwegian).

While this is of course an implementation of a specific synchronization scheme
for borrower data, attempts have been made to prepare the ground for other sync
schemes that might be implemented later. Especially the structure of the new
borrower_sync table might be reviewed with an eye to how it might fit other
schemes.

To test:

Since the password and cryptographic key needed to use this functionality
is only available to Norwegian library system vendors, only regression testing
can be done on the submitted code. Suggested things to check:

- Apply the patch and make sure the database update is done. This should add
  the new "borrower_sync" table and five new systmpreferences under the
  "Patrons" > "Norwegian patron database" category:
  - NorwegianPatronDBEnable
  - NorwegianPatronDBEndpoint
  - NorwegianPatronDBUsername
  - NorwegianPatronDBPassword
  - NorwegianPatronDBSearchNLAfterLocalHit
- Check that patrons can be created, edited and deleted as usual, when
  NorwegianPatronDBEnable is set to "Disable"
- Check that the new tests in t/NorwegianPatronDB.pm run ok, e.g. on a
  gitified setup:
  $ sudo koha-shell -c "PERL5LIB=/path/to/kohaclone prove -v t/NorwegianPatronDB.t" instancename
- Check that all the other tests still run ok
- Check that the POD in the new files itroduced by this patch looks ok:
  - Koha/NorwegianPatronDB.pm
  - members/nl-search.pl
  - misc/cronjobs/nl-sync-from-koha.pl
  - misc/cronjobs/nl-sync-to-koha.pl
  - t/NorwegianPatronDB.t

Sponsored-by: Oslo Public Library

Update 2014-09-18:
- Rebase on master
- Split out changes to Koha::Schema
- Incorporate new way of authenticating with NL

Update 2014-10-21:
- Rebase on master
- Use Module::Load to load Koha::NorwegianPatronDB in non-NL-specific
  scripts and modules
- Fix the version number of Digest::SHA
- Fix a missing semicolon in kohastructure.sql

Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
2014-11-14 09:42:23 -03:00

184 lines
4.3 KiB
Perl

#!/usr/bin/perl
# Copyright 2014 Oslo Public Library
=head1 NAME
nl-sync-from-koha.pl - Sync patrons from Koha to the Norwegian national patron database (NL).
=head1 SYNOPSIS
perl nl-sync-from-koha.pl -v --run
=cut
use Koha::NorwegianPatronDB qw( NLCheckSysprefs NLSync );
use Koha::Database;
use Getopt::Long;
use Pod::Usage;
use Modern::Perl;
# Get options
my ( $run, $verbose, $debug ) = get_options();
=head1 ACTIONS
=head2
Find local patrons that have been changed and need to be sent upstream to NL.
These patrons will be distinguished by two borrower attributes:
=over 4
=item * The "nlstatus" attribute will have a value of "needsync". (Which means
that the patron has been changed in Koha, but not yet successfully synced
upstream.)
=item * The "nlsync" attribute will have a value of 1. (Which means that this
patron has accepted to be synced with NL, as opposed to a value of 0 which
would indicate that the patron has asked not to be synced with NL.)
=back
=head1 STEPS
This script performs the following steps:
=head2 Check sysprefs
=cut
my $check_result = NLCheckSysprefs();
if ( $check_result->{'error'} == 1 ) {
if ( $check_result->{'nlenabled'} == 0 ) { say "* Please activate this function with the NorwegianPatronDBEnable system preference." };
if ( $check_result->{'endpoint'} == 0 ) { say "* Please specify an endpoint with the NorwegianPatronDBEndpoint system preference." };
if ( $check_result->{'userpass'} == 0 ) { say "* Please fill in the NorwegianPatronDBUsername and NorwegianPatronDBPassword system preferences." };
exit 0;
}
unless ( $run ) {
say "* You have not specified --run, no real syncing will be done.";
}
=head2 Find patrons that need to be synced
=cut
my @needs_sync = Koha::Database->new->schema->resultset('BorrowerSync')->search({
-and => [
sync => 1,
synctype => 'norwegianpatrondb',
-or => [
syncstatus => 'edited',
syncstatus => 'new',
syncstatus => 'delete',
],
],
});
=head2 Do the actual sync
=cut
my $sync_success = 0;
my $sync_failed = 0;
foreach my $borrower ( @needs_sync ) {
my $cardnumber = $borrower->borrowernumber->cardnumber;
my $firstname = $borrower->borrowernumber->firstname;
my $surname = $borrower->borrowernumber->surname;
my $syncstatus = $borrower->syncstatus;
say "*** Syncing patron: $cardnumber - $firstname $surname ($syncstatus)" if $verbose;
if ( $run ) {
my $response = NLSync({ 'patron' => $borrower->borrowernumber });
if ( $response ) {
my $result = $response->result;
if ( $result->{'status'} && $result->{'status'} == 1 ) {
$sync_success++;
} else {
$sync_failed++;
}
if ( $result->{'melding'} && $verbose ) {
say $result->{'melding'};
}
}
}
}
=head2 Summarize if verbose mode is enabled
=cut
if ( $verbose ) {
say "-----------------------------";
say "Sync succeeded: $sync_success";
say "Sync failed : $sync_failed";
}
=head1 OPTIONS
=over 4
=item B<-r, --run>
Actually carry out syncing operations. Without this option, the script will
only report what it would have done, but not change any data, locally or
remotely.
=item B<-v --verbose>
Report on the progress of the script.
=item B<-d --debug>
Even more output.
=item B<-h, -?, --help>
Prints this help message and exits.
=back
=cut
sub get_options {
# Options
my $run = '',
my $verbose = '';
my $debug = '';
my $help = '';
GetOptions (
'r|run' => \$run,
'v|verbose' => \$verbose,
'd|debug' => \$debug,
'h|?|help' => \$help
);
pod2usage( -exitval => 0 ) if $help;
return ( $run, $verbose, $debug );
}
=head1 AUTHOR
Magnus Enger <digitalutvikling@gmail.com>
=head1 COPYRIGHT
Copyright 2014 Oslo Public Library
=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.
You should have received a copy of the GNU General Public License along with
Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
Fifth Floor, Boston, MA 02110-1301 USA.
=cut