Bug 23805: (RM follow-up) Remove CHECK constraints
[koha.git] / misc / add_date_fields_to_marc_records.pl
blobb7ab77c2cb61d7322e8c902a665537726a5e99cd
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 BEGIN {
21 use FindBin;
22 eval { require "$FindBin::Bin/../kohalib.pl" };
25 use Koha::Script;
27 use Getopt::Long;
28 use Pod::Usage;
29 use MARC::Field;
31 use C4::Biblio;
32 use Koha::DateUtils qw( dt_from_string );
34 my ( $verbose, $help, $confirm, $where, @fields, $unless_exists_field );
35 my $dbh = C4::Context->dbh;
37 GetOptions(
38 'help|h' => \$help,
39 'verbose|v' => \$verbose,
40 'confirm|c' => \$confirm,
41 'where=s' => \$where,
42 'field=s@' => \@fields,
43 'unless-exists=s' => \$unless_exists_field,
44 ) || podusage(1);
46 pod2usage(1) if $help;
47 pod2usage("Parameter field is mandatory") unless @fields;
49 my @fields_to_add;
50 my $dt = dt_from_string; # Could be an option of the script
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) );
58 say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
59 if ($verbose) {
60 say "The following MARC fields will be added:";
61 say "\t" . $_->as_formatted for @fields_to_add;
64 $where = $where ? "WHERE $where" : '';
65 my $sth =
66 $dbh->prepare("SELECT biblionumber, frameworkcode FROM biblio $where");
67 $sth->execute();
69 while ( my ( $biblionumber, $frameworkcode ) = $sth->fetchrow_array ) {
70 my $marc_record =
71 C4::Biblio::GetMarcBiblio( { biblionumber => $biblionumber } );
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);
77 $marc_record->append_fields(@fields_to_add);
78 if ($confirm) {
79 my $modified =
80 C4::Biblio::ModBiblio( $marc_record, $biblionumber, $frameworkcode );
81 say "Bibliographic record $biblionumber has been modified"
82 if $verbose and $modified;
84 elsif ($verbose) {
85 say "Bibliographic record $biblionumber would have been modified";
89 =head1 NAME
91 add_date_fields_to_marc_records.pl
93 =head1 SYNOPSIS
95 perl add_date_fields_to_marc_records.pl --help
97 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
99 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
101 =head1 DESCRIPTION
103 Add some MARC fields to bibliographic records.
105 The replacement tokens are the ones used by strftime.
107 =head1 OPTIONS
109 =over 8
111 =item B<--help>
113 Prints this help
115 =item B<--verbose>
117 Verbose mode.
119 =item B<--confirm>
121 Confirmation flag, the script will be running in dry-run mode if set not.
123 =item B<--where>
125 Limits the search on bibliographic records with a user-specified WHERE clause.
127 Only the columns from the biblio table are available.
129 =item B<--field>
131 Fields to add to the bibliographic records.
133 Must be formatted as 'tag' $ 'subfield' = 'value'
135 For instance:
137 905$a=0/%Y will add a new field 905$a with the value '0/2019' (if run in 2019)
139 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
141 =item B<--unless-exists>
143 Will only create the new fields if this field does not exist.
145 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.
146 If --unless-exists is not passed, a new 905$a will be created in any case.
149 =back
151 =cut