Bug 18309: Add UNIMARC field 214 and its subfields
[koha.git] / misc / maintenance / update_authorities.pl
bloba854f5acdb4cb88b5a63b6faf7aef73dea3cc91e
1 #!/usr/bin/perl
3 # Copyright Rijksmuseum 2017
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Modern::Perl;
22 use Getopt::Long;
23 use List::MoreUtils qw/uniq/;
24 use Pod::Usage;
26 use Koha::Script;
27 use C4::AuthoritiesMarc qw/AddAuthority DelAuthority GetAuthority merge/;
29 my ( @authid, $confirm, $delete, $help, $merge, $reference, $renumber, $verbose );
30 GetOptions(
31 'authid:s' => \@authid,
32 'confirm' => \$confirm,
33 'delete' => \$delete,
34 'help' => \$help,
35 'merge' => \$merge,
36 'reference:i' => \$reference,
37 'renumber' => \$renumber,
38 'verbose' => \$verbose,
41 @authid = map { split /[,]/, $_; } @authid;
42 print "No changes will be made\n" unless $confirm;
43 pod2usage(1) if $help;
45 if ( $delete and $merge and $renumber ) {
46 pod2usage(q|Only one action parameter can be passed (delete, merge or renumber)|);
49 if( $delete ) {
50 delete_auth( \@authid );
51 } elsif( $merge ) {
52 pod2usage(q|Reference parameter is missing|) unless $reference;
53 merge_auth( \@authid, $reference );
54 } elsif( $renumber ) {
55 renumber( \@authid );
56 } else {
57 pod2usage(1);
60 sub delete_auth {
61 my ( $auths ) = @_;
62 foreach my $authid ( uniq(@$auths) ) {
63 if( $confirm ) {
64 DelAuthority({ authid => $authid }); # triggers a merge (read: cleanup)
65 print "Removing $authid\n" if $verbose;
66 } else {
67 print "Would have removed $authid\n" if $verbose;
72 sub merge_auth {
73 my ( $auths, $reference ) = @_;
75 return unless $reference;
77 my $marc_ref = GetAuthority( $reference ) || die "Reference record $reference not found\n";
78 # First update all linked biblios of reference
79 merge({ mergefrom => $reference, MARCfrom => $marc_ref, mergeto => $reference, MARCto => $marc_ref, override_limit => 1 }) if $confirm;
81 # Merge all authid's into reference
82 my $marc;
83 foreach my $authid ( uniq(@$auths) ) {
84 next if $authid == $reference;
85 $marc = GetAuthority($authid);
86 if( !$marc ) {
87 print "Authority id $authid ignored, does not exist.\n";
88 next;
90 if( $confirm ) {
91 merge({
92 mergefrom => $authid,
93 MARCfrom => $marc,
94 mergeto => $reference,
95 MARCto => $marc_ref,
96 override_limit => 1
97 });
98 DelAuthority({ authid => $authid, skip_merge => 1 });
99 print "Record $authid merged into reference $reference.\n" if $verbose;
100 } else {
101 print "Would have merged record $authid into reference $reference.\n" if $verbose;
106 sub renumber {
107 my ( $auths ) = @_;
108 foreach my $authid ( uniq(@$auths) ) {
109 if( my $authority = Koha::Authorities->find($authid) ) {
110 my $marc = GetAuthority( $authid );
111 if( $confirm ) {
112 AddAuthority( $marc, $authid, $authority->authtypecode );
113 # AddAuthority contains an update of 001, 005 etc.
114 print "Renumbered $authid\n" if $verbose;
115 } else {
116 print "Would have renumbered $authid\n" if $verbose;
118 } else {
119 print "Record $authid not found!\n" if $verbose;
124 =head1 NAME
126 update_authorities.pl
128 =head1 DESCRIPTION
130 Script to perform various authority related maintenance tasks.
131 This version supports deleting an authority record and updating all linked
132 biblio records.
133 Furthermore it supports merging authority records with one reference record,
134 and updating all linked biblio records.
135 It also allows you to force a renumber, i.e. save the authid into field 001.
137 =head1 SYNOPSIS
139 update_authorities.pl -c -authid 1,2,3 -delete
141 update_authorities.pl -c -authid 1 -authid 2 -authid 3 -delete
143 update_authorities.pl -c -authid 1,2 -merge -reference 3
145 update_authorities.pl -c -merge -reference 4
147 update_authorities.pl -c -authid 1,2,3 -renumber
149 =head1 OPTIONS
151 authid: List authority numbers separated by commas or repeat the
152 parameter.
154 confirm: Needed to commit changes.
156 delete: Delete the listed authority numbers and remove its references from
157 linked biblio records.
159 merge: Merge the passed authid's into reference and update all linked biblio
160 records. If you do not pass authid's, the linked biblio records of reference
161 will be updated only.
163 renumber: Save authid into field 001.
165 =head1 AUTHOR
167 Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
169 =cut