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>.
22 use Getopt::Long qw( GetOptions );
23 use Pod::Usage qw( pod2usage );
28 use Koha::DateUtils qw( dt_from_string );
30 my ( $verbose, $help, $confirm, $where, @fields, $unless_exists_field );
31 my $dbh = C4::Context->dbh;
35 'verbose|v' => \$verbose,
36 'confirm|c' => \$confirm,
38 'field=s@' => \@fields,
39 'unless-exists=s' => \$unless_exists_field,
42 pod2usage(1) if $help;
43 pod2usage("Parameter field is mandatory") unless @fields;
46 my $dt = dt_from_string; # Could be an option of the script
47 for my $field (@fields) {
48 my ( $f_sf, $value ) = split '=', $field;
49 my ( $tag, $subfield ) = split '\$', $f_sf;
51 MARC::Field->new( $tag, '', '', $subfield => $dt->strftime($value) );
54 say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
56 say "The following MARC fields will be added:";
57 say "\t" . $_->as_formatted for @fields_to_add;
60 $where = $where ? "WHERE $where" : '';
62 $dbh->prepare("SELECT biblionumber, frameworkcode FROM biblio $where");
65 while ( my ( $biblionumber, $frameworkcode ) = $sth->fetchrow_array ) {
66 my $biblio = Koha::Biblios->find($biblionumber);
67 my $marc_record = $biblio->metadata->record;
68 next unless $marc_record;
69 if ( $unless_exists_field ) {
70 my ( $tag, $subfield ) = split '\$', $unless_exists_field;
71 next if $marc_record->subfield($tag, $subfield);
73 $marc_record->append_fields(@fields_to_add);
76 C4::Biblio::ModBiblio( $marc_record, $biblionumber, $frameworkcode );
77 say "Bibliographic record $biblionumber has been modified"
78 if $verbose and $modified;
81 say "Bibliographic record $biblionumber would have been modified";
87 add_date_fields_to_marc_records.pl
91 perl add_date_fields_to_marc_records.pl --help
93 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 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
99 Add some MARC fields to bibliographic records.
101 The replacement tokens are the ones used by strftime.
117 Confirmation flag, the script will be running in dry-run mode if set not.
121 Limits the search on bibliographic records with a user-specified WHERE clause.
123 Only the columns from the biblio table are available.
127 Fields to add to the bibliographic records.
129 Must be formatted as 'tag' $ 'subfield' = 'value'
133 905$a=0/%Y will add a new field 905$a with the value '0/2019' (if run in 2019)
135 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
137 =item B<--unless-exists>
139 Will only create the new fields if this field does not exist.
141 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.
142 If --unless-exists is not passed, a new 905$a will be created in any case.