Added unit tests to test all of get_amazon_tld in Amazon.pm.
[koha.git] / misc / maintenance / sync_items_in_marc_bib.pl
1 #!/usr/bin/perl
2
3 use strict;
4 #use warnings; FIXME - Bug 2505
5 BEGIN {
6     # find Koha's Perl modules
7     # test carefully before changing this
8     use FindBin;
9     eval { require "$FindBin::Bin/../kohalib.pl" };
10 }
11
12 use C4::Context;
13 use C4::Biblio;
14 use C4::Items;
15 use Getopt::Long;
16
17 $| = 1;
18
19 # command-line parameters
20 my $want_help = 0;
21 my $do_update = 0;
22 my $wherestrings;
23
24 my $result = GetOptions(
25     'run-update'    => \$do_update,
26     'where=s@'         => \$wherestrings,
27     'h|help'        => \$want_help,
28 );
29
30 if (not $result or $want_help or not $do_update) {
31     print_usage();
32     exit 0;
33 }
34
35 my $num_bibs_processed     = 0;
36 my $num_bibs_modified      = 0;
37 my $num_marc_items_deleted = 0;
38 my $num_marc_items_added   = 0;
39 my $num_bad_bibs           = 0;
40 my $dbh = C4::Context->dbh;
41 $dbh->{AutoCommit} = 0;
42
43 our ($itemtag, $itemsubfield) = GetMarcFromKohaField("items.itemnumber", '');
44 our ($item_sth) = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?");
45
46 process_bibs();
47 $dbh->commit();
48
49 exit 0;
50
51 sub process_bibs {
52     my $sql = "SELECT biblionumber FROM biblio JOIN biblioitems USING (biblionumber)";
53     $sql.="WHERE ". join(" AND ",@$wherestrings) if ($wherestrings);
54     $sql.="ORDER BY biblionumber ASC";
55     my $sth = $dbh->prepare($sql);
56     eval{$sth->execute();};
57     if ($@){ die "error $@";};
58     while (my ($biblionumber) = $sth->fetchrow_array()) {
59         $num_bibs_processed++;
60         process_bib($biblionumber);
61
62         if (($num_bibs_processed % 100) == 0) {
63             print_progress_and_commit($num_bibs_processed);
64         }
65     }
66
67     $dbh->commit;
68
69     print <<_SUMMARY_;
70
71 Embedded item synchronization report
72 ------------------------------------
73 Number of bibs checked:                   $num_bibs_processed
74 Number of bibs modified:                  $num_bibs_modified
75 Number of item fields removed from bibs:  $num_marc_items_deleted
76 Number of item fields added to bibs:      $num_marc_items_added
77 Number of bibs with errors:               $num_bad_bibs
78 _SUMMARY_
79 }
80
81 sub process_bib {
82     my $biblionumber = shift;
83
84     my $bib = GetMarcBiblio($biblionumber);
85     unless (defined $bib) {
86         print "\nCould not retrieve bib $biblionumber from the database - record is corrupt.\n";
87         $num_bad_bibs++;
88         return;
89     }
90
91     my $bib_modified = 0;
92
93     # delete any item tags
94     foreach my $field ($bib->field($itemtag)) {
95         unless ($bib->delete_field($field)) {
96             warn "Could not delete item in $itemtag for biblionumber $biblionumber";
97             next;
98         }
99         $num_marc_items_deleted++;
100         $bib_modified = 1;
101     }
102
103     # add back items from items table
104     $item_sth->execute($biblionumber);
105     while (my $itemnumber = $item_sth->fetchrow_array) {
106         my $marc_item = C4::Items::GetMarcItem($biblionumber, $itemnumber);
107         unless ($marc_item) {
108             warn "FAILED C4::Items::GetMarcItem for biblionumber=$biblionumber, itemnumber=$itemnumber";
109             next;
110         }
111         foreach my $item_field ($marc_item->field($itemtag)) {
112             $bib->insert_fields_ordered($item_field);
113             $num_marc_items_added++;
114             $bib_modified = 1;
115         }
116     }
117
118     if ($bib_modified) {
119         ModBiblioMarc($bib, $biblionumber, GetFrameworkCode($biblionumber));
120         $num_bibs_modified++;
121     }
122
123 }
124
125 sub print_progress_and_commit {
126     my $recs = shift;
127     $dbh->commit();
128     print "... processed $recs records\n";
129 }
130
131 sub print_usage {
132     print <<_USAGE_;
133 $0: synchronize item data embedded in MARC bibs
134
135 Replaces the item data embedded in the MARC bib 
136 records (for indexing) with the authoritative 
137 item data as stored in the items table.
138
139 If Zebra is used, run rebuild_zebra.pl -b -r after
140 running this script.
141
142 NOTE: this script should be run only if there is
143 reason to suspect that the embedded item tags are
144 not in sync with the items table.
145
146 Parameters:
147     --run-update            run the synchronization
148     --where condition       selects the biblios on a criterium (Repeatable)
149     --help or -h            show this message.
150 _USAGE_
151 }