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 biblio records and items in MARC 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::Items; |
36 |
use Koha::PID::Controller; |
37 |
use Koha::Script -cron; |
38 |
|
39 |
my $params = {}; |
40 |
GetOptions( |
41 |
'help|?|h' => \$params->{help}, |
42 |
'manual|m' => \$params->{manual}, |
43 |
'confirm|c' => \$params->{confirm}, |
44 |
'verbose|v' => \$params->{verbose}, |
45 |
'category:s' => \$params->{category}, |
46 |
'updates:i' => \$params->{updates}, |
47 |
'records:i' => \$params->{records}, |
48 |
'hours:i' => \$params->{hours}, |
49 |
) or pod2usage(2); |
50 |
pod2usage( -verbose => 2 ) if $params->{manual}; |
51 |
pod2usage(1) if $params->{help} || !$params->{confirm}; |
52 |
|
53 |
cronlogaction(); |
54 |
|
55 |
# main loop |
56 |
my $loopcontrol = { |
57 |
read => 0, update => 0, time => time() + ($params->{hours}//0)*3600, |
58 |
}; |
59 |
my $rs = get_record_set($params); |
60 |
my $ctrl = Koha::PID::Controller->new; |
61 |
my $pid; |
62 |
while( my $obj = $rs->next ) { |
63 |
$pid = $ctrl->get({ category => $params->{category}, object => $obj }); |
64 |
my $result; |
65 |
if( !$pid ) { |
66 |
$pid = $ctrl->generate({ category => $params->{category}, kohaIdentifier => $obj->id, object => $obj }); |
67 |
$result = $ctrl->insert({ pid => $pid, category => $params->{category}, kohaIdentifier => $obj->id, object => $obj }); |
68 |
$loopcontrol->{update}++; |
69 |
} |
70 |
print Dumper( $pid, $result->{success}, $obj->id ); |
71 |
$loopcontrol->{read}++; |
72 |
last if $params->{updates} && $loopcontrol->{update} > $params->{updates}; |
73 |
last if $params->{records} && $loopcontrol->{read} > $params->{records}; |
74 |
last if $params->{hours} && $loopcontrol->{time} > time(); |
75 |
} |
76 |
print Dumper( $loopcontrol ); |
77 |
|
78 |
|
79 |
sub get_record_set { |
80 |
my $params = shift; |
81 |
my $category = $params->{category}; |
82 |
if( $category eq 'item' ) { |
83 |
return Koha::Items->search; |
84 |
} elsif( $category eq 'biblio' ) { |
85 |
return Koha::Biblios->search; |
86 |
} elsif( $category eq 'PERSO_NAME' || $category eq 'CORPO_NAME' || $category eq 'GEOGR_NAME' || $category eq 'MEETI_NAME' || $category eq 'TOPIC_TERM' ) { |
87 |
return Koha::Authorities->search({ authtypecode => $category }); |
88 |
} |
89 |
} |
90 |
|
91 |
=head1 SYNOPSIS |
92 |
|
93 |
./insert_persistent_id.pl -c |
94 |
|
95 |
=head1 DESCRIPTION |
96 |
|
97 |
This script adds persistent identifiers to authority, biblio and item records. |
98 |
|
99 |
=head1 OPTIONS |
100 |
|
101 |
=cut |