Bug 30147: Fix modules usage in opac-detail.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::DateUtils qw( dt_from_string );
28
29 my ( $verbose, $help, $confirm, $where, @fields, $unless_exists_field );
30 my $dbh = C4::Context->dbh;
31
32 GetOptions(
33     'help|h'    => \$help,
34     'verbose|v' => \$verbose,
35     'confirm|c' => \$confirm,
36     'where=s'   => \$where,
37     'field=s@'  => \@fields,
38     'unless-exists=s' => \$unless_exists_field,
39 ) || podusage(1);
40
41 pod2usage(1) if $help;
42 pod2usage("Parameter field is mandatory") unless @fields;
43
44 my @fields_to_add;
45 my $dt = dt_from_string;    # Could be an option of the script
46 for my $field (@fields) {
47     my ( $f_sf, $value )    = split '=',  $field;
48     my ( $tag,  $subfield ) = split '\$', $f_sf;
49     push @fields_to_add,
50       MARC::Field->new( $tag, '', '', $subfield => $dt->strftime($value) );
51 }
52
53 say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
54 if ($verbose) {
55     say "The following MARC fields will be added:";
56     say "\t" . $_->as_formatted for @fields_to_add;
57 }
58
59 $where = $where ? "WHERE $where" : '';
60 my $sth =
61   $dbh->prepare("SELECT biblionumber, frameworkcode FROM biblio $where");
62 $sth->execute();
63
64 while ( my ( $biblionumber, $frameworkcode ) = $sth->fetchrow_array ) {
65     my $marc_record =
66       C4::Biblio::GetMarcBiblio( { biblionumber => $biblionumber } );
67     next unless $marc_record;
68     if ( $unless_exists_field ) {
69         my ( $tag,  $subfield ) = split '\$', $unless_exists_field;
70         next if $marc_record->subfield($tag, $subfield);
71     }
72     $marc_record->append_fields(@fields_to_add);
73     if ($confirm) {
74         my $modified =
75           C4::Biblio::ModBiblio( $marc_record, $biblionumber, $frameworkcode );
76         say "Bibliographic record $biblionumber has been modified"
77           if $verbose and $modified;
78     }
79     elsif ($verbose) {
80         say "Bibliographic record $biblionumber would have been modified";
81     }
82 }
83
84 =head1 NAME
85
86 add_date_fields_to_marc_records.pl
87
88 =head1 SYNOPSIS
89
90   perl add_date_fields_to_marc_records.pl --help
91
92   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
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' --where "biblionumber=42" --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 =head1 OPTIONS
103
104 =over 8
105
106 =item B<--help>
107
108 Prints this help
109
110 =item B<--verbose>
111
112 Verbose mode.
113
114 =item B<--confirm>
115
116 Confirmation flag, the script will be running in dry-run mode if set not.
117
118 =item B<--where>
119
120 Limits the search on bibliographic records with a user-specified WHERE clause.
121
122 Only the columns from the biblio table are available.
123
124 =item B<--field>
125
126 Fields to add to the bibliographic records.
127
128 Must be formatted as 'tag' $ 'subfield' = 'value'
129
130 For instance:
131
132 905$a=0/%Y will add a new field 905$a with the value '0/2019' (if run in 2019)
133
134 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
135
136 =item B<--unless-exists>
137
138 Will only create the new fields if this field does not exist.
139
140 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.
141 If --unless-exists is not passed, a new 905$a will be created in any case.
142
143
144 =back
145
146 =cut