3 # This script should be used only with UNIMARC flavour
4 # It is designed to report some missing information from biblio
11 use C4::Biblio qw( ModBiblio );
13 use Getopt::Long qw( GetOptions );
17 my ( $field, $subfield );
18 if ( $input =~ /^(\d{3})$/ ) {
21 elsif ( $input =~ /^(\d{3})(\w)$/ ) {
25 return ( $field, $subfield );
28 my ( $run, $help, $verbose, $where, $date_created_marc, $date_modified_marc );
31 'verbose|v' => \$verbose,
34 'date-created-marc|c:s' => \$date_created_marc,
35 'date-modified-marc|m:s' => \$date_modified_marc,
37 my $debug = 0; # FIXME pass an option for that?
38 $verbose = 1 if $debug;
46 $verbose and print "================================\n";
48 # date created MARC field/subfield
49 $date_created_marc = '099c' unless $date_created_marc;
50 my ( $c_field, $c_subfield ) = _read_marc_code($date_created_marc);
51 die "date-created-marc '$date_created_marc' is not correct." unless $c_field;
52 die "date-created-marc field is greated that 009, it should have a subfield."
53 if ( $c_field > 9 && !defined $c_subfield );
54 die "date-created-marc field is lower that 010, it should not have a subfield."
55 if ( $c_field < 10 && defined $c_subfield );
57 print "Date created on $c_field";
58 print $c_subfield if defined $c_subfield; # use of defined to allow 0
62 # date last modified MARC field/subfield
63 $date_modified_marc = '099d' unless $date_modified_marc;
64 my ( $m_field, $m_subfield ) = _read_marc_code($date_modified_marc);
65 die "date-modified-marc '$date_modified_marc' is not correct." unless $m_field;
66 die "date-modified-marc field is greated that 009, it should have a subfield."
67 if ( $m_field > 9 && !defined $m_subfield );
68 die "date-modified-marc field is lower that 010, it should not have a subfield."
69 if ( $m_field < 10 && defined $m_subfield );
71 "When date-created-marc and date-modified-marc are on same field, they should have distinct subfields"
72 if ( $c_field eq $m_field )
73 && ( !defined $c_subfield
74 || !defined $m_subfield
75 || $c_subfield eq $m_subfield );
77 print "Date last modified on $m_field";
78 print $m_subfield if defined $m_subfield; # use of defined to allow 0
87 my $biblio = Koha::Biblios->find($id);
88 $biblio &&= $biblio->metadata->record;
91 $debug and warn '[ERROR] Biblio not found.';
95 my $c_marc_field = $biblio->field($c_field);
96 my $m_marc_field = $biblio->field($m_field);
102 ? $c_marc_field->subfield($c_subfield)
103 : $c_marc_field->data();
105 $c_marc_value = '' unless defined $c_marc_value;
111 ? $m_marc_field->subfield($m_subfield)
112 : $m_marc_field->data();
114 $m_marc_value ||= '';
116 $sth_prepared = $dbh->prepare(
119 DATE_FORMAT(datecreated,'%Y-%m-%d') AS datecreatediso,
120 DATE_FORMAT(timestamp,'%Y-%m-%d') AS datemodifiediso,
123 WHERE biblionumber = ?
125 ) unless $sth_prepared;
126 $sth_prepared->execute($id);
127 my $bibliorow = $sth_prepared->fetchrow_hashref;
128 my $frameworkcode = $bibliorow->{'frameworkcode'};
129 my $c_db_value = $bibliorow->{'datecreatediso'} || '';
130 my $m_db_value = $bibliorow->{'datemodifiediso'} || '';
132 # do nothing if already sync
133 return if ( $c_marc_value eq $c_db_value && $m_marc_value eq $m_db_value );
135 # do apply to database ?
136 return 1 unless $run;
141 unless ($c_marc_field) {
142 if ( defined $c_subfield ) {
145 $c_field, ' ', ' ', $c_subfield => $c_db_value
150 $biblio->add_fields( MARC::Field->new( $c_field, $c_db_value ) );
152 $debug and warn "[WARN] $c_field did not exist.";
153 $c_marc_field = $biblio->field($c_field);
156 if ( $m_field eq $c_field ) {
157 $m_marc_field = $c_marc_field;
161 if ( defined $c_subfield ) {
162 $c_marc_field->update( $c_subfield, $c_db_value );
165 $c_marc_field->update($c_db_value);
169 # date last modified field
170 unless ($m_marc_field) {
171 if ( defined $m_subfield ) {
174 $m_field, ' ', ' ', $m_subfield => $m_db_value
179 $biblio->add_fields( MARC::Field->new( $m_field, $m_db_value ) );
182 $debug and warn "[WARN] $m_field did not exist.";
183 $m_marc_field = $biblio->field($m_field);
186 if ( defined $m_subfield ) {
187 $m_marc_field->update( $m_subfield, $m_db_value );
190 $m_marc_field->update($m_db_value);
195 if ( &ModBiblio( $biblio, $id, $frameworkcode ) ) {
199 $debug and warn '[ERROR] ModBiblio failed.';
205 $dbh = C4::Context->dbh;
211 JOIN biblioitems USING (biblionumber)
213 $query .= qq{ WHERE $where} if $where;
214 my $sth = $dbh->prepare($query);
217 $verbose and print "Number of concerned biblios: " . $sth->rows . "\n";
219 while ( my $biblios = $sth->fetchrow_hashref ) {
220 $verbose and print 'Bib ' . $biblios->{'biblionumber'} . ':';
221 my $ret = updateMarc( $biblios->{'biblionumber'} );
223 $verbose and print 'modified';
226 $verbose and print "\n";
229 $verbose and print "Number of modified biblios: " . $mod_count . "\n";
232 if ( lc( C4::Context->preference('marcflavour') ) eq "unimarc" ) {
235 and print "*** Not in run mode, modifications will not be applyed ***\n";
237 $verbose and print "================================\n";
242 "This script is UNIMARC only and should be used only on UNIMARC databases\n";
247 Synchronizes date created and date last modified from biblio table to MARC data.
248 Does not update biblio if dates are already synchronized.
252 --help or -h show this message
253 --verbose or -v verbose logging
254 --run run the command else modifications will not be applied to database
255 --where (optional) use this to limit execution to some biblios :
256 write a SQL where clause using biblio and/or biblioitems fields
257 --date-created-marc or c (optional) date created MARC field and optional subfield,
259 --date-modified-marc or m (optional) date last modified MARC field and optional subfield,