From 4bdd586834001aaceb30458731f454b84ed2de5d Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Tue, 3 Mar 2020 13:58:35 +0000 Subject: [PATCH] Bug 24544: Add script to insert persistent identifiers Content-Type: text/plain; charset=utf-8 Test plan: See patch with example plugins. This script can only be tested fully when you enable a plugin to generate new identifiers. The next patch provides samples to do that. Signed-off-by: Marcel de Rooy --- misc/cronjobs/insert_persistent_id.pl | 175 ++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100755 misc/cronjobs/insert_persistent_id.pl diff --git a/misc/cronjobs/insert_persistent_id.pl b/misc/cronjobs/insert_persistent_id.pl new file mode 100755 index 0000000000..b39f642506 --- /dev/null +++ b/misc/cronjobs/insert_persistent_id.pl @@ -0,0 +1,175 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Copyright (C) 2020 Rijksmuseum +# +# 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 . + +=head1 NAME + +insert_persistent_id.pl - cron script to insert persistent identifiers for authority, biblio and item records + +=cut + +use Modern::Perl; +use Data::Dumper; +use Getopt::Long; +use Pod::Usage; + +use C4::Context; +use C4::Log; +use Koha::Authorities; +use Koha::Biblios; +use Koha::Exceptions; +use Koha::Items; +use Koha::PID::Controller; +use Koha::Script -cron; + +my $params = {}; +GetOptions( + 'help|?|h' => \$params->{help}, + 'manual|m' => \$params->{manual}, + 'verbose|v' => \$params->{verbose}, + + 'confirm|c' => \$params->{confirm}, + 'all|a' => \$params->{all}, # otherwise script is incremental + + 'hours:i' => \$params->{hours}, + 'records:i' => \$params->{records}, + 'updates:i' => \$params->{updates}, +) or pod2usage(2); + +pod2usage( -verbose => 2 ) if $params->{manual}; +pod2usage( -verbose => 1 ) if $params->{help} || !$params->{confirm}; + +main($params); + +sub main { # main loop + my $params = shift; + my $loopcontrol = { + records => 0, updates => 0, time => time() + ($params->{hours}//0)*3600, + }; + my $ctrl = Koha::PID::Controller->new; + my $last_run = get_last_run($params); # { auth => X, biblio => Y, item => Z } + foreach my $category ( 'auth', 'biblio', 'item' ) { + next if C4::Context->preference('PID_Field') !~ /$category/; + + my $rs = get_record_set( $category, $last_run->{$category} ); + while( my $object = $rs->next ) { + # Check limits + last if defined $params->{updates} && $loopcontrol->{updates} >= $params->{updates}; + last if defined $params->{records} && $loopcontrol->{records} >= $params->{records}; + last if defined $params->{hours} && time() >= $loopcontrol->{time}; + + my $pid = $ctrl->get({ category => $category, object => $object }); + my $result; + if( !$pid ) { + $result = $ctrl->create({ category => $category, kohaIdentifier => $object->id, object => $object }); + if( $result->{success} ) { + $pid = $result->{uri}; + $result = $ctrl->insert({ pid => $pid, category => $category, kohaIdentifier => $object->id, object => $object }); + $loopcontrol->{updates}++ if $result->{success}; + warn "Did not insert $pid for ". $object->id if !$result->{success} && $params->{verbose}; + } else { + warn "No pid created for ". $object->id if $params->{verbose}; + } + } + $loopcontrol->{records}++; + $last_run->{$category} = $object->id; + } + } + print Dumper( $loopcontrol ) if $params->{verbose}; + save_last_run( $last_run ); +} + +sub get_last_run { +# Look for last logaction line from this script (incremental) + my $params = shift; + + return { auth => 0, biblio => 0, item => 0 } if $params->{all}; # full 'scan' + + my $rs = Koha::ActionLogs->search({ + module => 'CRONJOBS', action => 'RUN', info => { -like => '%insert_persistent_id.pl%' } + }, { + order_by => { -desc => [ 'action_id' ] }, + }); + if( my $rec = $rs->next ) { + my $str = $rec->info; + if( $str =~ /:auth,(\d+),biblio,(\d+),item,(\d+)$/ ) { + return { auth => $1, biblio => $2, item => $3 }; + } + } + return { auth => 0, biblio => 0, item => 0 }; +} + +sub get_record_set { + my $category = shift // q{}; + my $last_id = shift // 0; + if( $category eq 'item' ) { + return Koha::Items->search({ itemnumber => { '>', $last_id } }); + } elsif( $category eq 'biblio' ) { + return Koha::Biblios->search({ biblionumber => { '>', $last_id } }); + } else { # auth + return Koha::Authorities->search({ authid => { '>', $last_id } }); + } +} + +sub save_last_run { + my ( $last_run ) = @_; + my $str = join ',', map { ( $_, $last_run->{$_} ) } sort keys %$last_run; + # no cronlogaction(); Why not? We need logaction to override CronjobLog pref + logaction( 'CRONJOBS', 'RUN', undef, (caller(0))[1]. ":". $str, 'COMMANDLINE' ); +} + +=head1 SYNOPSIS + +misc/cronjobs/insert_persistent_id.pl -c -v + +=head1 DESCRIPTION + +This script adds persistent identifiers to authority, biblio and item records. +Note that preference PID_Field controls which records are modified. + +Normally, it operates in incremental mode. It continues where the previous +run left off. That information is found in the action logs. A few options +allow you to control the number of hours or records for processing. + +The module Koha::PID::Controller contains the logic for retrieving, creating +and inserting persistent identifiers. Its documentation tells you more about +the use of the various plugin hooks involved. + +=head1 OPTIONS + +-a (All) Full update in contrast to default incremental update + +-c Commit flag + +-help Usage statement + +-hours N Stop execution after N hours + +-manual Show documentation + +-records N Stop execution after reading N records + +-updates N Stop execution after updating N records + +-verbose Enable verbose mode + +=head1 AUTHOR + +Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands + +=cut -- 2.11.0