Bug 29697: Replace GetMarcBiblio occurrences with $biblio->metadata->record
[srvgit] / misc / add_date_fields_to_marc_records.pl
index 9d348d3..40b9fa3 100755 (executable)
 
 use Modern::Perl;
 
-BEGIN {
-    use FindBin;
-    eval { require "$FindBin::Bin/../kohalib.pl" };
-}
+use Koha::Script;
 
-use Getopt::Long;
-use Pod::Usage;
+use Getopt::Long qw( GetOptions );
+use Pod::Usage qw( pod2usage );
 use MARC::Field;
 
 use C4::Biblio;
+use Koha::Biblios;
 use Koha::DateUtils qw( dt_from_string );
 
 my ( $verbose, $help, $confirm, $where, @fields, $unless_exists_field );
@@ -59,14 +57,14 @@ if ($verbose) {
     say "\t" . $_->as_formatted for @fields_to_add;
 }
 
-$where ||= "";
+$where = $where ? "WHERE $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 } );
+    my $biblio = Koha::Biblios->find($biblionumber);
+    my $marc_record = $biblio->metadata->record;
     next unless $marc_record;
     if ( $unless_exists_field ) {
         my ( $tag,  $subfield ) = split '\$', $unless_exists_field;
@@ -94,6 +92,8 @@ add_date_fields_to_marc_records.pl
 
   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
 
+  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
+
 =head1 DESCRIPTION
 
 Add some MARC fields to bibliographic records.
@@ -120,6 +120,8 @@ Confirmation flag, the script will be running in dry-run mode if set not.
 
 Limits the search on bibliographic records with a user-specified WHERE clause.
 
+Only the columns from the biblio table are available.
+
 =item B<--field>
 
 Fields to add to the bibliographic records.
@@ -134,7 +136,11 @@ For instance:
 
 =item B<--unless-exists>
 
-Will only create the new fields if this field does not exist
+Will only create the new fields if this field does not exist.
+
+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.
+If --unless-exists is not passed, a new 905$a will be created in any case.
+
 
 =back