View | Details | Raw Unified | Return to bug 24544
Collapse All | Expand All

(-)a/misc/cronjobs/insert_persistent_id.pl (-1 / +175 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright (C) 2020 Rijksmuseum
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
=head1 NAME
21
22
insert_persistent_id.pl - cron script to insert persistent identifiers for authority, biblio and item records
23
24
=cut
25
26
use Modern::Perl;
27
use Data::Dumper;
28
use Getopt::Long;
29
use Pod::Usage;
30
31
use C4::Context;
32
use C4::Log;
33
use Koha::Authorities;
34
use Koha::Biblios;
35
use Koha::Exceptions;
36
use Koha::Items;
37
use Koha::PID::Controller;
38
use Koha::Script -cron;
39
40
my $params = {};
41
GetOptions(
42
    'help|?|h'   => \$params->{help},
43
    'manual|m'   => \$params->{manual},
44
    'verbose|v'  => \$params->{verbose},
45
46
    'confirm|c'  => \$params->{confirm},
47
    'all|a'      => \$params->{all}, # otherwise script is incremental
48
49
    'hours:i'    => \$params->{hours},
50
    'records:i'  => \$params->{records},
51
    'updates:i'  => \$params->{updates},
52
) or pod2usage(2);
53
54
pod2usage( -verbose => 2 ) if $params->{manual};
55
pod2usage( -verbose => 1 ) if $params->{help} || !$params->{confirm};
56
57
main($params);
58
59
sub main { # main loop
60
    my $params = shift;
61
    my $loopcontrol = {
62
        records => 0, updates => 0, time => time() + ($params->{hours}//0)*3600,
63
    };
64
    my $ctrl = Koha::PID::Controller->new;
65
    my $last_run = get_last_run($params); # { auth => X, biblio => Y, item => Z }
66
    foreach my $category ( 'auth', 'biblio', 'item' ) {
67
        next if C4::Context->preference('PID_Field') !~ /$category/;
68
69
        my $rs = get_record_set( $category, $last_run->{$category} );
70
        while( my $object = $rs->next ) {
71
            # Check limits
72
            last if defined $params->{updates} && $loopcontrol->{updates} >= $params->{updates};
73
            last if defined $params->{records} && $loopcontrol->{records} >= $params->{records};
74
            last if defined $params->{hours} && time() >= $loopcontrol->{time};
75
76
            my $pid = $ctrl->get({ category => $category, object => $object });
77
            my $result;
78
            if( !$pid ) {
79
                $result = $ctrl->create({ category  => $category, kohaIdentifier => $object->id, object => $object });
80
                if( $result->{success} ) {
81
                    $pid = $result->{uri};
82
                    $result = $ctrl->insert({ pid => $pid, category => $category, kohaIdentifier => $object->id, object => $object });
83
                    $loopcontrol->{updates}++ if $result->{success};
84
                    warn "Did not insert $pid for ". $object->id if !$result->{success} && $params->{verbose};
85
                } else {
86
                    warn "No pid created for ". $object->id if $params->{verbose};
87
                }
88
            }
89
            $loopcontrol->{records}++;
90
            $last_run->{$category} = $object->id;
91
        }
92
    }
93
    print Dumper( $loopcontrol ) if $params->{verbose};
94
    save_last_run( $last_run );
95
}
96
97
sub get_last_run {
98
# Look for last logaction line from this script (incremental)
99
    my $params = shift;
100
101
    return { auth => 0, biblio => 0, item => 0 } if $params->{all}; # full 'scan'
102
103
    my $rs = Koha::ActionLogs->search({
104
        module => 'CRONJOBS', action => 'RUN', info => { -like => '%insert_persistent_id.pl%' }
105
    }, {
106
        order_by => { -desc => [ 'action_id' ] },
107
    });
108
    if( my $rec = $rs->next ) {
109
        my $str = $rec->info;
110
        if( $str =~ /:auth,(\d+),biblio,(\d+),item,(\d+)$/ ) {
111
            return { auth => $1, biblio => $2, item => $3 };
112
        }
113
    }
114
    return { auth => 0, biblio => 0, item => 0 };
115
}
116
117
sub get_record_set {
118
    my $category = shift // q{};
119
    my $last_id = shift // 0;
120
    if( $category eq 'item' ) {
121
        return Koha::Items->search({ itemnumber => { '>', $last_id } });
122
    } elsif( $category eq 'biblio' ) {
123
        return Koha::Biblios->search({ biblionumber => { '>', $last_id } });
124
    } else { # auth
125
        return Koha::Authorities->search({ authid => { '>', $last_id } });
126
    }
127
}
128
129
sub save_last_run {
130
    my ( $last_run ) = @_;
131
    my $str = join ',', map { ( $_, $last_run->{$_} ) } sort keys %$last_run;
132
    # no cronlogaction(); Why not? We need logaction to override CronjobLog pref
133
    logaction( 'CRONJOBS', 'RUN', undef, (caller(0))[1]. ":". $str, 'COMMANDLINE' );
134
}
135
136
=head1 SYNOPSIS
137
138
misc/cronjobs/insert_persistent_id.pl -c -v
139
140
=head1 DESCRIPTION
141
142
This script adds persistent identifiers to authority, biblio and item records.
143
Note that preference PID_Field controls which records are modified.
144
145
Normally, it operates in incremental mode. It continues where the previous
146
run left off. That information is found in the action logs. A few options
147
allow you to control the number of hours or records for processing.
148
149
The module Koha::PID::Controller contains the logic for retrieving, creating
150
and inserting persistent identifiers. Its documentation tells you more about
151
the use of the various plugin hooks involved.
152
153
=head1 OPTIONS
154
155
-a (All) Full update in contrast to default incremental update
156
157
-c Commit flag
158
159
-help Usage statement
160
161
-hours N Stop execution after N hours
162
163
-manual Show documentation
164
165
-records N Stop execution after reading N records
166
167
-updates N Stop execution after updating N records
168
169
-verbose Enable verbose mode
170
171
=head1 AUTHOR
172
173
Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
174
175
=cut

Return to bug 24544