Bug 23924: Add --date-field to add_date_fields_to_marc_records.pl
[koha.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 use Koha::Script;
21
22 use Getopt::Long qw( GetOptions );
23 use Pod::Usage qw( pod2usage );
24 use MARC::Field;
25
26 use C4::Biblio;
27 use Koha::Biblios;
28 use Koha::DateUtils qw( dt_from_string );
29
30 my ( $verbose, $help, $confirm, $where, @fields, $date_field, $unless_exists_field );
31 my $dbh = C4::Context->dbh;
32
33 GetOptions(
34     'help|h'    => \$help,
35     'verbose|v' => \$verbose,
36     'confirm|c' => \$confirm,
37     'where=s'   => \$where,
38     'field=s@'  => \@fields,
39     'date-field=s' => \$date_field,
40     'unless-exists=s' => \$unless_exists_field,
41 ) || podusage(1);
42
43 pod2usage(1) if $help;
44 pod2usage("Parameter field is mandatory") unless @fields;
45
46 say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
47
48 my @fields_to_add;
49 unless ( $date_field ) {
50     my $dt = dt_from_string;
51     for my $field (@fields) {
52         my ( $f_sf, $value )    = split '=',  $field;
53         my ( $tag,  $subfield ) = split '\$', $f_sf;
54         push @fields_to_add,
55           MARC::Field->new( $tag, '', '', $subfield => $dt->strftime($value) );
56     }
57
58     if ($verbose) {
59         say "The following MARC fields will be added:";
60         say "\t" . $_->as_formatted for @fields_to_add;
61     }
62 }
63
64 $where = $where ? "WHERE $where" : '';
65 my $sth =
66   $dbh->prepare("SELECT biblionumber, frameworkcode FROM biblio $where");
67 $sth->execute();
68
69 while ( my ( $biblionumber, $frameworkcode ) = $sth->fetchrow_array ) {
70     my $biblio = Koha::Biblios->find($biblionumber);
71     my $marc_record = $biblio->metadata->record;
72     next unless $marc_record;
73     if ( $unless_exists_field ) {
74         my ( $tag,  $subfield ) = split '\$', $unless_exists_field;
75         next if $marc_record->subfield($tag, $subfield);
76     }
77     if ( $date_field ) {
78         my ( $tag,  $subfield ) = split '\$', $date_field;
79         my $date = $marc_record->subfield($tag, $subfield);
80         next unless $date;
81         warn $date;
82         my $dt = dt_from_string($date);
83         for my $field (@fields) {
84             my ( $f_sf, $value )    = split '=',  $field;
85             my ( $tag,  $subfield ) = split '\$', $f_sf;
86             push @fields_to_add,
87               MARC::Field->new( $tag, '', '', $subfield => $dt->strftime($value) );
88         }
89         if ($verbose) {
90             say sprintf "The following MARC fields will be added to record %s:", $biblionumber;
91             say "\t" . $_->as_formatted for @fields_to_add;
92         }
93     }
94
95     $marc_record->append_fields(@fields_to_add);
96
97     if ($confirm) {
98         my $modified =
99           C4::Biblio::ModBiblio( $marc_record, $biblionumber, $frameworkcode );
100         say "Bibliographic record $biblionumber has been modified"
101           if $verbose and $modified;
102     }
103     elsif ($verbose) {
104         say "Bibliographic record $biblionumber would have been modified";
105     }
106 }
107
108 =head1 NAME
109
110 add_date_fields_to_marc_records.pl
111
112 =head1 SYNOPSIS
113
114   perl add_date_fields_to_marc_records.pl --help
115
116   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
117
118   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' --where "biblionumber=42" --verbose --confirm
119
120   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' --date-field='908$a' --verbose --confirm
121
122 =head1 DESCRIPTION
123
124 Add some MARC fields to bibliographic records.
125
126 The replacement tokens are the ones used by strftime.
127
128 =head1 OPTIONS
129
130 =over 8
131
132 =item B<--help>
133
134 Prints this help
135
136 =item B<--verbose>
137
138 Verbose mode.
139
140 =item B<--confirm>
141
142 Confirmation flag, the script will be running in dry-run mode if set not.
143
144 =item B<--where>
145
146 Limits the search on bibliographic records with a user-specified WHERE clause.
147
148 Only the columns from the biblio table are available.
149
150 =item B<--field>
151
152 Fields to add to the bibliographic records.
153
154 Must be formatted as 'tag' $ 'subfield' = 'value'
155
156 For instance:
157
158 905$a=0/%Y will add a new field 905$a with the value '0/2019' (if run in 2019)
159
160 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
161
162 =item B<--unless-exists>
163
164 Will only create the new fields if this field does not exist.
165
166 For instance, if --field='905$a=0/%Y' and --unless-exists='905$a' are provided, a 905$a will be created unless there is already one.
167 If --unless-exists is not passed, a new 905$a will be created in any case.
168
169 =item B<--date-field>
170
171 The date will be picked from a specific marc field.
172
173 For instance, if the record contains a date formatted YYYY-MM-DD in 908$a, you can pass --date-field='908$a'
174 Note that the date format used will be the one defined in the syspref 'dateformat', or iso format.
175
176 =back
177
178 =cut