5 # find Koha's Perl modules
6 # test carefully before changing this
8 eval { require "$FindBin::Bin/../kohalib.pl" };
18 # command-line parameters
22 my $result = GetOptions(
23 'run-update' => \$do_update,
24 'h|help' => \$want_help,
27 if (not $result or $want_help or not $do_update) {
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;
37 my $dbh = C4::Context->dbh;
38 $dbh->{AutoCommit} = 0;
45 my $sql = "SELECT biblionumber FROM biblio ORDER BY biblionumber ASC";
46 my $sth = $dbh->prepare($sql);
48 while (my ($biblionumber) = $sth->fetchrow_array()) {
49 $num_bibs_processed++;
50 process_bib($biblionumber);
52 if (($num_bibs_processed % 100) == 0) {
53 print_progress_and_commit($num_bibs_processed);
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
72 my $biblionumber = shift;
74 my $bib = GetMarcBiblio($biblionumber);
75 unless (defined $bib) {
76 print "\nCould not retrieve bib $biblionumber from the database - record is corrupt.\n";
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++;
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++;
104 ModBiblioMarc($bib, $biblionumber, GetFrameworkCode($biblionumber));
105 $num_bibs_modified++;
110 sub print_progress_and_commit {
113 print "... processed $recs records\n";
118 $0: synchronize item data embedded in MARC bibs
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.
124 If Zebra is used, run rebuild_zebra.pl -b -r after
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.
132 --run-update run the synchronization
133 --help or -h show this message.