From e54e887b58d7daa0309d16923cce4419aee6314b Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 13 Mar 2019 11:23:13 -0300 Subject: [PATCH] Bug 22509: Add new script to add MARC fields with date formatted stringsto bib records Say you want to add, into a new MARC fields, the following string: 2/2019/Mar-03/13 Depending on today's date. Test plan: Use the script after with the different options (see --help option for the list) Sponsored-by: Orex Digital Signed-off-by: Hugo Agud Signed-off-by: Martin Renvoize --- misc/add_date_fields_to_marc_records.pl | 128 ++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100755 misc/add_date_fields_to_marc_records.pl diff --git a/misc/add_date_fields_to_marc_records.pl b/misc/add_date_fields_to_marc_records.pl new file mode 100755 index 0000000000..03d3d66dc2 --- /dev/null +++ b/misc/add_date_fields_to_marc_records.pl @@ -0,0 +1,128 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +BEGIN { + use FindBin; + eval { require "$FindBin::Bin/../kohalib.pl" }; +} + +use Getopt::Long; +use Pod::Usage; +use MARC::Field; + +use C4::Biblio; +use Koha::DateUtils qw( dt_from_string ); + +my ( $verbose, $help, $confirm, $where, @fields ); +my $dbh = C4::Context->dbh; + +GetOptions( + 'help|h' => \$help, + 'verbose|v' => \$verbose, + 'confirm|c' => \$confirm, + 'where=s' => \$where, + 'field=s@' => \@fields, +) || podusage(1); + +pod2usage(0) if $help; + +my @fields_to_add; +my $dt = dt_from_string; # Could be an option of the script +for my $field (@fields) { + my ( $f_sf, $value ) = split '=', $field; + my ( $tag, $subfield ) = split '\$', $f_sf; + push @fields_to_add, + MARC::Field->new( $tag, '', '', $subfield => $dt->strftime($value) ); +} + +say "Confirm flag not passed, running in dry-run mode..."; +if ($verbose) { + say "The following MARC fields will be added:"; + say "\t" . $_->as_formatted for @fields_to_add; +} + +$where ||= ""; +my $sth = + $dbh->prepare("SELECT biblionumber, frameworkcode FROM biblio $where"); +$sth->execute(); + +while ( my ( $biblionumber, $frameworkcode ) = $sth->fetchrow_array ) { + my $marc_record = + C4::Biblio::GetMarcBiblio( { biblionumber => $biblionumber } ); + next unless $marc_record; + $marc_record->append_fields(@fields_to_add); + if ($confirm) { + my $modified = + C4::Biblio::ModBiblio( $marc_record, $biblionumber, $frameworkcode ); + say "Bibliographic record $biblionumber has been modified" + if $verbose and $modified; + } + elsif ($verbose) { + say "Bibliographic record $biblionumber would have been modified"; + } +} + +=head1 NAME + +add_date_fields_to_marc_records.pl + +=head1 SYNOPSIS + + perl add_date_fields_to_marc_records.pl --help + + 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' --verbose --confirm + +=head1 DESCRIPTION + +Add some MARC fields to bibliographic records. + +The replacement tokens are the ones used by strftime. + +=over 8 + +=item B<--help> + +Prints this help + +=item B<--verbose> + +Verbose mode. + +=item B<--confirm> + +Confirmation flag, the script will be running in dry-run mode if set not. +=item B<--where> + +Limits the search on bibliographic records with a user-specified WHERE clause. + +=item B<--field> + +Fields to add to the bibliographic records. + +Must be formatted as 'tag' $ 'subfield' = 'value' + +For instance: + +905$a=0/%Y will add a new field 905$a with the value '0/2019' (if run in 2019) + +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 + +=back + +=cut -- 2.39.5