10 USAGE: update_items.pl -[options]
13 -today Items with a modified timestamp today
14 -biblionumber <integer> One biblionumber to update
15 -itemnumber <integer> One itemnumber to update
20 \$ ./update_items.pl -today
21 [ update items modified today ]
23 \$ ./update_items.pl -biblionumber 2
24 [ update items for biblionumber 2 ]
26 my ($today_only,$biblionumber_to_update,$itemnumber_to_update,$help);
28 'today' => \$today_only,
29 'biblionumber:o' => \$biblionumber_to_update,
30 'itemnumber:o' => \$itemnumber_to_update,
39 # This script can be run from cron or on the command line. It updates
40 # a zebra index with modifications for the period covered or the
41 # biblionumber or itemnumber specified.
43 # You will need to customize this for your installation
44 # I hope that the comments will help -- Josha Ferraro <jmf@liblime.com>
45 my $dbh = C4::Context->dbh;
47 # Get the date, used both for filename and for period we're updating
48 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
51 # nice to zero-pad these dates
52 my $pad_mday= sprintf("%0*d", "2",$mday);
53 my $pad_mon = sprintf("%0*d", "2",$mon);
55 # FIXME: we should define a syspref for 'how often to run this script'
56 my $period = "$year-$pad_mon-$pad_mday%"; # today only - you'll want to customize this bit
57 print "period: $period\n";
58 my $date = "$year$mon$mday$hour$min$sec";
60 # This is the file that zebra will use to update the index at the end of this script
61 my $filename = "records.$date.iso2709";
62 my $biblioserverdir = C4::Context->zebraconfig('biblioserver')->{directory};
64 # check that directory exist, otherwise create.
65 unless (-d "$biblioserverdir/specialUpdate") {
66 system("mkdir -p $biblioserverdir/specialUpdate");
67 print "Info: created $biblioserverdir/specialUpdate\n";
68 $created_dir_or_file++;
71 my $outfile = "$biblioserverdir/specialUpdate/$filename";
73 # open a filehandle for writing records -- notice it's UTF-8
74 open OUT,">utf8",$outfile or die "can't open filehandle\n";
76 # if there aren't any changes, and all we're doing is updating things from today, don't bother
77 print "counting first\n";
78 my $countitems_sth = $dbh->prepare("SELECT COUNT(*) FROM items WHERE timestamp LIKE ?");
79 $countitems_sth->execute($period);
80 my $number_of_items = $countitems_sth->fetchrow();
81 unless ($number_of_items) {
82 print "no recent items to update\n";
86 # get all the relevant biblionumbers, we're gonna update every item in these biblionumbers
87 print "finding biblionumbers\n";
90 $biblionumber_sth = $dbh->prepare("SELECT DISTINCT biblioitems.biblionumber FROM biblioitems LEFT JOIN items ON (biblioitems.biblionumber=items.biblionumber) WHERE items.timestamp LIKE ? AND biblioitems.marcxml IS NOT NULL");
91 $biblionumber_sth->execute($period) or die "problem with query\n";
93 elsif ($biblionumber_to_update) {
94 $biblionumber_sth = $dbh->prepare("SELECT biblionumber FROM biblioitems WHERE marcxml IS NOT NULL AND biblionumber=?");
95 $biblionumber_sth->execute($biblionumber_to_update) or die "problem with query\n";
99 print "fetching marc and items data, updating\n";
100 # for each itemnumber, find the biblionumber, get all the items data for that biblio,
101 # update all the items data in biblioitems.marc, and finally, index it in zebra
104 # 1st : find where some informations are hidden : itemnumber, date_due, popularity
106 my ($itemnumberTag,$itemnumberSubfield) = GetMarcFromKohaField($dbh,"items.itemnumber","");
107 my ($date_dueTag,$date_dueSubfield) = GetMarcFromKohaField($dbh,"items.issues","");
109 while (my $biblionumber=$biblionumber_sth->fetchrow) {
112 # get this biblio's MARC record
113 my $record = MARCgetbiblio($biblionumber);
115 # delete all existing items data from this record FIXME: 952 shouldn't be hardcoded
116 for my $field_to_del ($record->field("952")) {
117 my $del_count = $record->delete_field($field_to_del);
118 print "deleted $del_count fields";
120 # Find out the itemnumbers this biblio has
121 my $itemnumbers_sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
122 $itemnumbers_sth->execute($biblionumber);
124 # for each of the items, get all the item data
125 while (my $data = $itemnumbers_sth->fetchrow_hashref) {
126 my $itemnumber = $data->{itemnumber};
127 my $item_data_sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
128 $item_data_sth->execute($data->{itemnumber});
129 my $item_data_hashref = $item_data_sth->fetchrow_hashref();
131 # create a new MARC::Field object and put a date_due in it (from issues table)
132 my $date_due_sth = $dbh->prepare("SELECT date_due FROM issues WHERE itemnumber=? AND returndate IS NULL");
133 $date_due_sth->execute($itemnumber);
134 my ($date_due) = $date_due_sth->fetchrow();
135 $date_due = "0000-00-00" unless ($date_due);
137 # FIXME: this should use Frameworks!!! -- I'll do it soon -- JF
138 my $items_field = MARC::Field->new( 952, " ", " ", "2" => $date_due, );
140 # put all the data into our MARC::Record field, based on the Koha2MARC mappings
141 for my $label (keys %$item_data_hashref) {
142 if ($item_data_hashref->{$label}) {
143 my $find_tag_subfield_sth = $dbh->prepare("SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE kohafield=?");
144 $find_tag_subfield_sth->execute("items.$label");
145 my ($tag,$subfield) = $find_tag_subfield_sth->fetchrow;
147 $items_field->add_subfields($subfield => $item_data_hashref->{$label} ) unless (!$item_data_hashref->{$label});
148 print "added: $label ($tag $subfield): $item_data_hashref->{$label}";
151 # You probably want to update your mappings if you see anything here ... but
152 # in some cases it's safe to ignore these warnings
153 print "WARN: no items.$label mapping found: $item_data_hashref->{$label}\n";
157 # now update our original record, appending this field
158 $record->insert_fields_ordered($items_field);
160 # at this point, we have an up-to-date MARC record
161 # put it back in biblioitems.marc
162 my $put_back_sth = $dbh->prepare("UPDATE biblioitems SET marc=? WHERE biblionumber=?");
163 $put_back_sth->execute($record->as_usmarc(),$biblionumber);
165 # schedule it for a zebra index update FIXME: we need better error handling
166 print OUT $record->as_usmarc();
168 # FIXME: add time taken
169 print "finished with $count items in\n";
170 # now we're ready to index this change in Zebra and commit it
171 # FIXME: these dirs shouldn't be hardcoded and sudo probably isn't what you use
172 chdir "/koha/zebradb/biblios/tab";
173 my $error = system("sudo zebraidx -g iso2709 -c /koha/etc/zebra-biblios.cfg -d biblios update $outfile");
175 die "update operation failed";
177 $error = system("sudo zebraidx -g iso2709 -c /koha/etc/zebra-biblios.cfg commit");
180 die "commit operation failed";
183 `sudo mv $outfile $outfile.finished`;