Bug 9019: Return item fields in ILS-DI GetRecords
[koha.git] / misc / maintenance / UNIMARC_sync_date_created_with_marc_biblio.pl
1 #!/usr/bin/perl
2 #
3 # This script should be used only with UNIMARC flavour
4 # It is designed to report some missing information from biblio
5 # table into marc data
6 #
7 use strict;
8 use warnings;
9
10 BEGIN {
11     use FindBin;
12     eval { require "$FindBin::Bin/../kohalib.pl" };
13 }
14
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     }
24     elsif ( $input =~ /^(\d{3})(\w)$/ ) {
25         $field    = $1;
26         $subfield = $2;
27     }
28     return ( $field, $subfield );
29 }
30
31 my ( $run, $help, $verbose, $where, $date_created_marc, $date_modified_marc );
32 GetOptions(
33     'help|h'                 => \$help,
34     'verbose|v'              => \$verbose,
35     'run'                    => \$run,
36     'where:s'                => \$where,
37     'date-created-marc|c:s'  => \$date_created_marc,
38     'date-modified-marc|m:s' => \$date_modified_marc,
39 );
40 my $debug = $ENV{DEBUG};
41 $verbose = 1 if $debug;
42
43 # display help ?
44 if ($help) {
45     print_usage();
46     exit 0;
47 }
48
49 $verbose and print "================================\n";
50
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 if ($verbose) {
56     print "Date created on $c_field";
57     print $c_subfield if defined $c_subfield;    # use of defined to allow 0
58     print "\n";
59 }
60
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;
65 die
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 );
71 if ($verbose) {
72     print "Date last modified on $m_field";
73     print $m_subfield if defined $m_subfield;    # use of defined to allow 0
74     print "\n";
75 }
76
77 my $dbh;
78 my $sth_prepared;
79
80 sub updateMarc {
81     my $id     = shift;
82     my $biblio = GetMarcBiblio($id);
83
84     unless ($biblio) {
85         $debug and warn '[ERROR] GetMarcBiblio did not return any biblio.';
86         return;
87     }
88
89     my $c_marc_field = $biblio->field($c_field);
90     my $m_marc_field = $biblio->field($m_field);
91
92     my $c_marc_value;
93     if ($c_marc_field) {
94         $c_marc_value =
95           defined $c_subfield
96           ? $c_marc_field->subfield($c_subfield)
97           : $c_marc_field->data();
98     }
99     $c_marc_value = '' unless defined $c_marc_value;
100
101     my $m_marc_value;
102     if ($m_marc_field) {
103         $m_marc_value =
104           defined $m_subfield
105           ? $m_marc_field->subfield($m_subfield)
106           : $m_marc_field->data();
107     }
108     $m_marc_value ||= '';
109
110     $sth_prepared = $dbh->prepare(
111         q{
112         SELECT
113             DATE_FORMAT(datecreated,'%Y-%m-%d') AS datecreatediso,
114             DATE_FORMAT(timestamp,'%Y-%m-%d') AS datemodifiediso,
115             frameworkcode
116         FROM biblio
117         WHERE biblionumber = ?
118     }
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'} || '';
125
126     # do nothing if already sync
127     return if ( $c_marc_value eq $c_db_value && $m_marc_value eq $m_db_value );
128
129     # do apply to database ?
130     return 1 unless $run;
131
132     # update MARC record
133
134     # date created field
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);
139
140         # when on same field
141         if ( $m_field eq $c_field ) {
142             $m_marc_field = $c_marc_field;
143         }
144     }
145     if ( defined $c_subfield ) {
146         $c_marc_field->update( $c_subfield, $c_db_value );
147     }
148     else {
149         $c_marc_field->update($c_db_value);
150     }
151
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);
157     }
158     if ( defined $m_subfield ) {
159         $m_marc_field->update( $m_subfield, $m_db_value );
160     }
161     else {
162         $m_marc_field->update($m_db_value);
163     }
164
165     # apply to databse
166     if ( &ModBiblio( $biblio, $id, $frameworkcode ) ) {
167         return 1;
168     }
169
170     $debug and warn '[ERROR] ModBiblio failed.';
171     return;
172 }
173
174 sub process {
175
176     $dbh = C4::Context->dbh;
177     my $mod_count = 0;
178
179     my $query = q{
180         SELECT biblionumber
181         FROM biblio
182         JOIN biblioitems USING (biblionumber)
183     };
184     $query .= qq{ WHERE $where} if $where;
185     my $sth = $dbh->prepare($query);
186     $sth->execute();
187
188     $verbose and print "Number of concerned biblios: " . $sth->rows . "\n";
189
190     while ( my $biblios = $sth->fetchrow_hashref ) {
191         $verbose and print 'Bib ' . $biblios->{'biblionumber'} . ':';
192         my $ret = updateMarc( $biblios->{'biblionumber'} );
193         if ($ret) {
194             $verbose and print 'modified';
195             $mod_count++;
196         }
197         $verbose and print "\n";
198     }
199
200     $verbose and print "Number of modified biblios: " . $mod_count . "\n";
201 }
202
203 if ( lc( C4::Context->preference('marcflavour') ) eq "unimarc" ) {
204     $verbose
205       and !$run
206       and print "*** Not in run mode, modifications will not be applyed ***\n";
207
208     $verbose and print "================================\n";
209     process();
210 }
211 else {
212     print
213 "This script is UNIMARC only and should be used only on UNIMARC databases\n";
214 }
215
216 sub print_usage {
217     print <<_USAGE_;
218 Synchronizes date created and date last modified from biblio table to MARC data.
219 Does not update biblio if dates are already synchronized.
220 UNIMARC specific.
221
222 Parameters:
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,
229                                 099c by default
230     --date-modified-marc or m   (optional) date last modified MARC field and optional subfield,
231                                 099d by default
232 _USAGE_
233 }