[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Koha-cvs] koha/misc update_items.pl [dev_week]
From: |
Joshua Ferraro |
Subject: |
[Koha-cvs] koha/misc update_items.pl [dev_week] |
Date: |
Wed, 04 Oct 2006 02:56:53 +0000 |
CVSROOT: /sources/koha
Module name: koha
Branch: dev_week
Changes by: Joshua Ferraro <kados> 06/10/04 02:56:53
Added files:
misc : update_items.pl
Log message:
Here's a little ditty to put in cron. I run mine once a day. it updates
all the items data in the MARC record (remember, in dev_week/2.4, items
table is authoritative for items data, not MARC.
Biblio.pm has a POD that explains the reasons behind the design choices
we made regarding not aut-updating items with statuses, etc. during
circ.
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/koha/misc/update_items.pl?cvsroot=koha&only_with_tag=dev_week&rev=1.1.2.1
Patches:
Index: update_items.pl
===================================================================
RCS file: update_items.pl
diff -N update_items.pl
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ update_items.pl 4 Oct 2006 02:56:53 -0000 1.1.2.1
@@ -0,0 +1,121 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use C4::Context;
+use MARC::Record;
+
+# This script is meant to be run from cron. It updates
+# a zebra index with modifications for the period covered
+# You will need to customize this for your installation
+# I hope that the comments will help -- Josha Ferraro <address@hidden>
+my $dbh = C4::Context->dbh;
+
+my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
+$year +=1900;
+$mon +=1;
+
+# need to zero-pad these:
+my $pad_mday= sprintf("%0*d", "2",$mday);
+my $pad_mon = sprintf("%0*d", "2",$mon);
+# FIXME: we should define a syspref for 'how often to run this script'
+my $period = "$year-$pad_mon-$pad_mday%"; # today only - you'll want to
customize this bit
+print "period: $period\n";
+my $date = "$year$mon$mday$hour$min$sec";
+my $filename = "records.$date.iso2709";
+my $outfile = "/koha/zebradb/biblios/specialUpdate/$filename";
+
+# open a filehandle for writing records
+open OUT,">utf8",$outfile or die "can't open filehandle\n";
+
+# if there aren't any changes, don't bother
+print "counting first\n";
+my $countitems_sth = $dbh->prepare("SELECT COUNT(*) FROM items WHERE timestamp
LIKE ?");
+$countitems_sth->execute($period);
+my $number_of_items = $countitems_sth->fetchrow();
+unless ($number_of_items) {
+ print "no items to update\n";
+ exit;
+}
+
+# get all the relevant biblionumbers, we're gonna update every item in these
biblionumbers
+print "finding biblionumbers\n";
+my $biblionumber_sth = $dbh->prepare("SELECT DISTINCT biblionumber FROM items
WHERE timestamp LIKE ?");
+$biblionumber_sth->execute($period);
+my $count;
+
+print "fetching marc and items data, updating\n";
+# for each itemnumber, find the biblionumber, get all the items data for that
biblio,
+# update all the items data in biblioitems.marc, and finally, index it in zebra
+while (my $biblionumber=$biblionumber_sth->fetchrow) {
+ $count++;
+
+ # get this biblio's MARC record
+ my $marc_sth = $dbh->prepare("SELECT marc FROM biblioitems WHERE
biblionumber=?");
+ $marc_sth->execute($biblionumber);
+ # transform this data into a MARC::Record object
+ my $record = MARC::Record->new_from_usmarc($marc_sth->fetchrow());
+
+ # delete all existing items data from this record
+ for my $field_to_del ($record->field("952")) {
+ my $del_count = $record->delete_field($field_to_del);
+ print "deleted $del_count fields";
+ }
+ # Find out the itemnumbers this biblio has
+ my $itemnumbers_sth = $dbh->prepare("SELECT itemnumber FROM items WHERE
biblionumber=?");
+ $itemnumbers_sth->execute($biblionumber);
+
+ # for each of the items, get all the item data
+ while (my $data = $itemnumbers_sth->fetchrow_hashref) {
+ my $itemnumber = $data->{itemnumber};
+ my $item_data_sth = $dbh->prepare("SELECT * FROM items WHERE
itemnumber=?");
+ $item_data_sth->execute($data->{itemnumber});
+ my $item_data_hashref = $item_data_sth->fetchrow_hashref();
+
+ # create a new MARC::Record Field, and put a date_due in it
(from issues table)
+ my $date_due_sth = $dbh->prepare("SELECT date_due FROM issues
WHERE itemnumber=? AND returndate IS NULL");
+ $date_due_sth->execute($itemnumber);
+ my ($date_due) = $date_due_sth->fetchrow();
+ $date_due = "0000-00-00" unless ($date_due);
+ # FIXME: this should use Frameworks!!! -- I'll do it soon -- JF
+ my $items_field = MARC::Field->new( 952, " ", " ", "2" =>
$date_due, );
+ # put all the data into our MARC::Record field, based on the
Koha2MARC mappings
+ for my $label (keys %$item_data_hashref) {
+ if ($item_data_hashref->{$label}) {
+ my $find_tag_subfield_sth =
$dbh->prepare("SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE
kohafield=?");
+ $find_tag_subfield_sth->execute("items.$label");
+ my ($tag,$subfield) =
$find_tag_subfield_sth->fetchrow;
+ if ($tag) {
+ $items_field->add_subfields($subfield
=> $item_data_hashref->{$label} ) unless (!$item_data_hashref->{$label});
+ print "added: $label ($tag $subfield):
$item_data_hashref->{$label}";
+ }
+ else {
+ # You probably want to update your
mappings if you see anything here
+ print "losing items.$label data because
no mapping found: $item_data_hashref->{$label}\n";
+ }
+ }
+ }
+ # now update our original record, appending this field
+ $record->insert_fields_ordered($items_field);
+ }
+ # at this point, we have an up-to-date MARC record
+ # put it back in biblioitems.marc
+ my $put_back_sth = $dbh->prepare("UPDATE biblioitems SET marc=? WHERE
biblionumber=?");
+ $put_back_sth->execute($record->as_usmarc(),$biblionumber);
+
+ # schedule it for a zebra index update
+ print OUT $record->as_usmarc();
+}
+# now we're ready to index this change in Zebra and commit it
+# FIXME: these dirs shouldn't be hardcoded and sudo probably isn't what you use
+chdir "/koha/zebradb/biblios/tab";
+my $error = system("sudo zebraidx -g iso2709 -c /koha/etc/zebra-biblios.cfg -d
biblios update $outfile");
+if ($error) {
+ die "commit operation failed";
+}
+$error = system("sudo zebraidx -g iso2709 -c /koha/etc/zebra-biblios.cfg
commit");
+
+if ($error) {
+ die "commit operation failed";
+}
+
+`sudo mv $outfile $outfile.old`;
- [Koha-cvs] koha/misc update_items.pl [dev_week],
Joshua Ferraro <=