5 # find Koha's Perl modules
6 # test carefully before changing this
8 eval { require "$FindBin::Bin/../kohalib.pl" };
16 USAGE: update_items.pl -[options]
19 -today Items with a modified timestamp today
20 -biblionumber <integer> One biblionumber to update
21 -itemnumber <integer> One itemnumber to update
26 \$ ./update_items.pl -today
27 [ update items modified today ]
29 \$ ./update_items.pl -biblionumber 2
30 [ update items for biblionumber 2 ]
32 my ($today_only,$biblionumber_to_update,$itemnumber_to_update,$help);
34 'today' => \$today_only,
35 'biblionumber:o' => \$biblionumber_to_update,
36 'itemnumber:o' => \$itemnumber_to_update,
45 # This script can be run from cron or on the command line. It updates
46 # a zebra index with modifications for the period covered or the
47 # biblionumber or itemnumber specified.
49 # You will need to customize this for your installation
50 # I hope that the comments will help -- Josha Ferraro <jmf@liblime.com>
51 my $dbh = C4::Context->dbh;
53 # Get the date, used both for filename and for period we're updating
54 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
57 # nice to zero-pad these dates
58 my $pad_mday= sprintf("%0*d", "2",$mday);
59 my $pad_mon = sprintf("%0*d", "2",$mon);
61 # FIXME: we should define a syspref for 'how often to run this script'
62 my $period = "$year-$pad_mon-$pad_mday%"; # today only - you'll want to customize this bit
63 print "period: $period\n";
64 my $date = "$year$mon$mday$hour$min$sec";
66 # This is the file that zebra will use to update the index at the end of this script
67 my $filename = "records.$date.iso2709";
68 my $biblioserverdir = C4::Context->zebraconfig('biblioserver')->{directory};
70 # check that directory exist, otherwise create.
71 unless (-d "$biblioserverdir/specialUpdate") {
72 system("mkdir -p $biblioserverdir/specialUpdate");
73 print "Info: created $biblioserverdir/specialUpdate\n";
76 my $outfile = "$biblioserverdir/specialUpdate/$filename";
78 # open a filehandle for writing records -- notice it's UTF-8
79 open OUT,">utf8",$outfile or die "can't open filehandle\n";
81 # if there aren't any changes, and all we're doing is updating things from today, don't bother
82 print "counting first\n";
83 my $countitems_sth = $dbh->prepare("SELECT COUNT(*) FROM items WHERE timestamp LIKE ?");
84 $countitems_sth->execute($period);
85 my $number_of_items = $countitems_sth->fetchrow();
86 unless ($number_of_items) {
87 print "no recent items to update\n";
91 # get all the relevant biblionumbers, we're gonna update every item in these biblionumbers
92 print "finding biblionumbers\n";
95 $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");
96 $biblionumber_sth->execute($period) or die "problem with query\n";
98 elsif ($biblionumber_to_update) {
99 $biblionumber_sth = $dbh->prepare("SELECT biblionumber FROM biblioitems WHERE marcxml IS NOT NULL AND biblionumber=?");
100 $biblionumber_sth->execute($biblionumber_to_update) or die "problem with query\n";
104 print "fetching marc and items data, updating\n";
105 # for each itemnumber, find the biblionumber, get all the items data for that biblio,
106 # update all the items data in biblioitems.marc, and finally, index it in zebra
109 # 1st : find where some informations are hidden : itemnumber, date_due, popularity
111 my ($itemnumberTag,$itemnumberSubfield) = GetMarcFromKohaField("items.itemnumber","");
112 my ($date_dueTag,$date_dueSubfield) = GetMarcFromKohaField("items.issues","");
113 while (my $biblionumber=$biblionumber_sth->fetchrow) {
116 # get this biblio's MARC record
117 my $record = MARCgetbiblio($biblionumber);
119 # delete all existing items data from this record FIXME: 952 shouldn't be hardcoded
120 for my $field_to_del ($record->field("952")) {
121 my $del_count = $record->delete_field($field_to_del);
122 print "deleted $del_count fields";
124 # Find out the itemnumbers this biblio has
125 my $itemnumbers_sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
126 $itemnumbers_sth->execute($biblionumber);
128 # for each of the items, get all the item data
129 while (my $data = $itemnumbers_sth->fetchrow_hashref) {
130 my $itemnumber = $data->{itemnumber};
131 my $item_data_sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
132 $item_data_sth->execute($data->{itemnumber});
133 my $item_data_hashref = $item_data_sth->fetchrow_hashref();
135 # create a new MARC::Field object and put a date_due in it (from issues table)
136 my $date_due_sth = $dbh->prepare("SELECT date_due FROM issues WHERE itemnumber=?");
137 $date_due_sth->execute($itemnumber);
138 my ($date_due) = $date_due_sth->fetchrow();
139 $date_due = "0000-00-00" unless ($date_due);
141 # FIXME: this should use Frameworks!!! -- I'll do it soon -- JF
142 my $items_field = MARC::Field->new( 952, " ", " ", "2" => $date_due, );
144 # put all the data into our MARC::Record field, based on the Koha2MARC mappings
145 for my $label (keys %$item_data_hashref) {
146 if ($item_data_hashref->{$label}) {
147 my $find_tag_subfield_sth = $dbh->prepare("SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE kohafield=?");
148 $find_tag_subfield_sth->execute("items.$label");
149 my ($tag,$subfield) = $find_tag_subfield_sth->fetchrow;
151 $items_field->add_subfields($subfield => $item_data_hashref->{$label} ) unless (!$item_data_hashref->{$label});
152 print "added: $label ($tag $subfield): $item_data_hashref->{$label}";
155 # You probably want to update your mappings if you see anything here ... but
156 # in some cases it's safe to ignore these warnings
157 print "WARN: no items.$label mapping found: $item_data_hashref->{$label}\n";
161 # now update our original record, appending this field
162 $record->insert_fields_ordered($items_field);
164 # at this point, we have an up-to-date MARC record
165 # put it back in biblioitems.marc
166 my $put_back_sth = $dbh->prepare("UPDATE biblioitems SET marc=? WHERE biblionumber=?");
167 $put_back_sth->execute($record->as_usmarc(),$biblionumber);
169 # schedule it for a zebra index update FIXME: we need better error handling
170 print OUT $record->as_usmarc();
172 # FIXME: add time taken
173 print "finished with $count items in\n";
174 # now we're ready to index this change in Zebra and commit it
175 # FIXME: these dirs shouldn't be hardcoded and sudo probably isn't what you use
176 chdir "/koha/zebradb/biblios/tab";
177 my $error = system("sudo zebraidx -g iso2709 -c /koha/etc/zebra-biblios.cfg -d biblios update $outfile");
179 die "update operation failed";
181 $error = system("sudo zebraidx -g iso2709 -c /koha/etc/zebra-biblios.cfg commit");
184 die "commit operation failed";
187 `sudo mv $outfile $outfile.finished`;