[Koha-cvs] koha/misc update_items.pl [dev_week]

Joshua Ferraro jmf at kados.org
Wed Oct 11 15:51:48 CEST 2006


CVSROOT:	/sources/koha
Module name:	koha
Branch:		dev_week
Changes by:	Joshua Ferraro <kados>	06/10/11 13:51:48

Modified files:
	misc           : update_items.pl 

Log message:
	Several new features and parameters to pass ... also, better documentation.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/koha/misc/update_items.pl?cvsroot=koha&only_with_tag=dev_week&r1=1.1.2.1&r2=1.1.2.2

Patches:
Index: update_items.pl
===================================================================
RCS file: /sources/koha/koha/misc/Attic/update_items.pl,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -b -r1.1.2.1 -r1.1.2.2
--- update_items.pl	4 Oct 2006 02:56:53 -0000	1.1.2.1
+++ update_items.pl	11 Oct 2006 13:51:48 -0000	1.1.2.2
@@ -4,43 +4,78 @@
 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
+use Getopt::Long;
+my $USAGE = "
+USAGE: update_items.pl -[options]
+
+OPTIONS:
+ -today				Items with a modified timestamp today
+ -biblionumber <integer>	One biblionumber to update
+ -itemnumber <integer>		One itemnumber to update
+
+DEVELOPMENT OPTIONS:
+
+EXAMPLES:
+\$ ./update_items.pl -today
+[ update items modified today ]
+
+\$ ./update_items.pl -biblionumber 2
+[ update items for biblionumber 2 ]
+";
+my ($today_only,$biblionumber_to_update,$itemnumber_to_update);
+GetOptions(
+	'today' => \$today_only,
+	'biblionumber:o' => \$biblionumber_to_update,
+	'itemnumber:o' => \$itemnumber_to_update
+);
+# This script can be run from cron or on the command line. It updates
+# a zebra index with modifications for the period covered or the 
+# biblionumber or itemnumber specified.
+#
 # You will need to customize this for your installation
 # I hope that the comments will help -- Josha Ferraro <jmf at liblime.com>
 my $dbh = C4::Context->dbh;
 
+# Get the date, used both for filename and for period we're updating
 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
 $year +=1900;
 $mon +=1;
-
-# need to zero-pad these:
+# nice to zero-pad these dates
 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";
+
+# This is the file that zebra will use to update the index at the end of this script
 my $filename = "records.$date.iso2709";
 my $outfile = "/koha/zebradb/biblios/specialUpdate/$filename";
-
-# open a filehandle for writing records
+# open a filehandle for writing records -- notice it's UTF-8
 open OUT,">utf8",$outfile or die "can't open filehandle\n";
 
-# if there aren't any changes, don't bother
+# if there aren't any changes, and all we're doing is updating things from today, 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;
+	print "no recent items to update\n";
+	exit if $today_only;
 }
 
 # 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 $biblionumber_sth;
+if ($today_only) {
+	$biblionumber_sth = $dbh->prepare("SELECT DISTINCT biblioitems.biblionumber FROM biblioitems LEFT JOIN items ON (biblioitems.biblionumber=items.biblionumber) WHERE items.timestamp LIKE ? AND biblioitems.marc IS NOT NULL");
+	$biblionumber_sth->execute($period) or die "problem with query\n";
+}
+elsif ($biblionumber_to_update) {
+	$biblionumber_sth = $dbh->prepare("SELECT biblionumber FROM biblioitems WHERE marc IS NOT NULL AND biblionumber=?");
+	$biblionumber_sth->execute($biblionumber_to_update) or die "problem with query\n";
+}
 my $count;
 
 print "fetching marc and items data, updating\n";
@@ -52,10 +87,12 @@
 	# get this biblio's MARC record
 	my $marc_sth = $dbh->prepare("SELECT marc FROM biblioitems WHERE biblionumber=?");
 	$marc_sth->execute($biblionumber);
+
+	# FIXME: we need better error handling here
 	# 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
+	# delete all existing items data from this record FIXME: 952 shouldn't be hardcoded
 	for my $field_to_del ($record->field("952")) {
 		my $del_count = $record->delete_field($field_to_del);
 		print "deleted $del_count fields";
@@ -71,13 +108,15 @@
 		$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)
+		# create a new MARC::Field object 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}) {
@@ -89,8 +128,9 @@
 					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";
+					# You probably want to update your mappings if you see anything here ... but
+					# in some cases it's safe to ignore these warnings
+					print "WARN: no items.$label mapping found: $item_data_hashref->{$label}\n";
                 }
 			}
 		}
@@ -102,15 +142,17 @@
 	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
+	# schedule it for a zebra index update FIXME: we need better error handling
 	print OUT $record->as_usmarc();	
 }
+# FIXME: add time taken
+print "finished with $count items in\n";
 # 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";
+	die "update operation failed";
 }
 $error = system("sudo zebraidx -g iso2709 -c /koha/etc/zebra-biblios.cfg commit");
 
@@ -118,4 +160,4 @@
 	die "commit operation failed";
 }
 
-`sudo mv $outfile $outfile.old`;
+`sudo mv $outfile $outfile.finished`;





More information about the Koha-cvs mailing list