From aceb8d5a12e12109664adadb35ea906517705802 Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Thu, 8 Mar 2012 11:12:19 +0100
Subject: [PATCH 2/2] Bug 6440: Follow-up: Implement OAI-PMH Sets

- Use Pod::Usage in build_oai_sets.pl
- Update sets at item creation and modification. Modified subroutines are
  AddItem, ModItem and AddItemBatchFromMarc
---
 C4/Items.pm                            |   17 ++++++++++++++++-
 misc/migration_tools/build_oai_sets.pl |   25 ++++++++++++-------------
 2 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/C4/Items.pm b/C4/Items.pm
index 676c91b..bad5a3d 100644
--- a/C4/Items.pm
+++ b/C4/Items.pm
@@ -30,6 +30,7 @@ use MARC::Record;
 use C4::ClassSource;
 use C4::Log;
 use List::MoreUtils qw/any/;
+use C4::OAI::Sets qw/UpdateOAISetsBiblio/;
 use Data::Dumper; # used as part of logging item record changes, not just for
                   # debugging; so please don't remove this
 
@@ -269,7 +270,12 @@ sub AddItem {
     $item->{'itemnumber'} = $itemnumber;
 
     ModZebra( $item->{biblionumber}, "specialUpdate", "biblioserver", undef, undef );
-   
+
+    if(C4::Context->preference('OAI-PMH:AutoUpdateSets')) {
+        my $record = GetMarcBiblio($biblionumber, 1);
+        UpdateOAISetsBiblio($biblionumber, $record);
+    }
+
     logaction("CATALOGUING", "ADD", $itemnumber, "item") if C4::Context->preference("CataloguingLog");
     
     return ($item->{biblionumber}, $item->{biblioitemnumber}, $itemnumber);
@@ -373,6 +379,10 @@ sub AddItemBatchFromMarc {
         $record->delete_field($item_field);
     }
 
+    if(C4::Context->preference('OAI-PMH:AutoUpdateSets')) {
+        UpdateOAISetsBiblio($biblionumber, $record);
+    }
+
     # update the MARC biblio
  #   $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode );
 
@@ -520,6 +530,11 @@ sub ModItem {
     # item status is possible
     ModZebra( $biblionumber, "specialUpdate", "biblioserver", undef, undef );
 
+    if(C4::Context->preference('OAI-PMH:AutoUpdateSets')) {
+        my $record = GetMarcBiblio($biblionumber, 1);
+        UpdateOAISetsBiblio($biblionumber, $record);
+    }
+
     logaction("CATALOGUING", "MODIFY", $itemnumber, Dumper($item)) if C4::Context->preference("CataloguingLog");
 }
 
diff --git a/misc/migration_tools/build_oai_sets.pl b/misc/migration_tools/build_oai_sets.pl
index 577c06e..fa9fb83 100755
--- a/misc/migration_tools/build_oai_sets.pl
+++ b/misc/migration_tools/build_oai_sets.pl
@@ -17,13 +17,22 @@
 # with Koha; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
+=head1 NAME
+
+build_oai_sets.pl - Build OAI-PMH sets for use with opac/oai.pl
+
+=head1 SYNOPSIS
+
+    build_oai_sets.pl -h
+    build_oai_sets.pl -v -r -i -l 5000 -o 5000
+
 =head1 DESCRIPTION
 
 This script build OAI-PMH sets (to be used by opac/oai.pl) according to sets
 and mappings defined in Koha. It reads informations from oai_sets and
 oai_sets_mappings, and then fill table oai_sets_biblios with builded infos.
 
-=head1 USAGE
+=head1 OPTIONS
 
     build_oai_sets.pl [-h] [-v] [-r] [-i] [-l LENGTH [-o OFFSET]]
         -h          Print help message;
@@ -41,6 +50,7 @@ use MARC::Record;
 use MARC::File::XML;
 use List::MoreUtils qw/uniq/;
 use Getopt::Std;
+use Pod::Usage;
 
 use C4::Context;
 use C4::Charset qw/StripNonXmlChars/;
@@ -52,8 +62,7 @@ $Getopt::Std::STANDARD_HELP_VERSION = 1;
 my $go = getopts('vo:l:ihr', \%opts);
 
 if(!$go or $opts{h}){
-    &print_usage;
-    exit;
+    pod2usage(-verbose => 1);
 }
 
 my $verbose = $opts{v};
@@ -153,13 +162,3 @@ if($reset) {
 }
 AddOAISetsBiblios($sets_biblios);
 print "done.\n";
-
-sub print_usage {
-    print "build_oai_sets.pl: Build OAI-PMH sets, according to mappings defined in Koha\n";
-    print "Usage: build_oai_sets.pl [-h] [-v] [-i] [-l LENGTH [-o OFFSET]]\n\n";
-    print "\t-h\t\tPrint this help and exit\n";
-    print "\t-v\t\tBe verbose\n";
-    print "\t-i\t\tEmbed items informations, mandatory if you defined mappings on item fields\n";
-    print "\t-l LENGTH\tProcess LENGTH biblios\n";
-    print "\t-o OFFSET\tIf LENGTH is defined, start processing from OFFSET\n\n";
-}
-- 
1.7.9.1