View | Details | Raw Unified | Return to bug 23924
Collapse All | Expand All

(-)a/misc/add_date_fields_to_marc_records.pl (-13 / +43 lines)
Lines 27-33 use C4::Biblio; Link Here
27
use Koha::Biblios;
27
use Koha::Biblios;
28
use Koha::DateUtils qw( dt_from_string );
28
use Koha::DateUtils qw( dt_from_string );
29
29
30
my ( $verbose, $help, $confirm, $where, @fields, $unless_exists_field );
30
my ( $verbose, $help, $confirm, $where, @fields, $date_field, $unless_exists_field );
31
my $dbh = C4::Context->dbh;
31
my $dbh = C4::Context->dbh;
32
32
33
GetOptions(
33
GetOptions(
Lines 36-60 GetOptions( Link Here
36
    'confirm|c' => \$confirm,
36
    'confirm|c' => \$confirm,
37
    'where=s'   => \$where,
37
    'where=s'   => \$where,
38
    'field=s@'  => \@fields,
38
    'field=s@'  => \@fields,
39
    'date-field=s' => \$date_field,
39
    'unless-exists=s' => \$unless_exists_field,
40
    'unless-exists=s' => \$unless_exists_field,
40
) || podusage(1);
41
) || podusage(1);
41
42
42
pod2usage(1) if $help;
43
pod2usage(1) if $help;
43
pod2usage("Parameter field is mandatory") unless @fields;
44
pod2usage("Parameter field is mandatory") unless @fields;
44
45
46
say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
47
45
my @fields_to_add;
48
my @fields_to_add;
46
my $dt = dt_from_string;    # Could be an option of the script
49
unless ( $date_field ) {
47
for my $field (@fields) {
50
    my $dt = dt_from_string;
48
    my ( $f_sf, $value )    = split '=',  $field;
51
    for my $field (@fields) {
49
    my ( $tag,  $subfield ) = split '\$', $f_sf;
52
        my ( $f_sf, $value )    = split '=',  $field;
50
    push @fields_to_add,
53
        my ( $tag,  $subfield ) = split '\$', $f_sf;
51
      MARC::Field->new( $tag, '', '', $subfield => $dt->strftime($value) );
54
        push @fields_to_add,
52
}
55
          MARC::Field->new( $tag, '', '', $subfield => $dt->strftime($value) );
56
    }
53
57
54
say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
58
    if ($verbose) {
55
if ($verbose) {
59
        say "The following MARC fields will be added:";
56
    say "The following MARC fields will be added:";
60
        say "\t" . $_->as_formatted for @fields_to_add;
57
    say "\t" . $_->as_formatted for @fields_to_add;
61
    }
58
}
62
}
59
63
60
$where = $where ? "WHERE $where" : '';
64
$where = $where ? "WHERE $where" : '';
Lines 70-76 while ( my ( $biblionumber, $frameworkcode ) = $sth->fetchrow_array ) { Link Here
70
        my ( $tag,  $subfield ) = split '\$', $unless_exists_field;
74
        my ( $tag,  $subfield ) = split '\$', $unless_exists_field;
71
        next if $marc_record->subfield($tag, $subfield);
75
        next if $marc_record->subfield($tag, $subfield);
72
    }
76
    }
77
    if ( $date_field ) {
78
        my ( $tag,  $subfield ) = split '\$', $date_field;
79
        my $date = $marc_record->subfield($tag, $subfield);
80
        next unless $date;
81
        warn $date;
82
        my $dt = dt_from_string($date);
83
        for my $field (@fields) {
84
            my ( $f_sf, $value )    = split '=',  $field;
85
            my ( $tag,  $subfield ) = split '\$', $f_sf;
86
            push @fields_to_add,
87
              MARC::Field->new( $tag, '', '', $subfield => $dt->strftime($value) );
88
        }
89
        if ($verbose) {
90
            say sprintf "The following MARC fields will be added to record %s:", $biblionumber;
91
            say "\t" . $_->as_formatted for @fields_to_add;
92
        }
93
    }
94
73
    $marc_record->append_fields(@fields_to_add);
95
    $marc_record->append_fields(@fields_to_add);
96
74
    if ($confirm) {
97
    if ($confirm) {
75
        my $modified =
98
        my $modified =
76
          C4::Biblio::ModBiblio( $marc_record, $biblionumber, $frameworkcode );
99
          C4::Biblio::ModBiblio( $marc_record, $biblionumber, $frameworkcode );
Lines 94-99 add_date_fields_to_marc_records.pl Link Here
94
117
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
118
  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
96
119
120
  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' --date-field='908$a' --verbose --confirm
121
97
=head1 DESCRIPTION
122
=head1 DESCRIPTION
98
123
99
Add some MARC fields to bibliographic records.
124
Add some MARC fields to bibliographic records.
Lines 141-146 Will only create the new fields if this field does not exist. Link Here
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.
166
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.
167
If --unless-exists is not passed, a new 905$a will be created in any case.
143
168
169
=item B<--date-field>
170
171
The date will be picked from a specific marc field.
172
173
For instance, if the record contains a date formatted YYYY-MM-DD in 908$a, you can pass --date-field='908$a'
174
Note that the date format used will be the one defined in the syspref 'dateformat', or iso format.
144
175
145
=back
176
=back
146
177
147
- 

Return to bug 23924