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

Return to bug 23924