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