Bug 29697: Replace GetMarcBiblio occurrences with $biblio->metadata->record
[srvgit] / misc / migration_tools / create_analytical_rel.pl
1 #!/usr/bin/perl
2
3 use strict;
4 #use warnings; FIXME - Bug 2505
5
6 use Koha::Script;
7 use C4::Context;
8 use C4::Biblio qw( ModBiblio );
9 use Koha::Items;
10 use Koha::Biblios;
11 use Getopt::Long qw( GetOptions );
12
13 $| = 1;
14
15 # command-line parameters
16 my $want_help = 0;
17 my $do_update = 0;
18 my $wherestrings;
19
20 my $result = GetOptions(
21     'run-update'    => \$do_update,
22     'where=s@'         => \$wherestrings,
23     'h|help'        => \$want_help,
24 );
25
26 if (not $result or $want_help or not $do_update) {
27     print_usage();
28     exit 0;
29 }
30
31 my $num_bibs_processed     = 0;
32 my $num_bibs_modified      = 0;
33 my $num_noitem_forbarcode = 0;
34 my $num_nobarcode_inhostfield =0;
35 my $num_hostfields_unabletomodify =0;
36 my $num_bad_bibs           = 0;
37 my $dbh = C4::Context->dbh;
38 $dbh->{AutoCommit} = 0;
39
40 process_bibs();
41 $dbh->commit();
42
43 exit 0;
44
45 sub process_bibs {
46     my $sql = "SELECT biblionumber FROM biblio JOIN biblioitems USING (biblionumber)";
47     $sql.="WHERE ". join(" AND ",@$wherestrings) if ($wherestrings);
48     $sql.="ORDER BY biblionumber ASC";
49     my $sth = $dbh->prepare($sql);
50     eval{$sth->execute();};
51     if ($@){ die "error $@";};
52     while (my ($biblionumber) = $sth->fetchrow_array()) {
53         $num_bibs_processed++;
54         process_bib($biblionumber);
55
56         if (($num_bibs_processed % 100) == 0) {
57             print_progress_and_commit($num_bibs_processed);
58         }
59     }
60
61     $dbh->commit;
62
63     print <<_SUMMARY_;
64
65 Create Analytical records relationships report
66 -----------------------------------------------
67 Number of bibs checked:                   $num_bibs_processed
68 Number of bibs modified:                  $num_bibs_modified
69 Number of hostfields with no barcodes:          $num_nobarcode_inhostfield
70 Number of barcodes not found:                   $num_noitem_forbarcode
71 Number of hostfields unable to modify:          $num_hostfields_unabletomodify
72 Number of bibs with errors:               $num_bad_bibs
73 _SUMMARY_
74 }
75
76 sub process_bib {
77     my $biblionumber = shift;
78
79     my $biblio = Koha::Biblios->find($biblionumber);
80     my $record = $biblio->metadata->record;
81     unless (defined $record) {
82         print "\nCould not retrieve bib $biblionumber from the database - record is corrupt.\n";
83         $num_bad_bibs++;
84         return;
85     }
86         #loop through each host field and populate subfield 0 and 9
87     my $analyticfield = '773';
88     foreach my $hostfield ( $record->field($analyticfield) ) {
89                 if(my $barcode = $hostfield->subfield('o')){
90             my $item = Koha::Items->find({ barcode => $barcode });
91             if ($item) {
92                 my $modif;
93                 if ( $hostfield->subfield('0') ne $biblionumber ) {
94                     $hostfield->update( '0', $biblionumber );
95                     $modif = 1;
96                 }
97                 if ( $hostfield->subfield('9') ne $item->itemnumber ) {
98                     $hostfield->update( '9', $item->itemnumber );
99                     $modif = 1;
100                 }
101                 if ($modif) {
102                     $num_bibs_modified++;
103                     my $modresult = ModBiblio( $record, $biblionumber, '' );
104                     warn "Modifying biblio $biblionumber";
105                     if ( !$modresult ) {
106                         warn "Unable to modify biblio $biblionumber with update host field";
107                         $num_hostfields_unabletomodify++;
108                     }
109                 }
110             }
111             else {
112                 warn "No item record found for barcode $barcode";
113                 $num_noitem_forbarcode++;
114             }
115                 } else{
116                         warn "No barcode in host field for biblionumber $biblionumber";
117                         $num_nobarcode_inhostfield++;
118                 }
119         }
120 }
121
122 sub print_progress_and_commit {
123     my $recs = shift;
124     $dbh->commit();
125     print "... processed $recs records\n";
126 }
127
128 sub print_usage {
129     print <<_USAGE_;
130 $0: establish relationship to host items
131
132 Based on barcode in host field populates subfield 0 with host biblionumber and subfield 9 with host itemnumber.
133
134 Subfield 0 and 9 are used in Koha screns to display relationships between analytical records and host bibs and items.
135
136 NOT usable with UNIMARC data. You can use it only if you have tag 461 with also an items id (like barcode or item numbers). In UNIMARC this situation is very rare. If you have data coded in this way, send a mail to koha-dev mailing list and ask for the feature.
137
138 Parameters:
139     --run-update            run the synchronization
140     --where condition       selects the biblios on a criterium (Repeatable)
141     --help or -h            show this message.
142 _USAGE_
143 }