3 # This script should be used only with UNIMARC flavour
4 # It is designed to report some missing information from biblio
12 eval { require "$FindBin::Bin/../kohalib.pl" };
20 my ( $field, $subfield );
21 if ( $input =~ /^(\d{3})$/ ) {
24 elsif ( $input =~ /^(\d{3})(\w)$/ ) {
28 return ( $field, $subfield );
31 my ( $run, $help, $verbose, $where, $date_created_marc, $date_modified_marc );
34 'verbose|v' => \$verbose,
37 'date-created-marc|c:s' => \$date_created_marc,
38 'date-modified-marc|m:s' => \$date_modified_marc,
40 my $debug = $ENV{DEBUG};
41 $verbose = 1 if $debug;
49 $verbose and print "================================\n";
51 # date created MARC field/subfield
52 $date_created_marc = '099c' unless $date_created_marc;
53 my ( $c_field, $c_subfield ) = _read_marc_code($date_created_marc);
54 die "date-created-marc '$date_created_marc' is not correct." unless $c_field;
56 print "Date created on $c_field";
57 print $c_subfield if defined $c_subfield; # use of defined to allow 0
61 # date last modified MARC field/subfield
62 $date_modified_marc = '099d' unless $date_modified_marc;
63 my ( $m_field, $m_subfield ) = _read_marc_code($date_modified_marc);
64 die "date-modified-marc '$date_modified_marc' is not correct." unless $m_field;
66 "When date-created-marc and date-modified-marc are on same field, they should have distinct subfields"
67 if ( $c_field eq $m_field )
68 && ( !defined $c_subfield
69 || !defined $m_subfield
70 || $c_subfield eq $m_subfield );
72 print "Date last modified on $m_field";
73 print $m_subfield if defined $m_subfield; # use of defined to allow 0
82 my $biblio = GetMarcBiblio($id);
85 $debug and warn '[ERROR] GetMarcBiblio did not return any biblio.';
89 my $c_marc_field = $biblio->field($c_field);
90 my $m_marc_field = $biblio->field($m_field);
96 ? $c_marc_field->subfield($c_subfield)
97 : $c_marc_field->data();
99 $c_marc_value = '' unless defined $c_marc_value;
105 ? $m_marc_field->subfield($m_subfield)
106 : $m_marc_field->data();
108 $m_marc_value ||= '';
110 $sth_prepared = $dbh->prepare(
113 DATE_FORMAT(datecreated,'%Y-%m-%d') AS datecreatediso,
114 DATE_FORMAT(timestamp,'%Y-%m-%d') AS datemodifiediso,
117 WHERE biblionumber = ?
119 ) unless $sth_prepared;
120 $sth_prepared->execute($id);
121 my $bibliorow = $sth_prepared->fetchrow_hashref;
122 my $frameworkcode = $bibliorow->{'frameworkcode'};
123 my $c_db_value = $bibliorow->{'datecreatediso'} || '';
124 my $m_db_value = $bibliorow->{'datemodifiediso'} || '';
126 # do nothing if already sync
127 return if ( $c_marc_value eq $c_db_value && $m_marc_value eq $m_db_value );
129 # do apply to database ?
130 return 1 unless $run;
135 unless ($c_marc_field) {
136 $biblio->add_fields( new MARC::Field( $c_field, '', '' ) );
137 $debug and warn "[WARN] $c_field did not exist.";
138 $c_marc_field = $biblio->field($c_field);
141 if ( $m_field eq $c_field ) {
142 $m_marc_field = $c_marc_field;
145 if ( defined $c_subfield ) {
146 $c_marc_field->update( $c_subfield, $c_db_value );
149 $c_marc_field->update($c_db_value);
152 # date last modified field
153 unless ($m_marc_field) {
154 $biblio->add_fields( new MARC::Field( $m_field, '', '' ) );
155 $debug and warn "[WARN] $m_field did not exist.";
156 $m_marc_field = $biblio->field($m_field);
158 if ( defined $m_subfield ) {
159 $m_marc_field->update( $m_subfield, $m_db_value );
162 $m_marc_field->update($m_db_value);
166 if ( &ModBiblio( $biblio, $id, $frameworkcode ) ) {
170 $debug and warn '[ERROR] ModBiblio failed.';
176 $dbh = C4::Context->dbh;
182 JOIN biblioitems USING (biblionumber)
184 $query .= qq{ WHERE $where} if $where;
185 my $sth = $dbh->prepare($query);
188 $verbose and print "Number of concerned biblios: " . $sth->rows . "\n";
190 while ( my $biblios = $sth->fetchrow_hashref ) {
191 $verbose and print 'Bib ' . $biblios->{'biblionumber'} . ':';
192 my $ret = updateMarc( $biblios->{'biblionumber'} );
194 $verbose and print 'modified';
197 $verbose and print "\n";
200 $verbose and print "Number of modified biblios: " . $mod_count . "\n";
203 if ( lc( C4::Context->preference('marcflavour') ) eq "unimarc" ) {
206 and print "*** Not in run mode, modifications will not be applyed ***\n";
208 $verbose and print "================================\n";
213 "This script is UNIMARC only and should be used only on UNIMARC databases\n";
218 Synchronizes date created and date last modified from biblio table to MARC data.
219 Does not update biblio if dates are already synchronized.
223 --help or -h show this message
224 --verbose or -v verbose logging
225 --run run the command else modifications will not be applyed to database
226 --where (optional) use this to limit execution to some biblios :
227 write a SQL where clause using biblio and/or biblioitems fields
228 --date-created-marc or c (optional) date created MARC field and optional subfield,
230 --date-modified-marc or m (optional) date last modified MARC field and optional subfield,