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;
55 die "date-created-marc field is greated that 009, it should have a subfield."
56 if ( $c_field > 9 && !defined $c_subfield );
57 die "date-created-marc field is lower that 010, it should not have a subfield."
58 if ( $c_field < 10 && defined $c_subfield );
60 print "Date created on $c_field";
61 print $c_subfield if defined $c_subfield; # use of defined to allow 0
65 # date last modified MARC field/subfield
66 $date_modified_marc = '099d' unless $date_modified_marc;
67 my ( $m_field, $m_subfield ) = _read_marc_code
($date_modified_marc);
68 die "date-modified-marc '$date_modified_marc' is not correct." unless $m_field;
69 die "date-modified-marc field is greated that 009, it should have a subfield."
70 if ( $m_field > 9 && !defined $m_subfield );
71 die "date-modified-marc field is lower that 010, it should not have a subfield."
72 if ( $m_field < 10 && defined $m_subfield );
74 "When date-created-marc and date-modified-marc are on same field, they should have distinct subfields"
75 if ( $c_field eq $m_field )
76 && ( !defined $c_subfield
77 || !defined $m_subfield
78 || $c_subfield eq $m_subfield );
80 print "Date last modified on $m_field";
81 print $m_subfield if defined $m_subfield; # use of defined to allow 0
90 my $biblio = GetMarcBiblio
($id);
93 $debug and warn '[ERROR] GetMarcBiblio did not return any biblio.';
97 my $c_marc_field = $biblio->field($c_field);
98 my $m_marc_field = $biblio->field($m_field);
104 ?
$c_marc_field->subfield($c_subfield)
105 : $c_marc_field->data();
107 $c_marc_value = '' unless defined $c_marc_value;
113 ?
$m_marc_field->subfield($m_subfield)
114 : $m_marc_field->data();
116 $m_marc_value ||= '';
118 $sth_prepared = $dbh->prepare(
121 DATE_FORMAT(datecreated,'%Y-%m-%d') AS datecreatediso,
122 DATE_FORMAT(timestamp,'%Y-%m-%d') AS datemodifiediso,
125 WHERE biblionumber = ?
127 ) unless $sth_prepared;
128 $sth_prepared->execute($id);
129 my $bibliorow = $sth_prepared->fetchrow_hashref;
130 my $frameworkcode = $bibliorow->{'frameworkcode'};
131 my $c_db_value = $bibliorow->{'datecreatediso'} || '';
132 my $m_db_value = $bibliorow->{'datemodifiediso'} || '';
134 # do nothing if already sync
135 return if ( $c_marc_value eq $c_db_value && $m_marc_value eq $m_db_value );
137 # do apply to database ?
138 return 1 unless $run;
143 unless ($c_marc_field) {
144 if ( defined $c_subfield ) {
147 $c_field, ' ', ' ', $c_subfield => $c_db_value
152 $biblio->add_fields( new MARC::Field( $c_field, $c_db_value ) );
154 $debug and warn "[WARN] $c_field did not exist.";
155 $c_marc_field = $biblio->field($c_field);
158 if ( $m_field eq $c_field ) {
159 $m_marc_field = $c_marc_field;
163 if ( defined $c_subfield ) {
164 $c_marc_field->update( $c_subfield, $c_db_value );
167 $c_marc_field->update($c_db_value);
171 # date last modified field
172 unless ($m_marc_field) {
173 if ( defined $m_subfield ) {
176 $m_field, ' ', ' ', $m_subfield => $m_db_value
181 $biblio->add_fields( new MARC::Field( $m_field, $m_db_value ) );
184 $debug and warn "[WARN] $m_field did not exist.";
185 $m_marc_field = $biblio->field($m_field);
188 if ( defined $m_subfield ) {
189 $m_marc_field->update( $m_subfield, $m_db_value );
192 $m_marc_field->update($m_db_value);
197 if ( &ModBiblio( $biblio, $id, $frameworkcode ) ) {
201 $debug and warn '[ERROR] ModBiblio failed.';
207 $dbh = C4::Context->dbh;
213 JOIN biblioitems USING (biblionumber)
215 $query .= qq{ WHERE
$where} if $where;
216 my $sth = $dbh->prepare($query);
219 $verbose and print "Number of concerned biblios: " . $sth->rows . "\n";
221 while ( my $biblios = $sth->fetchrow_hashref ) {
222 $verbose and print 'Bib ' . $biblios->{'biblionumber'} . ':';
223 my $ret = updateMarc
( $biblios->{'biblionumber'} );
225 $verbose and print 'modified';
228 $verbose and print "\n";
231 $verbose and print "Number of modified biblios: " . $mod_count . "\n";
234 if ( lc( C4
::Context
->preference('marcflavour') ) eq "unimarc" ) {
237 and print "*** Not in run mode, modifications will not be applyed ***\n";
239 $verbose and print "================================\n";
244 "This script is UNIMARC only and should be used only on UNIMARC databases\n";
249 Synchronizes date created
and date
last modified from biblio table to MARC data
.
250 Does
not update biblio
if dates are already synchronized
.
254 --help
or -h show this message
255 --verbose
or -v verbose logging
256 --run run the command
else modifications will
not be applied to database
257 --where
(optional
) use this to limit execution to some biblios
:
258 write a SQL where clause using biblio
and/or biblioitems fields
259 --date
-created
-marc
or c
(optional
) date created MARC field
and optional subfield
,
261 --date
-modified
-marc
or m
(optional
) date
last modified MARC field
and optional subfield
,