4 #use warnings; FIXME - Bug 2505
6 # find Koha's Perl modules
7 # test carefully before changing this
9 eval { require "$FindBin::Bin/../kohalib.pl" };
19 # command-line parameters
24 my $result = GetOptions(
25 'run-update' => \$do_update,
26 'where=s@' => \$wherestrings,
27 'h|help' => \$want_help,
30 if (not $result or $want_help or not $do_update) {
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;
40 my $dbh = C4::Context->dbh;
41 $dbh->{AutoCommit} = 0;
43 our ($itemtag, $itemsubfield) = GetMarcFromKohaField("items.itemnumber", '');
44 our ($item_sth) = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?");
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);
62 if (($num_bibs_processed % 100) == 0) {
63 print_progress_and_commit($num_bibs_processed);
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
82 my $biblionumber = shift;
84 my $bib = GetMarcBiblio($biblionumber);
85 unless (defined $bib) {
86 print "\nCould not retrieve bib $biblionumber from the database - record is corrupt.\n";
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";
99 $num_marc_items_deleted++;
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";
111 foreach my $item_field ($marc_item->field($itemtag)) {
112 $bib->insert_fields_ordered($item_field);
113 $num_marc_items_added++;
119 ModBiblioMarc($bib, $biblionumber, GetFrameworkCode($biblionumber));
120 $num_bibs_modified++;
125 sub print_progress_and_commit {
128 print "... processed $recs records\n";
133 $0: synchronize item data embedded in MARC bibs
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.
139 If Zebra is used, run rebuild_zebra.pl -b -r after
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.
147 --run-update run the synchronization
148 --where condition selects the biblios on a criterium (Repeatable)
149 --help or -h show this message.