Lines 2-8
Link Here
|
2 |
# |
2 |
# |
3 |
# This script should be used only with UNIMARC flavour |
3 |
# This script should be used only with UNIMARC flavour |
4 |
# It is designed to report some missing information from biblio |
4 |
# It is designed to report some missing information from biblio |
5 |
# table into marc data |
5 |
# table into marc data |
6 |
# |
6 |
# |
7 |
use strict; |
7 |
use strict; |
8 |
use warnings; |
8 |
use warnings; |
Lines 13-75
BEGIN {
Link Here
|
13 |
} |
13 |
} |
14 |
|
14 |
|
15 |
use C4::Biblio; |
15 |
use C4::Biblio; |
|
|
16 |
use Getopt::Long; |
17 |
|
18 |
sub _read_marc_code { |
19 |
my $input = shift; |
20 |
my ( $field, $subfield ); |
21 |
if ( $input =~ /^(\d{3})$/ ) { |
22 |
$field = $1; |
23 |
} elsif ( $input =~ /^(\d{3})(\w)$/ ) { |
24 |
$field = $1; |
25 |
$subfield = $2; |
26 |
} |
27 |
return ( $field, $subfield ); |
28 |
} |
29 |
|
30 |
my ( $run, $help, $verbose, $where, $date_created_marc, $date_modified_marc ); |
31 |
GetOptions( |
32 |
'help|h' => \$help, |
33 |
'verbose|v' => \$verbose, |
34 |
'run' => \$run, |
35 |
'where:s' => \$where, |
36 |
'date-created-marc|c:s' => \$date_created_marc, |
37 |
'date-modified-marc|m:s' => \$date_modified_marc, |
38 |
); |
39 |
my $debug = $ENV{DEBUG}; |
40 |
$verbose = 1 if $debug; |
41 |
|
42 |
# display help ? |
43 |
if ($help) { |
44 |
print_usage(); |
45 |
exit 0; |
46 |
} |
47 |
|
48 |
$verbose and print "================================\n"; |
49 |
|
50 |
# date created MARC field/subfield |
51 |
$date_created_marc = '099c' unless $date_created_marc; |
52 |
my ( $c_field, $c_subfield ) = _read_marc_code($date_created_marc); |
53 |
die "date-created-marc '$date_created_marc' is not correct." unless $c_field; |
54 |
if ($verbose) { |
55 |
print "Date created on $c_field"; |
56 |
print $c_subfield if defined $c_subfield; # use of defined to allow 0 |
57 |
print "\n"; |
58 |
} |
59 |
|
60 |
# date last modified MARC field/subfield |
61 |
$date_modified_marc = '099d' unless $date_modified_marc; |
62 |
my ( $m_field, $m_subfield ) = _read_marc_code($date_modified_marc); |
63 |
die "date-modified-marc '$date_modified_marc' is not correct." unless $m_field; |
64 |
die |
65 |
"When date-created-marc and date-modified-marc are on same field, they should have distinct subfields" |
66 |
if ( $c_field eq $m_field ) |
67 |
&& ( !defined $c_subfield |
68 |
|| !defined $m_subfield |
69 |
|| $c_subfield eq $m_subfield ); |
70 |
if ($verbose) { |
71 |
print "Date last modified on $m_field"; |
72 |
print $m_subfield if defined $m_subfield; # use of defined to allow 0 |
73 |
print "\n"; |
74 |
} |
75 |
|
76 |
my $dbh; |
77 |
my $sth_prepared; |
16 |
|
78 |
|
17 |
sub updateMarc { |
79 |
sub updateMarc { |
18 |
my $id = shift; |
80 |
my $id = shift; |
19 |
my $dbh = C4::Context->dbh; |
|
|
20 |
my $field; |
21 |
my $biblio = GetMarcBiblio($id); |
81 |
my $biblio = GetMarcBiblio($id); |
22 |
|
82 |
|
23 |
return unless $biblio; |
83 |
unless ($biblio) { |
24 |
|
84 |
$debug and warn '[ERROR] GetMarcBiblio did not return any biblio.'; |
25 |
if(!$biblio->field('099')) |
85 |
return; |
26 |
{ |
|
|
27 |
$field = new MARC::Field('099','','', |
28 |
'c' => '', |
29 |
'd'=>''); |
30 |
$biblio->add_fields($field); |
31 |
} |
86 |
} |
32 |
|
87 |
|
33 |
$field = $biblio->field('099'); |
88 |
my $c_marc_field = $biblio->field($c_field); |
|
|
89 |
my $m_marc_field = $biblio->field($m_field); |
34 |
|
90 |
|
35 |
my $sth = $dbh->prepare("SELECT DATE_FORMAT(datecreated,'%Y-%m-%d') as datecreated, |
91 |
my $c_marc_value; |
36 |
DATE_FORMAT(timestamp,'%Y-%m-%d') as timestamp, |
92 |
if ($c_marc_field) { |
37 |
frameworkcode |
93 |
$c_marc_value = |
38 |
FROM biblio |
94 |
defined $c_subfield |
39 |
WHERE biblionumber = ?"); |
95 |
? $c_marc_field->subfield($c_subfield) |
40 |
$sth->execute($id); |
96 |
: $c_marc_field->data(); |
41 |
(my $bibliorow = $sth->fetchrow_hashref); |
97 |
} |
|
|
98 |
$c_marc_value = '' unless defined $c_marc_value; |
99 |
|
100 |
my $m_marc_value; |
101 |
if ($m_marc_field) { |
102 |
$m_marc_value = |
103 |
defined $m_subfield |
104 |
? $m_marc_field->subfield($m_subfield) |
105 |
: $m_marc_field->data(); |
106 |
} |
107 |
$m_marc_value ||= ''; |
108 |
|
109 |
$sth_prepared = $dbh->prepare( |
110 |
q{ |
111 |
SELECT |
112 |
DATE_FORMAT(datecreated,'%Y-%m-%d') AS datecreatediso, |
113 |
DATE_FORMAT(timestamp,'%Y-%m-%d') AS datemodifiediso, |
114 |
frameworkcode |
115 |
FROM biblio |
116 |
WHERE biblionumber = ? |
117 |
} |
118 |
) unless $sth_prepared; |
119 |
$sth_prepared->execute($id); |
120 |
my $bibliorow = $sth_prepared->fetchrow_hashref; |
42 |
my $frameworkcode = $bibliorow->{'frameworkcode'}; |
121 |
my $frameworkcode = $bibliorow->{'frameworkcode'}; |
|
|
122 |
my $c_db_value = $bibliorow->{'datecreatediso'} || ''; |
123 |
my $m_db_value = $bibliorow->{'datemodifiediso'} || ''; |
43 |
|
124 |
|
44 |
$field->update( 'c' => $bibliorow->{'datecreated'}, |
125 |
# do nothing if already sync |
45 |
'd' => $bibliorow->{'timestamp'} |
126 |
return if ( $c_marc_value eq $c_db_value && $m_marc_value eq $m_db_value ); |
46 |
); |
|
|
47 |
|
127 |
|
48 |
if(&ModBiblio($biblio, $id, $frameworkcode)) |
128 |
# do apply to database ? |
49 |
{ |
129 |
return 1 unless $run; |
50 |
print "\r$id"; |
|
|
51 |
} |
52 |
|
130 |
|
|
|
131 |
# update MARC record |
132 |
|
133 |
# date created field |
134 |
if ($c_marc_field) { |
135 |
if ( defined $c_subfield ) { |
136 |
$c_marc_field->update( $c_subfield, $c_db_value ); |
137 |
} |
138 |
else { |
139 |
$c_marc_field->update($c_db_value); |
140 |
} |
141 |
} |
142 |
else { |
143 |
$c_marc_field = |
144 |
new MARC::Field( $c_field, '', '', $c_subfield => $c_db_value, ); |
145 |
$biblio->add_fields($c_marc_field); |
146 |
$debug and warn "[WARN] $c_field did not exist."; |
147 |
|
148 |
# when on same field |
149 |
if ( $m_field eq $c_field ) { |
150 |
$m_marc_field = $c_marc_field; |
151 |
} |
152 |
} |
153 |
|
154 |
# date last modified field |
155 |
if ($m_marc_field) { |
156 |
if ( defined $m_subfield ) { |
157 |
$m_marc_field->update( $m_subfield, $m_db_value ); |
158 |
} |
159 |
else { |
160 |
$m_marc_field->update($m_db_value); |
161 |
} |
162 |
} |
163 |
else { |
164 |
$m_marc_field = |
165 |
new MARC::Field( $m_field, '', '', $m_subfield => $m_db_value, ); |
166 |
$biblio->add_fields($m_marc_field); |
167 |
$debug and warn "[WARN] $m_field did not exist."; |
168 |
} |
169 |
|
170 |
# apply to databse |
171 |
if ( &ModBiblio( $biblio, $id, $frameworkcode ) ) { |
172 |
return 1; |
173 |
} |
174 |
|
175 |
$debug and warn '[ERROR] ModBiblio failed.'; |
176 |
return; |
53 |
} |
177 |
} |
54 |
|
178 |
|
55 |
sub process { |
179 |
sub process { |
56 |
|
180 |
|
57 |
my $dbh = C4::Context->dbh; |
181 |
$dbh = C4::Context->dbh; |
|
|
182 |
my $mod_count = 0; |
58 |
|
183 |
|
59 |
my $sth = $dbh->prepare("SELECT biblionumber FROM biblio"); |
184 |
my $query = q{ |
|
|
185 |
SELECT biblionumber |
186 |
FROM biblio |
187 |
JOIN biblioitems USING (biblionumber) |
188 |
}; |
189 |
$query .= qq{ WHERE $where} if $where; |
190 |
my $sth = $dbh->prepare($query); |
60 |
$sth->execute(); |
191 |
$sth->execute(); |
61 |
|
192 |
|
62 |
while(my $biblios = $sth->fetchrow_hashref) |
193 |
$verbose and print "Number of concerned biblios: " . $sth->rows . "\n"; |
63 |
{ |
194 |
|
64 |
updateMarc($biblios->{'biblionumber'}); |
195 |
while ( my $biblios = $sth->fetchrow_hashref ) { |
65 |
print "."; |
196 |
$verbose and print 'Bib ' . $biblios->{'biblionumber'} . ':'; |
|
|
197 |
my $ret = updateMarc( $biblios->{'biblionumber'} ); |
198 |
if ($ret) { |
199 |
$verbose and print 'modified'; |
200 |
$mod_count++; |
201 |
} |
202 |
$verbose and print "\n"; |
66 |
} |
203 |
} |
67 |
|
204 |
|
|
|
205 |
$verbose and print "Number of modified biblios: " . $mod_count . "\n"; |
68 |
} |
206 |
} |
69 |
|
207 |
|
70 |
if (lc(C4::Context->preference('marcflavour')) eq "unimarc"){ |
208 |
if ( lc( C4::Context->preference('marcflavour') ) eq "unimarc" ) { |
71 |
process(); |
209 |
$verbose |
72 |
} |
210 |
and !$run |
|
|
211 |
and print "*** Not in run mode, modifications will not be applyed ***\n"; |
212 |
|
213 |
$verbose and print "================================\n"; |
214 |
process(); |
215 |
} |
73 |
else { |
216 |
else { |
74 |
print "this script is UNIMARC only and should be used only on unimarc databases\n"; |
217 |
print |
|
|
218 |
"This script is UNIMARC only and should be used only on UNIMARC databases\n"; |
219 |
} |
220 |
|
221 |
sub print_usage { |
222 |
print <<_USAGE_; |
223 |
Synchronizes date created and date last modified from biblio table to MARC data. |
224 |
Does not update biblio if dates are already synchronized. |
225 |
UNIMARC specific. |
226 |
|
227 |
Parameters: |
228 |
--help or -h show this message |
229 |
--verbose or -v verbose logging |
230 |
--run run the command else modifications will not be applyed to database |
231 |
--where (optional) use this to limit execution to some biblios : |
232 |
write a SQL where clause using biblio and/or biblioitems fields |
233 |
--date-created-marc or c (optional) date created MARC field and optional subfield, |
234 |
099c by default |
235 |
--date-modified-marc or m (optional) date last modified MARC field and optional subfield, |
236 |
099d by default |
237 |
_USAGE_ |
75 |
} |
238 |
} |
76 |
- |
|
|