6368c36c58de2b7f20ec284917df1f86f85e1a14
[koha-equinox.git] / misc / add_date_fields_to_marc_records.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 BEGIN {
21     use FindBin;
22     eval { require "$FindBin::Bin/../kohalib.pl" };
23 }
24
25 use Getopt::Long;
26 use Pod::Usage;
27 use MARC::Field;
28
29 use C4::Biblio;
30 use Koha::DateUtils qw( dt_from_string );
31
32 my ( $verbose, $help, $confirm, $where, @fields, $unless_exists_field );
33 my $dbh = C4::Context->dbh;
34
35 GetOptions(
36     'help|h'    => \$help,
37     'verbose|v' => \$verbose,
38     'confirm|c' => \$confirm,
39     'where=s'   => \$where,
40     'field=s@'  => \@fields,
41     'unless-exists=s' => \$unless_exists_field,
42 ) || podusage(1);
43
44 pod2usage(0) if $help;
45
46 my @fields_to_add;
47 my $dt = dt_from_string;    # Could be an option of the script
48 for my $field (@fields) {
49     my ( $f_sf, $value )    = split '=',  $field;
50     my ( $tag,  $subfield ) = split '\$', $f_sf;
51     push @fields_to_add,
52       MARC::Field->new( $tag, '', '', $subfield => $dt->strftime($value) );
53 }
54
55 say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
56 if ($verbose) {
57     say "The following MARC fields will be added:";
58     say "\t" . $_->as_formatted for @fields_to_add;
59 }
60
61 $where ||= "";
62 my $sth =
63   $dbh->prepare("SELECT biblionumber, frameworkcode FROM biblio $where");
64 $sth->execute();
65
66 while ( my ( $biblionumber, $frameworkcode ) = $sth->fetchrow_array ) {
67     my $marc_record =
68       C4::Biblio::GetMarcBiblio( { biblionumber => $biblionumber } );
69     next unless $marc_record;
70     if ( $unless_exists_field ) {
71         my ( $tag,  $subfield ) = split '\$', $unless_exists_field;
72         next if $marc_record->subfield($tag, $subfield);
73     }
74     $marc_record->append_fields(@fields_to_add);
75     if ($confirm) {
76         my $modified =
77           C4::Biblio::ModBiblio( $marc_record, $biblionumber, $frameworkcode );
78         say "Bibliographic record $biblionumber has been modified"
79           if $verbose and $modified;
80     }
81     elsif ($verbose) {
82         say "Bibliographic record $biblionumber would have been modified";
83     }
84 }
85
86 =head1 NAME
87
88 add_date_fields_to_marc_records.pl
89
90 =head1 SYNOPSIS
91
92   perl add_date_fields_to_marc_records.pl --help
93
94   perl add_date_fields_to_marc_records.pl --field='905$a=0/%Y' --field='905$a=1/%Y/%b-%m' --field='905$a=2/%Y/%b-%m/%d' --unless-exists='905$a' --verbose --confirm
95
96 =head1 DESCRIPTION
97
98 Add some MARC fields to bibliographic records.
99
100 The replacement tokens are the ones used by strftime.
101
102 =over 8
103
104 =item B<--help>
105
106 Prints this help
107
108 =item B<--verbose>
109
110 Verbose mode.
111
112 =item B<--confirm>
113
114 Confirmation flag, the script will be running in dry-run mode if set not.
115 =item B<--where>
116
117 Limits the search on bibliographic records with a user-specified WHERE clause.
118
119 =item B<--field>
120
121 Fields to add to the bibliographic records.
122
123 Must be formatted as 'tag' $ 'subfield' = 'value'
124
125 For instance:
126
127 905$a=0/%Y will add a new field 905$a with the value '0/2019' (if run in 2019)
128
129 905$a=2/%Y/%b-%m/%d'will a a new field 905$a with the value '2/2019/Mar-03/13' if run on March 13th 2019
130
131 =item B<--unless-exists>
132
133 Will only create the new fields if this field does not exist
134
135 =back
136
137 =cut