5 # find Koha's Perl modules
6 # test carefully before changing this
8 eval { require "$FindBin::Bin/../kohalib.pl" };
18 # command-line parameters
23 my $result = GetOptions(
24 'run-update' => \$do_update,
25 'where=s@' => \$wherestrings,
26 'h|help' => \$want_help,
29 if (not $result or $want_help or not $do_update) {
34 my $num_bibs_processed = 0;
35 my $num_bibs_modified = 0;
36 my $num_marc_items_deleted = 0;
37 my $num_marc_items_added = 0;
39 my $dbh = C4::Context->dbh;
40 $dbh->{AutoCommit} = 0;
42 our ($itemtag, $itemsubfield) = GetMarcFromKohaField("items.itemnumber", '');
43 our ($item_sth) = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?");
51 my $sql = "SELECT biblionumber FROM biblio JOIN biblioitems USING (biblionumber)";
52 $sql.="WHERE ". join(" AND ",@$wherestrings) if ($wherestrings);
53 $sql.="ORDER BY biblionumber ASC";
54 my $sth = $dbh->prepare($sql);
55 eval{$sth->execute();};
56 if ($@){ die "error $@";};
57 while (my ($biblionumber) = $sth->fetchrow_array()) {
58 $num_bibs_processed++;
59 process_bib($biblionumber);
61 if (($num_bibs_processed % 100) == 0) {
62 print_progress_and_commit($num_bibs_processed);
70 Embedded item synchronization report
71 ------------------------------------
72 Number of bibs checked: $num_bibs_processed
73 Number of bibs modified: $num_bibs_modified
74 Number of item fields removed from bibs: $num_marc_items_deleted
75 Number of item fields added to bibs: $num_marc_items_added
76 Number of bibs with errors: $num_bad_bibs
81 my $biblionumber = shift;
83 my $bib = GetMarcBiblio($biblionumber);
84 unless (defined $bib) {
85 print "\nCould not retrieve bib $biblionumber from the database - record is corrupt.\n";
92 # delete any item tags
93 foreach my $field ($bib->field($itemtag)) {
94 unless ($bib->delete_field($field)) {
95 warn "Could not delete item in $itemtag for biblionumber $biblionumber";
98 $num_marc_items_deleted++;
102 # add back items from items table
103 $item_sth->execute($biblionumber);
104 while (my $itemnumber = $item_sth->fetchrow_array) {
105 my $marc_item = C4::Items::GetMarcItem($biblionumber, $itemnumber);
106 unless ($marc_item) {
107 warn "FAILED C4::Items::GetMarcItem for biblionumber=$biblionumber, itemnumber=$itemnumber";
110 foreach my $item_field ($marc_item->field($itemtag)) {
111 $bib->insert_fields_ordered($item_field);
112 $num_marc_items_added++;
118 ModBiblioMarc($bib, $biblionumber, GetFrameworkCode($biblionumber));
119 $num_bibs_modified++;
124 sub print_progress_and_commit {
127 print "... processed $recs records\n";
132 $0: synchronize item data embedded in MARC bibs
134 Replaces the item data embedded in the MARC bib
135 records (for indexing) with the authoritative
136 item data as stored in the items table.
138 If Zebra is used, run rebuild_zebra.pl -b -r after
141 NOTE: this script should be run only if there is
142 reason to suspect that the embedded item tags are
143 not in sync with the items table.
146 --run-update run the synchronization
147 --where condition selects the biblios on a criterium (Repeatable)
148 --help or -h show this message.