Adding some indexes.
[koha.git] / misc / cronjobs / update_items.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use C4::Context;
5 use C4::Biblio;
6 use MARC::Record;
7
8 use Getopt::Long;
9 my $USAGE = "
10 USAGE: update_items.pl -[options]
11
12 OPTIONS:
13  -today                         Items with a modified timestamp today
14  -biblionumber <integer>        One biblionumber to update
15  -itemnumber <integer>          One itemnumber to update
16
17 DEVELOPMENT OPTIONS:
18
19 EXAMPLES:
20 \$ ./update_items.pl -today
21 [ update items modified today ]
22
23 \$ ./update_items.pl -biblionumber 2
24 [ update items for biblionumber 2 ]
25 ";
26 my ($today_only,$biblionumber_to_update,$itemnumber_to_update,$help);
27 GetOptions(
28         'today' => \$today_only,
29         'biblionumber:o' => \$biblionumber_to_update,
30         'itemnumber:o' => \$itemnumber_to_update,
31         'h' => \$help,
32 );
33
34 if ($h) {
35     print $USAGE."\n";
36     exit;
37 }
38
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.
42 #
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;
46
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);
49 $year +=1900;
50 $mon +=1;
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);
54
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";
59
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};
63
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++;
69 }
70
71 my $outfile = "$biblioserverdir/specialUpdate/$filename";
72
73 # open a filehandle for writing records -- notice it's UTF-8
74 open OUT,">utf8",$outfile or die "can't open filehandle\n";
75
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";
83         exit if $today_only;
84 }
85
86 # get all the relevant biblionumbers, we're gonna update every item in these biblionumbers
87 print "finding biblionumbers\n";
88 my $biblionumber_sth;
89 if ($today_only) {
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";
92 }
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";
96 }
97 my $count;
98
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
102
103 #
104 # 1st : find where some informations are hidden : itemnumber, date_due, popularity
105 #
106 my ($itemnumberTag,$itemnumberSubfield) = GetMarcFromKohaField("items.itemnumber","");
107 my ($date_dueTag,$date_dueSubfield) = GetMarcFromKohaField("items.issues","");
108 my (
109 while (my $biblionumber=$biblionumber_sth->fetchrow) {
110         $count++;
111
112         # get this biblio's MARC record
113     my $record = MARCgetbiblio($biblionumber);
114
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";
119         }
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);
123         
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();
130                 
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);
136
137                 # FIXME: this should use Frameworks!!! -- I'll do it soon -- JF
138                 my $items_field = MARC::Field->new( 952, " ", " ", "2" => $date_due, );
139
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;
146                                 if ($tag) {
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}";
149                                 }
150                                 else {
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";
154                                 }
155                         }
156                 }
157                 # now update our original record, appending this field
158                 $record->insert_fields_ordered($items_field);
159         }
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);
164
165         # schedule it for a zebra index update FIXME: we need better error handling
166         print OUT $record->as_usmarc(); 
167 }
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");
174 if ($error) {
175         die "update operation failed";
176 }
177 $error = system("sudo zebraidx -g iso2709 -c /koha/etc/zebra-biblios.cfg commit");
178
179 if ($error) {
180         die "commit operation failed";
181 }
182
183 `sudo mv $outfile $outfile.finished`;